|
@@ -2,9 +2,8 @@ import config
|
2
|
2
|
import json
|
3
|
3
|
import logging
|
4
|
4
|
import mqtt
|
5
|
|
-import os
|
6
|
5
|
import report
|
7
|
|
-import task
|
|
6
|
+import subprocess
|
8
|
7
|
import time
|
9
|
8
|
|
10
|
9
|
try:
|
|
@@ -14,46 +13,42 @@ except ImportError:
|
14
|
13
|
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
15
|
14
|
|
16
|
15
|
|
17
|
|
-class exec_command(mqtt.mqtt_client, task.threaded_queue):
|
|
16
|
+class exec_command(mqtt.mqtt_client):
|
18
|
17
|
def __init__(self):
|
19
|
18
|
self.__block_execution__ = False
|
20
|
|
- task.threaded_queue.__init__(self)
|
21
|
19
|
mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
22
|
20
|
self.add_callback(config.TOPIC, self.mqtt_rx)
|
|
21
|
+ # Start a pseudo process
|
|
22
|
+ self.process = subprocess.Popen(["sleep", "0"])
|
23
|
23
|
|
24
|
|
- def exec_command(self, rt):
|
25
|
|
- os.system(config.COMMAND)
|
26
|
|
- self.__block_execution__ = False
|
27
|
|
-
|
|
24
|
+ def exec_command(self):
|
|
25
|
+ self.process = subprocess.Popen(config.COMMAND.split(" "))
|
28
|
26
|
|
29
|
27
|
def mqtt_rx(self, client, userdate, message):
|
30
|
|
- if not self.__block_execution__:
|
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:
|
45
|
|
- logger.debug("Starting execution in background...")
|
46
|
|
- self.__block_execution__ = True
|
47
|
|
- self.enqueue(5, self.exec_command)
|
|
28
|
+ data = None
|
|
29
|
+ if config.PAYLOAD_KEY is None:
|
|
30
|
+ try:
|
|
31
|
+ data = message.payload.decode('utf-8')
|
|
32
|
+ except:
|
|
33
|
+ logger.exception("Error decoding mqtt message")
|
48
|
34
|
else:
|
49
|
|
- logger.debug("Execution in progress. Ignoring request...")
|
|
35
|
+ try:
|
|
36
|
+ payload = json.loads(message.payload)
|
|
37
|
+ except:
|
|
38
|
+ logger.exception("Error decoding json mqtt message")
|
|
39
|
+ else:
|
|
40
|
+ data = payload.get(config.PAYLOAD_KEY)
|
|
41
|
+ if config.PAYLOAD_DATA is None or data == config.PAYLOAD_DATA:
|
|
42
|
+ if self.process.poll() is None:
|
|
43
|
+ self.process.kill()
|
|
44
|
+ logger.debug("Starting execution in background...")
|
|
45
|
+ self.exec_command()
|
50
|
46
|
|
51
|
47
|
|
52
|
48
|
if __name__ == '__main__':
|
53
|
49
|
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|
54
|
50
|
#
|
55
|
51
|
ec = exec_command()
|
56
|
|
- ec.run()
|
57
|
52
|
#
|
58
|
53
|
while True:
|
59
|
54
|
time.sleep(30)
|