Execute a command on receiving a mqtt message
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

exec_command.py 1.4KB

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