Parcourir la source

one key dictionary comparison added

master
Dirk Alders il y a 2 ans
Parent
révision
79b444272f
2 fichiers modifiés avec 18 ajouts et 2 suppressions
  1. 3
    1
      example_config/config.py
  2. 15
    1
      exec_command.py

+ 3
- 1
example_config/config.py Voir le fichier

@@ -10,7 +10,9 @@ MQTT_PASS = "pass"
10 10
 MQTT_SERVER = "host"
11 11
 
12 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 16
 COMMAND = "aplay %s/ring.wav -D plughw:UACDemoV10,0" % __BASEPATH__
15 17
 
16 18
 #

+ 15
- 1
exec_command.py Voir le fichier

@@ -1,4 +1,5 @@
1 1
 import config
2
+import json
2 3
 import logging
3 4
 import mqtt
4 5
 import os
@@ -27,7 +28,20 @@ class exec_command(mqtt.mqtt_client, task.threaded_queue):
27 28
 
28 29
     def mqtt_rx(self, client, userdate, message):
29 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 45
                 logger.debug("Starting execution in background...")
32 46
                 self.__block_execution__ = True
33 47
                 self.enqueue(5, self.exec_command)

Chargement…
Annuler
Enregistrer