Browse Source

one key dictionary comparison added

master
Dirk Alders 2 years ago
parent
commit
79b444272f
2 changed files with 18 additions and 2 deletions
  1. 3
    1
      example_config/config.py
  2. 15
    1
      exec_command.py

+ 3
- 1
example_config/config.py View File

10
 MQTT_SERVER = "host"
10
 MQTT_SERVER = "host"
11
 
11
 
12
 TOPIC = 'topic'
12
 TOPIC = 'topic'
13
-PAYLOAD = b'payload'
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
+
14
 COMMAND = "aplay %s/ring.wav -D plughw:UACDemoV10,0" % __BASEPATH__
16
 COMMAND = "aplay %s/ring.wav -D plughw:UACDemoV10,0" % __BASEPATH__
15
 
17
 
16
 #
18
 #

+ 15
- 1
exec_command.py View File

1
 import config
1
 import config
2
+import json
2
 import logging
3
 import logging
3
 import mqtt
4
 import mqtt
4
 import os
5
 import os
27
 
28
 
28
     def mqtt_rx(self, client, userdate, message):
29
     def mqtt_rx(self, client, userdate, message):
29
         if not self.__block_execution__:
30
         if not self.__block_execution__:
30
-            if message.payload == config.PAYLOAD:
31
+            data = None
32
+            if config.PAYLOAD_KEY is None:
33
+                try:
34
+                    data = message.payload.decode('utf-8')
35
+                except:
36
+                    logger.exception("Error decoding mqtt message")
37
+            else:
38
+                try:
39
+                    payload = json.loads(message.payload)
40
+                except:
41
+                    logger.exception("Error decoding json mqtt message")
42
+                else:
43
+                    data = payload.get(config.PAYLOAD_KEY)
44
+            if config.PAYLOAD_DATA is None or data == config.PAYLOAD_DATA:
31
                 logger.debug("Starting execution in background...")
45
                 logger.debug("Starting execution in background...")
32
                 self.__block_execution__ = True
46
                 self.__block_execution__ = True
33
                 self.enqueue(5, self.exec_command)
47
                 self.enqueue(5, self.exec_command)

Loading…
Cancel
Save