Controls HiFi-Devices bia IR by MQTT-Commands
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import config
  2. import json
  3. import lirc
  4. import logging
  5. import mqtt
  6. import report
  7. import socket
  8. import time
  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. class remote_control(mqtt.mqtt_client):
  15. def __init__(self):
  16. self.__lirc_client__ = None
  17. self.__lirc_init__()
  18. #
  19. self.__start_data__ = None
  20. #
  21. mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
  22. #
  23. for remote in config.SUPPORTED_REMOTES:
  24. for command in config.SUPPORTED_REMOTES[remote]:
  25. topic = config.MQTT_TOPIC + "/" + remote + "/" + command
  26. self.add_callback(topic, self.mqtt_to_lirc)
  27. def __lirc_init__(self):
  28. if self.__lirc_client__ is not None:
  29. logger.info("Closing crashed lirc client instance")
  30. self.__lirc_client__.close()
  31. logger.info("Initiating lirc connection")
  32. self.__lirc_client__ = lirc.Client()
  33. def mqtt_to_lirc(self, client, userdata, message):
  34. try:
  35. payload = json.loads(message.payload)
  36. except json.decoder.JSONDecodeError:
  37. payload = None
  38. #
  39. remote = message.topic.split("/")[-2]
  40. command = message.topic.split("/")[-1]
  41. #
  42. if payload is None:
  43. if self.__start_data__ is not None:
  44. self.__send_stop__(*self.__start_data__)
  45. self.__start_data__ = None
  46. self.__send_once__(remote, command)
  47. elif payload is True:
  48. if self.__start_data__ is not None:
  49. self.__send_stop__(*self.__start_data__)
  50. self.__start_data__ = None
  51. self.__start_data__ = remote, command
  52. self.__send_start__(remote, command)
  53. elif payload is False:
  54. if self.__start_data__ is None:
  55. self.__send_stop__(remote, command)
  56. else:
  57. self.__send_stop__(*self.__start_data__)
  58. self.__start_data__ = None
  59. def __send_once__(self, remote, command):
  60. try:
  61. self.__lirc_client__.send_once(remote, command)
  62. except (TimeoutError, socket.timeout) as e:
  63. self.__lirc_timeout_error__()
  64. else:
  65. logger.info("Sending once: %s to %s.", command, remote)
  66. def __send_start__(self, remote, command):
  67. try:
  68. self.__lirc_client__.send_start(remote, command)
  69. except (TimeoutError, socket.timeout) as e:
  70. self.__lirc_timeout_error__()
  71. else:
  72. logger.info("Sending start: %s to %s.", command, remote)
  73. def __send_stop__(self, remote, command):
  74. try:
  75. self.__lirc_client__.send_stop(remote, command)
  76. except (TimeoutError, socket.timeout) as e:
  77. self.__lirc_timeout_error__()
  78. else:
  79. logger.info("Sending stop: %s to %s.", command, remote)
  80. def __lirc_timeout_error__(self):
  81. logger.error("Timeout-Error while sending IR-Command.")
  82. self.__lirc_init__()
  83. if __name__ == '__main__':
  84. report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
  85. #
  86. rc = remote_control()
  87. #
  88. while (True):
  89. time.sleep(30)