|
@@ -0,0 +1,49 @@
|
|
1
|
+import config
|
|
2
|
+import logging
|
|
3
|
+import mqtt
|
|
4
|
+import os
|
|
5
|
+import report
|
|
6
|
+import task
|
|
7
|
+import time
|
|
8
|
+
|
|
9
|
+try:
|
|
10
|
+ from config import APP_NAME as ROOT_LOGGER_NAME
|
|
11
|
+except ImportError:
|
|
12
|
+ ROOT_LOGGER_NAME = 'root'
|
|
13
|
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+class exec_command(mqtt.mqtt_client, task.threaded_queue):
|
|
17
|
+ def __init__(self):
|
|
18
|
+ self.__block_execution__ = False
|
|
19
|
+ task.threaded_queue.__init__(self)
|
|
20
|
+ mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
|
21
|
+ self.add_callback(config.TOPIC, self.mqtt_rx)
|
|
22
|
+
|
|
23
|
+ def exec_command(self, rt):
|
|
24
|
+ os.system(config.COMMAND)
|
|
25
|
+ self.__block_execution__ = False
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+ def mqtt_rx(self, client, userdate, message):
|
|
29
|
+ if not self.__block_execution__:
|
|
30
|
+ if message.payload == config.PAYLOAD:
|
|
31
|
+ logger.debug("Starting execution in background...")
|
|
32
|
+ self.__block_execution__ = True
|
|
33
|
+ self.enqueue(5, self.exec_command)
|
|
34
|
+ else:
|
|
35
|
+ logger.debug("Execution in progress. Ignoring request...")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+if __name__ == '__main__':
|
|
39
|
+ report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|
|
40
|
+ #
|
|
41
|
+ ec = exec_command()
|
|
42
|
+ ec.run()
|
|
43
|
+ #
|
|
44
|
+ while True:
|
|
45
|
+ time.sleep(30)
|
|
46
|
+ try:
|
|
47
|
+ ec.join()
|
|
48
|
+ finally:
|
|
49
|
+ ec.stop()
|