Explorar el Código

Module mqtt implemented

master
Dirk Alders hace 2 años
padre
commit
5140a7c51f
Se han modificado 3 ficheros con 13 adiciones y 20 borrados
  1. 3
    0
      .gitmodules
  2. 9
    20
      mpd_monitor.py
  3. 1
    0
      mqtt

+ 3
- 0
.gitmodules Ver fichero

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

+ 9
- 20
mpd_monitor.py Ver fichero

@@ -1,6 +1,6 @@
1 1
 import config
2 2
 import logging
3
-import paho.mqtt.client as paho
3
+import mqtt
4 4
 import report
5 5
 import socket
6 6
 import subprocess
@@ -14,6 +14,8 @@ except ImportError:
14 14
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('librespot')
15 15
 
16 16
 
17
+mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
18
+
17 19
 class mpc(object):
18 20
     PLAYING_TXT = 'playing'
19 21
 
@@ -51,27 +53,15 @@ class mpc(object):
51 53
 
52 54
 
53 55
 def send_state_msg_mqtt(state):
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 + "/state"
59
-        logger.info("Sending MPD status information to mqtt %s = %s", topic, str(state))
60
-        client.publish(topic, "true" if state else "false")
61
-    except (socket.timeout, OSError) as e:
62
-        logger.warning("Erro while sending mpd state information")
56
+    topic = config.MQTT_TOPIC + "/state"
57
+    logger.info("Sending MPD status information to mqtt %s = %s", topic, str(state))
58
+    mc.send(topic, "true" if state else "false")
63 59
 
64 60
 
65 61
 def send_title_msg_mqtt(title):
66
-    client= paho.Client(config.APP_NAME)
67
-    client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
68
-    try:
69
-        client.connect(config.MQTT_SERVER, 1883)
70
-        topic = config.MQTT_TOPIC + "/title"
71
-        logger.info("Sending MPD title information to mqtt %s = \"%s\"", topic, title)
72
-        client.publish(topic, title)
73
-    except (socket.timeout, OSError) as e:
74
-        logger.warning("Erro while sending mpd title information")
62
+    topic = config.MQTT_TOPIC + "/title"
63
+    logger.info("Sending MPD title information to mqtt %s = \"%s\"", topic, title)
64
+    mc.send(topic, title)
75 65
 
76 66
 
77 67
 if __name__ == '__main__': 
@@ -79,4 +69,3 @@ if __name__ == '__main__':
79 69
     
80 70
     mpd = mpc(send_state_msg_mqtt, send_title_msg_mqtt)
81 71
     mpd.run()
82
-import paho.mqtt.client as paho

+ 1
- 0
mqtt

@@ -0,0 +1 @@
1
+Subproject commit cf97fa066cdff0e2f7eda0ff4d3c8c0f59c3f2ec

Loading…
Cancelar
Guardar