diff --git a/.gitmodules b/.gitmodules index 98f8bb5..9612d14 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "report"] path = report url = https://git.mount-mockery.de/pylib/report.git +[submodule "mqtt"] + path = mqtt + url = https://git.mount-mockery.de/pylib/mqtt.git diff --git a/mqtt b/mqtt new file mode 160000 index 0000000..3d9fc16 --- /dev/null +++ b/mqtt @@ -0,0 +1 @@ +Subproject commit 3d9fc164b4a2cca4a528bd71f04b87a50cd5a078 diff --git a/powerplug.py b/powerplug.py index e2f98ac..7dd5b12 100644 --- a/powerplug.py +++ b/powerplug.py @@ -1,6 +1,6 @@ import config import logging -import paho.mqtt.client as paho +import mqtt import report import socket import subprocess @@ -72,53 +72,32 @@ class sispmctl(object): topic = config.MQTT_TOPIC + "/status/" + str(output) logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1])) try: - self.__mqtt_client__.publish(topic, "true" if self.__state__[output - 1] else "false") + self.__mqtt_client__.send(topic, "true" if self.__state__[output - 1] else "false") except (socket.timeout, OSError) as e: logger.warning("Erro while sending state information information") -class mqtt_powerplug(object): - SUBTOPICS = [ - "set/1", - "set/2", - "set/3", - "set/4", - "set/all", - "toggle/1", - "toggle/2", - "toggle/3", - "toggle/4", - "toggle/all", - ] - +class mqtt_powerplug(mqtt.mqtt_client): def __init__(self): - self.__client__ = paho.Client(config.APP_NAME) # create client object - self.__client__.on_message = self.__receive__ # attach function to callback - self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials - try: - self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection - self.__client__.loop_start() # start the loop - self.__topics__ = [] - for subtopic in self.SUBTOPICS: - self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic) - self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic - except (socket.timeout, OSError) as e: - logger.warning("Erro while setting up mqtt instance and listener") - self.__sc__ = sispmctl(self.__client__) + mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS) + for target in ["1", "2", "3", "4", "all"]: + self.add_callback(config.MQTT_TOPIC + "/set/" + target, self.__set__) + self.add_callback(config.MQTT_TOPIC + "/toggle/" + target, self.__toggle__) + # + self.__sc__ = sispmctl(self) - def __receive__(self, client, userdata, message): - if message.topic in self.__topics__: - output = message.topic.split("/")[-1] - if message.topic.find("set") >= 0: - state = message.payload == b"true" - logger.info("Received request to set output channel %s to state %s", output, str(state)) - self.__sc__.set_out_state(output, state) - elif message.topic.find("toggle") >= 0: - logger.info("Received request to toggle output channel %s", output) - if message.payload != b"false": - self.__sc__.toggle_out_state(output) - else: - logger.warning("Ignoring unknown mqtt topic %s", message.topic) + def __set__(self, client, userdata, message): + output = message.topic.split("/")[-1] + if message.topic.find("set") >= 0: + state = message.payload == b"true" + logger.info("Received request to set output channel %s to state %s", output, str(state)) + self.__sc__.set_out_state(output, state) + + def __toggle__(self, client, userdata, message): + output = message.topic.split("/")[-1] + logger.info("Received request to toggle output channel %s", output) + if message.payload != b"false": + self.__sc__.toggle_out_state(output) def __del__(self): self.__client__.loop_stop() # stop the loop