12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import config
- import logging
- import mqtt
- import os
- import report
- import task
- import time
-
- try:
- from config import APP_NAME as ROOT_LOGGER_NAME
- except ImportError:
- ROOT_LOGGER_NAME = 'root'
- logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
-
-
- class exec_command(mqtt.mqtt_client, task.threaded_queue):
- def __init__(self):
- self.__block_execution__ = False
- task.threaded_queue.__init__(self)
- 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)
-
- def exec_command(self, rt):
- os.system(config.COMMAND)
- self.__block_execution__ = False
-
-
- def mqtt_rx(self, client, userdate, message):
- if not self.__block_execution__:
- if message.payload == config.PAYLOAD:
- logger.debug("Starting execution in background...")
- self.__block_execution__ = True
- self.enqueue(5, self.exec_command)
- else:
- logger.debug("Execution in progress. Ignoring request...")
-
-
- if __name__ == '__main__':
- report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
- #
- ec = exec_command()
- ec.run()
- #
- while True:
- time.sleep(30)
- try:
- ec.join()
- finally:
- ec.stop()
|