浏览代码

Possibility for multiple topics, keys, data and commands implemented

master
Dirk Alders 2 年前
父节点
当前提交
5736a42f6e
共有 3 个文件被更改,包括 27 次插入15 次删除
  1. 12
    5
      example_config/config.py
  2. 15
    9
      exec_command.py
  3. 0
    1
      ring.wav

+ 12
- 5
example_config/config.py 查看文件

@@ -9,11 +9,18 @@ MQTT_USER = "user"
9 9
 MQTT_PASS = "pass"
10 10
 MQTT_SERVER = "host"
11 11
 
12
-TOPIC = 'topic'
13
-PAYLOAD_KEY = 'key'             # Give a key or None to compare plain DATA
14
-PAYLOAD_DATA = 'data'           # Give data to compare or None if no comparison is needed
15
-
16
-COMMAND = "aplay %s/ring.wav -D plughw:UACDemoV10,0" % __BASEPATH__
12
+EXEC_LIST = {
13
+    'topic_1': {
14
+        'key': 'key',                   # Give a key or None to compare plain DATA
15
+        'data': 'data',                 # Give data to compare or None if no comparison is needed
16
+        'command': 'echo 123'
17
+    },
18
+    'topic_2': {
19
+        'key': None,                    # Give a key or None to compare plain DATA
20
+        'data': 'data',                 # Give data to compare or None if no comparison is needed
21
+        'command': 'echo 321'
22
+    }
23
+}
17 24
 
18 25
 #
19 26
 # Logging

+ 15
- 9
exec_command.py 查看文件

@@ -17,18 +17,21 @@ class exec_command(mqtt.mqtt_client):
17 17
     def __init__(self):
18 18
         self.__block_execution__ = False
19 19
         mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
20
-        self.add_callback(config.TOPIC, self.mqtt_rx)
20
+        for topic in config.EXEC_LIST:
21
+            self.add_callback(topic, self.mqtt_rx)
21 22
         # Start a pseudo process
22 23
         self.process = subprocess.Popen(["sleep", "0"])
23 24
 
24
-    def exec_command(self):
25
-        self.process = subprocess.Popen(config.COMMAND.split(" "))
25
+    def exec_command(self, cmd):
26
+        self.process = subprocess.Popen(cmd.split(" "))
26 27
 
27 28
     def mqtt_rx(self, client, userdate, message):
28
-        data = None
29
-        if config.PAYLOAD_KEY is None:
29
+        payload = None
30
+        key = config.EXEC_LIST[message.topic].get('key')
31
+        data = config.EXEC_LIST[message.topic].get('data')
32
+        if key is None:
30 33
             try:
31
-                data = message.payload.decode('utf-8')
34
+                payload = message.payload.decode('utf-8')
32 35
             except:
33 36
                 logger.exception("Error decoding mqtt message")
34 37
         else:
@@ -37,12 +40,15 @@ class exec_command(mqtt.mqtt_client):
37 40
             except:
38 41
                 logger.exception("Error decoding json mqtt message")
39 42
             else:
40
-                data = payload.get(config.PAYLOAD_KEY)
41
-        if config.PAYLOAD_DATA is None or data == config.PAYLOAD_DATA:
43
+                try:
44
+                    payload = payload.get(key)
45
+                except AttributeError:
46
+                    logger.exception("payload seems to be no dictionary")
47
+        if data is None or payload == data:
42 48
             if self.process.poll() is None:
43 49
                 self.process.kill()
44 50
             logger.debug("Starting execution in background...")
45
-            self.exec_command()
51
+            self.exec_command(config.EXEC_LIST[message.topic]['command'])
46 52
 
47 53
 
48 54
 if __name__ == '__main__': 

+ 0
- 1
ring.wav 查看文件

@@ -1 +0,0 @@
1
-sounds/door-bell.wav

正在加载...
取消
保存