瀏覽代碼

Module mqtt implemented

master
Dirk Alders 2 年之前
父節點
當前提交
70600ba2e7
共有 3 個檔案被更改,包括 26 行新增43 行删除
  1. 3
    0
      .gitmodules
  2. 1
    0
      mqtt
  3. 22
    43
      powerplug.py

+ 3
- 0
.gitmodules 查看文件

@@ -1,3 +1,6 @@
1 1
 [submodule "report"]
2 2
 	path = report
3 3
 	url = https://git.mount-mockery.de/pylib/report.git
4
+[submodule "mqtt"]
5
+	path = mqtt
6
+	url = https://git.mount-mockery.de/pylib/mqtt.git

+ 1
- 0
mqtt

@@ -0,0 +1 @@
1
+Subproject commit 3d9fc164b4a2cca4a528bd71f04b87a50cd5a078

+ 22
- 43
powerplug.py 查看文件

@@ -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

Loading…
取消
儲存