Explorar el Código

MQTT auth and improvements implemented

master
Dirk Alders hace 2 años
padre
commit
30e68a738d
Se han modificado 6 ficheros con 84 adiciones y 0 borrados
  1. 5
    0
      .gitignore
  2. 3
    0
      .gitmodules
  3. 22
    0
      config_example/config.py
  4. 52
    0
      mpd.py
  5. 1
    0
      report
  6. 1
    0
      requirements.txt

+ 5
- 0
.gitignore Ver fichero

@@ -1,3 +1,8 @@
1
+# ---> mpd
2
+#
3
+config.py
4
+*.err
5
+
1 6
 # ---> Python
2 7
 # Byte-compiled / optimized / DLL files
3 8
 __pycache__/

+ 3
- 0
.gitmodules Ver fichero

@@ -0,0 +1,3 @@
1
+[submodule "report"]
2
+	path = report
3
+	url = https://git.mount-mockery.de/pylib/report.git

+ 22
- 0
config_example/config.py Ver fichero

@@ -0,0 +1,22 @@
1
+#!/usr/bin/env python
2
+# -*- coding: UTF-8 -*-
3
+import os
4
+import report
5
+
6
+MQTT_USER = "user"
7
+MQTT_PASS = "pass"
8
+MQTT_SERVER = "localhost"
9
+MQTT_TOPIC = "hifi/mpd"
10
+
11
+#
12
+# Logging
13
+#
14
+__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
15
+APP_NAME = "mpd"
16
+LOGTARGET = 'logfile'   # possible choices are: 'logfile' or 'stdout'
17
+LOGLVL = 'DEBUG'
18
+
19
+LOGHOST = 'cutelog'
20
+LOGPORT = 19996
21
+
22
+formatter = report.LONG_FMT

+ 52
- 0
mpd.py Ver fichero

@@ -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

+ 1
- 0
report

@@ -0,0 +1 @@
1
+Subproject commit 21bac82e0c459ebf6d34783c9249526a657a6bbd

+ 1
- 0
requirements.txt Ver fichero

@@ -0,0 +1 @@
1
+paho-mqtt

Loading…
Cancelar
Guardar