|
@@ -0,0 +1,52 @@
|
|
1
|
+import config
|
|
2
|
+import logging
|
|
3
|
+import paho.mqtt.client as paho
|
|
4
|
+import report
|
|
5
|
+import subprocess
|
|
6
|
+import time
|
|
7
|
+
|
|
8
|
+
|
|
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('librespot')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+class mpc(object):
|
|
17
|
+ PLAYING_TXT = 'playing'
|
|
18
|
+
|
|
19
|
+ def __init__(self, state_callback):
|
|
20
|
+ logger.info("Starting MPD monitor...")
|
|
21
|
+ self.__state_callback__ = state_callback
|
|
22
|
+ self.__state__ = None
|
|
23
|
+ self.set_state(False)
|
|
24
|
+
|
|
25
|
+ def run(self):
|
|
26
|
+ while True:
|
|
27
|
+ output = subprocess.check_output(["mpc", "status"])
|
|
28
|
+ out_txt = output.decode('UTF-8')
|
|
29
|
+ self.set_state(self.PLAYING_TXT in out_txt)
|
|
30
|
+ time.sleep(.2)
|
|
31
|
+
|
|
32
|
+ def set_state(self, target_state):
|
|
33
|
+ if self.__state__ != target_state:
|
|
34
|
+ self.__state__ = target_state
|
|
35
|
+ logger.info('MPD state changed to %s', self.__state__)
|
|
36
|
+ self.__state_callback__(self.__state__)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+def send_msg_mqtt(state):
|
|
40
|
+ client= paho.Client("mpd")
|
|
41
|
+ client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
|
|
42
|
+ 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")
|
|
45
|
+
|
|
46
|
+
|
|
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
|
+ mpd = mpc(send_msg_mqtt)
|
|
51
|
+ mpd.run()
|
|
52
|
+import paho.mqtt.client as paho
|