Ver código fonte

currently playing information added to mqtt

master
Dirk Alders 2 anos atrás
pai
commit
b1a57836fb
1 arquivos alterados com 27 adições e 5 exclusões
  1. 27
    5
      mpd.py

+ 27
- 5
mpd.py Ver arquivo

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

Carregando…
Cancelar
Salvar