|
@@ -17,18 +17,21 @@ class exec_command(mqtt.mqtt_client):
|
17
|
17
|
def __init__(self):
|
18
|
18
|
self.__block_execution__ = False
|
19
|
19
|
mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
20
|
|
- self.add_callback(config.TOPIC, self.mqtt_rx)
|
|
20
|
+ for topic in config.EXEC_LIST:
|
|
21
|
+ self.add_callback(topic, self.mqtt_rx)
|
21
|
22
|
# Start a pseudo process
|
22
|
23
|
self.process = subprocess.Popen(["sleep", "0"])
|
23
|
24
|
|
24
|
|
- def exec_command(self):
|
25
|
|
- self.process = subprocess.Popen(config.COMMAND.split(" "))
|
|
25
|
+ def exec_command(self, cmd):
|
|
26
|
+ self.process = subprocess.Popen(cmd.split(" "))
|
26
|
27
|
|
27
|
28
|
def mqtt_rx(self, client, userdate, message):
|
28
|
|
- data = None
|
29
|
|
- if config.PAYLOAD_KEY is None:
|
|
29
|
+ payload = None
|
|
30
|
+ key = config.EXEC_LIST[message.topic].get('key')
|
|
31
|
+ data = config.EXEC_LIST[message.topic].get('data')
|
|
32
|
+ if key is None:
|
30
|
33
|
try:
|
31
|
|
- data = message.payload.decode('utf-8')
|
|
34
|
+ payload = message.payload.decode('utf-8')
|
32
|
35
|
except:
|
33
|
36
|
logger.exception("Error decoding mqtt message")
|
34
|
37
|
else:
|
|
@@ -37,12 +40,15 @@ class exec_command(mqtt.mqtt_client):
|
37
|
40
|
except:
|
38
|
41
|
logger.exception("Error decoding json mqtt message")
|
39
|
42
|
else:
|
40
|
|
- data = payload.get(config.PAYLOAD_KEY)
|
41
|
|
- if config.PAYLOAD_DATA is None or data == config.PAYLOAD_DATA:
|
|
43
|
+ try:
|
|
44
|
+ payload = payload.get(key)
|
|
45
|
+ except AttributeError:
|
|
46
|
+ logger.exception("payload seems to be no dictionary")
|
|
47
|
+ if data is None or payload == data:
|
42
|
48
|
if self.process.poll() is None:
|
43
|
49
|
self.process.kill()
|
44
|
50
|
logger.debug("Starting execution in background...")
|
45
|
|
- self.exec_command()
|
|
51
|
+ self.exec_command(config.EXEC_LIST[message.topic]['command'])
|
46
|
52
|
|
47
|
53
|
|
48
|
54
|
if __name__ == '__main__':
|