one key dictionary comparison added

This commit is contained in:
Dirk Alders 2022-09-22 09:32:43 +01:00
parent 50ecad5a4a
commit 79b444272f
2 changed files with 18 additions and 2 deletions

View File

@ -10,7 +10,9 @@ MQTT_PASS = "pass"
MQTT_SERVER = "host" MQTT_SERVER = "host"
TOPIC = 'topic' 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__ COMMAND = "aplay %s/ring.wav -D plughw:UACDemoV10,0" % __BASEPATH__
# #

View File

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