Browse Source

Possibility for multiple topics, keys, data and commands implemented

master
Dirk Alders 2 years ago
parent
commit
5736a42f6e
3 changed files with 27 additions and 15 deletions
  1. 12
    5
      example_config/config.py
  2. 15
    9
      exec_command.py
  3. 0
    1
      ring.wav

+ 12
- 5
example_config/config.py View File

9
 MQTT_PASS = "pass"
9
 MQTT_PASS = "pass"
10
 MQTT_SERVER = "host"
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
 # Logging
26
 # Logging

+ 15
- 9
exec_command.py View File

17
     def __init__(self):
17
     def __init__(self):
18
         self.__block_execution__ = False
18
         self.__block_execution__ = False
19
         mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
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
         # Start a pseudo process
22
         # Start a pseudo process
22
         self.process = subprocess.Popen(["sleep", "0"])
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
     def mqtt_rx(self, client, userdate, message):
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
             try:
33
             try:
31
-                data = message.payload.decode('utf-8')
34
+                payload = message.payload.decode('utf-8')
32
             except:
35
             except:
33
                 logger.exception("Error decoding mqtt message")
36
                 logger.exception("Error decoding mqtt message")
34
         else:
37
         else:
37
             except:
40
             except:
38
                 logger.exception("Error decoding json mqtt message")
41
                 logger.exception("Error decoding json mqtt message")
39
             else:
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
             if self.process.poll() is None:
48
             if self.process.poll() is None:
43
                 self.process.kill()
49
                 self.process.kill()
44
             logger.debug("Starting execution in background...")
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
 if __name__ == '__main__': 
54
 if __name__ == '__main__': 

+ 0
- 1
ring.wav View File

1
-sounds/door-bell.wav

Loading…
Cancel
Save