diff --git a/example_config/config.py b/example_config/config.py index 668f0d1..0de4b4a 100644 --- a/example_config/config.py +++ b/example_config/config.py @@ -10,7 +10,9 @@ MQTT_PASS = "pass" MQTT_SERVER = "host" TOPIC = 'topic' -PAYLOAD = b'payload' +PAYLOAD_KEY = 'key' # Give a key or None to compare plain DATA +PAYLOAD_DATA = 'data' # Give data to compare or None if no comparison is needed + COMMAND = "aplay %s/ring.wav -D plughw:UACDemoV10,0" % __BASEPATH__ # diff --git a/exec_command.py b/exec_command.py index a66b68f..b17920c 100644 --- a/exec_command.py +++ b/exec_command.py @@ -1,4 +1,5 @@ import config +import json import logging import mqtt import os @@ -27,7 +28,20 @@ class exec_command(mqtt.mqtt_client, task.threaded_queue): def mqtt_rx(self, client, userdate, message): if not self.__block_execution__: - if message.payload == config.PAYLOAD: + data = None + if config.PAYLOAD_KEY is None: + try: + data = message.payload.decode('utf-8') + except: + logger.exception("Error decoding mqtt message") + else: + try: + payload = json.loads(message.payload) + except: + logger.exception("Error decoding json mqtt message") + else: + data = payload.get(config.PAYLOAD_KEY) + if config.PAYLOAD_DATA is None or data == config.PAYLOAD_DATA: logger.debug("Starting execution in background...") self.__block_execution__ = True self.enqueue(5, self.exec_command)