Module mpd_state -> mqtt
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

mpd_monitor.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import config
  2. import logging
  3. import paho.mqtt.client as paho
  4. import report
  5. import socket
  6. import subprocess
  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('librespot')
  13. class mpc(object):
  14. PLAYING_TXT = 'playing'
  15. def __init__(self, state_callback, title_callback):
  16. logger.info("Starting MPD monitor...")
  17. self.__state_callback__ = state_callback
  18. self.__title_callback__ = title_callback
  19. self.__state__ = None
  20. self.__title__ = None
  21. self.set_state(False)
  22. self.set_title("")
  23. def run(self):
  24. while True:
  25. output = subprocess.check_output(["mpc", "status"])
  26. out_txt = output.decode('UTF-8')
  27. self.set_state(self.PLAYING_TXT in out_txt)
  28. if self.__state__:
  29. self.set_title(out_txt.split("\n")[0])
  30. else:
  31. self.set_title("")
  32. time.sleep(.2)
  33. def set_state(self, target_state):
  34. if self.__state__ != target_state:
  35. self.__state__ = target_state
  36. logger.info('MPD state changed to %s', self.__state__)
  37. self.__state_callback__(self.__state__)
  38. def set_title(self, title):
  39. if self.__title__ != title:
  40. logger.info('MPD is playing "%s"', title)
  41. self.__title_callback__(title)
  42. self.__title__ = title
  43. def send_state_msg_mqtt(state):
  44. client= paho.Client(config.APP_NAME)
  45. client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
  46. try:
  47. client.connect(config.MQTT_SERVER, 1883)
  48. topic = config.MQTT_TOPIC + "/state"
  49. logger.info("Sending MPD status information to mqtt %s = %s", topic, str(state))
  50. client.publish(topic, "true" if state else "false")
  51. except (socket.timeout, OSError) as e:
  52. logger.warning("Erro while sending mpd state information")
  53. def send_title_msg_mqtt(title):
  54. client= paho.Client(config.APP_NAME)
  55. client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
  56. try:
  57. client.connect(config.MQTT_SERVER, 1883)
  58. topic = config.MQTT_TOPIC + "/title"
  59. logger.info("Sending MPD title information to mqtt %s = \"%s\"", topic, title)
  60. client.publish(topic, title)
  61. except (socket.timeout, OSError) as e:
  62. logger.warning("Erro while sending mpd title information")
  63. if __name__ == '__main__':
  64. report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
  65. mpd = mpc(send_state_msg_mqtt, send_title_msg_mqtt)
  66. mpd.run()
  67. import paho.mqtt.client as paho