|
@@ -1,6 +1,6 @@
|
1
|
1
|
import config
|
2
|
2
|
import logging
|
3
|
|
-import paho.mqtt.client as paho
|
|
3
|
+import mqtt
|
4
|
4
|
import report
|
5
|
5
|
import socket
|
6
|
6
|
import subprocess
|
|
@@ -72,53 +72,32 @@ class sispmctl(object):
|
72
|
72
|
topic = config.MQTT_TOPIC + "/status/" + str(output)
|
73
|
73
|
logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
|
74
|
74
|
try:
|
75
|
|
- self.__mqtt_client__.publish(topic, "true" if self.__state__[output - 1] else "false")
|
|
75
|
+ self.__mqtt_client__.send(topic, "true" if self.__state__[output - 1] else "false")
|
76
|
76
|
except (socket.timeout, OSError) as e:
|
77
|
77
|
logger.warning("Erro while sending state information information")
|
78
|
78
|
|
79
|
79
|
|
80
|
|
-class mqtt_powerplug(object):
|
81
|
|
- SUBTOPICS = [
|
82
|
|
- "set/1",
|
83
|
|
- "set/2",
|
84
|
|
- "set/3",
|
85
|
|
- "set/4",
|
86
|
|
- "set/all",
|
87
|
|
- "toggle/1",
|
88
|
|
- "toggle/2",
|
89
|
|
- "toggle/3",
|
90
|
|
- "toggle/4",
|
91
|
|
- "toggle/all",
|
92
|
|
- ]
|
93
|
|
-
|
|
80
|
+class mqtt_powerplug(mqtt.mqtt_client):
|
94
|
81
|
def __init__(self):
|
95
|
|
- self.__client__ = paho.Client(config.APP_NAME) # create client object
|
96
|
|
- self.__client__.on_message = self.__receive__ # attach function to callback
|
97
|
|
- self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
|
98
|
|
- try:
|
99
|
|
- self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
|
100
|
|
- self.__client__.loop_start() # start the loop
|
101
|
|
- self.__topics__ = []
|
102
|
|
- for subtopic in self.SUBTOPICS:
|
103
|
|
- self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
|
104
|
|
- self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
|
105
|
|
- except (socket.timeout, OSError) as e:
|
106
|
|
- logger.warning("Erro while setting up mqtt instance and listener")
|
107
|
|
- self.__sc__ = sispmctl(self.__client__)
|
108
|
|
-
|
109
|
|
- def __receive__(self, client, userdata, message):
|
110
|
|
- if message.topic in self.__topics__:
|
111
|
|
- output = message.topic.split("/")[-1]
|
112
|
|
- if message.topic.find("set") >= 0:
|
113
|
|
- state = message.payload == b"true"
|
114
|
|
- logger.info("Received request to set output channel %s to state %s", output, str(state))
|
115
|
|
- self.__sc__.set_out_state(output, state)
|
116
|
|
- elif message.topic.find("toggle") >= 0:
|
117
|
|
- logger.info("Received request to toggle output channel %s", output)
|
118
|
|
- if message.payload != b"false":
|
119
|
|
- self.__sc__.toggle_out_state(output)
|
120
|
|
- else:
|
121
|
|
- logger.warning("Ignoring unknown mqtt topic %s", message.topic)
|
|
82
|
+ mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
|
83
|
+ for target in ["1", "2", "3", "4", "all"]:
|
|
84
|
+ self.add_callback(config.MQTT_TOPIC + "/set/" + target, self.__set__)
|
|
85
|
+ self.add_callback(config.MQTT_TOPIC + "/toggle/" + target, self.__toggle__)
|
|
86
|
+ #
|
|
87
|
+ self.__sc__ = sispmctl(self)
|
|
88
|
+
|
|
89
|
+ def __set__(self, client, userdata, message):
|
|
90
|
+ output = message.topic.split("/")[-1]
|
|
91
|
+ if message.topic.find("set") >= 0:
|
|
92
|
+ state = message.payload == b"true"
|
|
93
|
+ logger.info("Received request to set output channel %s to state %s", output, str(state))
|
|
94
|
+ self.__sc__.set_out_state(output, state)
|
|
95
|
+
|
|
96
|
+ def __toggle__(self, client, userdata, message):
|
|
97
|
+ output = message.topic.split("/")[-1]
|
|
98
|
+ logger.info("Received request to toggle output channel %s", output)
|
|
99
|
+ if message.payload != b"false":
|
|
100
|
+ self.__sc__.toggle_out_state(output)
|
122
|
101
|
|
123
|
102
|
def __del__(self):
|
124
|
103
|
self.__client__.loop_stop() # stop the loop
|