Possibility for multiple topics, keys, data and commands implemented
This commit is contained in:
parent
299917736d
commit
5736a42f6e
@ -9,11 +9,18 @@ MQTT_USER = "user"
|
|||||||
MQTT_PASS = "pass"
|
MQTT_PASS = "pass"
|
||||||
MQTT_SERVER = "host"
|
MQTT_SERVER = "host"
|
||||||
|
|
||||||
TOPIC = 'topic'
|
EXEC_LIST = {
|
||||||
PAYLOAD_KEY = 'key' # Give a key or None to compare plain DATA
|
'topic_1': {
|
||||||
PAYLOAD_DATA = 'data' # Give data to compare or None if no comparison is needed
|
'key': 'key', # Give a key or None to compare plain DATA
|
||||||
|
'data': 'data', # Give data to compare or None if no comparison is needed
|
||||||
COMMAND = "aplay %s/ring.wav -D plughw:UACDemoV10,0" % __BASEPATH__
|
'command': 'echo 123'
|
||||||
|
},
|
||||||
|
'topic_2': {
|
||||||
|
'key': None, # Give a key or None to compare plain DATA
|
||||||
|
'data': 'data', # Give data to compare or None if no comparison is needed
|
||||||
|
'command': 'echo 321'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#
|
#
|
||||||
# Logging
|
# Logging
|
||||||
|
@ -17,18 +17,21 @@ class exec_command(mqtt.mqtt_client):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__block_execution__ = False
|
self.__block_execution__ = False
|
||||||
mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
||||||
self.add_callback(config.TOPIC, self.mqtt_rx)
|
for topic in config.EXEC_LIST:
|
||||||
|
self.add_callback(topic, self.mqtt_rx)
|
||||||
# Start a pseudo process
|
# Start a pseudo process
|
||||||
self.process = subprocess.Popen(["sleep", "0"])
|
self.process = subprocess.Popen(["sleep", "0"])
|
||||||
|
|
||||||
def exec_command(self):
|
def exec_command(self, cmd):
|
||||||
self.process = subprocess.Popen(config.COMMAND.split(" "))
|
self.process = subprocess.Popen(cmd.split(" "))
|
||||||
|
|
||||||
def mqtt_rx(self, client, userdate, message):
|
def mqtt_rx(self, client, userdate, message):
|
||||||
data = None
|
payload = None
|
||||||
if config.PAYLOAD_KEY is None:
|
key = config.EXEC_LIST[message.topic].get('key')
|
||||||
|
data = config.EXEC_LIST[message.topic].get('data')
|
||||||
|
if key is None:
|
||||||
try:
|
try:
|
||||||
data = message.payload.decode('utf-8')
|
payload = message.payload.decode('utf-8')
|
||||||
except:
|
except:
|
||||||
logger.exception("Error decoding mqtt message")
|
logger.exception("Error decoding mqtt message")
|
||||||
else:
|
else:
|
||||||
@ -37,12 +40,15 @@ class exec_command(mqtt.mqtt_client):
|
|||||||
except:
|
except:
|
||||||
logger.exception("Error decoding json mqtt message")
|
logger.exception("Error decoding json mqtt message")
|
||||||
else:
|
else:
|
||||||
data = payload.get(config.PAYLOAD_KEY)
|
try:
|
||||||
if config.PAYLOAD_DATA is None or data == config.PAYLOAD_DATA:
|
payload = payload.get(key)
|
||||||
|
except AttributeError:
|
||||||
|
logger.exception("payload seems to be no dictionary")
|
||||||
|
if data is None or payload == data:
|
||||||
if self.process.poll() is None:
|
if self.process.poll() is None:
|
||||||
self.process.kill()
|
self.process.kill()
|
||||||
logger.debug("Starting execution in background...")
|
logger.debug("Starting execution in background...")
|
||||||
self.exec_command()
|
self.exec_command(config.EXEC_LIST[message.topic]['command'])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user