Controls HiFi-Devices bia IR by MQTT-Commands
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.

remote_control.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import config
  2. import json
  3. import lirc
  4. import logging
  5. import paho.mqtt.client as 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(object):
  15. def __init__(self):
  16. self.__lirc_client__ = lirc.Client()
  17. self.__client__ = mqtt.Client(config.APP_NAME) # create client object
  18. self.__client__.on_message = self.__receive__ # attach function to callback
  19. self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
  20. try:
  21. self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
  22. self.__client__.loop_start() # start the loop
  23. for remote in config.SUPPORTED_REMOTES:
  24. for command in config.SUPPORTED_REMOTES[remote]:
  25. topic = config.MQTT_TOPIC + "/" + remote + "/" + command
  26. logger.debug("Subscribing \"%s\"", topic)
  27. self.__client__.subscribe(topic) # subscibe a topic
  28. except (socket.timeout, OSError) as e:
  29. logger.warning("Erro while setting up mqtt instance and listener")
  30. def __receive__(self, client, userdata, message):
  31. try:
  32. payload = json.loads(message.payload)
  33. except json.decoder.JSONDecodeError:
  34. payload = None
  35. if payload is None:
  36. remote = message.topic.split("/")[-2]
  37. command = message.topic.split("/")[-1]
  38. try:
  39. self.__lirc_client__.send_once(remote, command)
  40. except TimeoutError:
  41. logger.exception("Timeout-Error while sending IR-Command.")
  42. logger.info("Sending once: %s to %s.", command, remote)
  43. elif payload is True:
  44. logger.warning("Start of remote command NYI!")
  45. elif payload is False:
  46. logger.warning("Stop of remote command NYI!")
  47. if __name__ == '__main__':
  48. report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
  49. #
  50. rc = remote_control()
  51. #
  52. while (True):
  53. time.sleep(30)