Browse Source

Module mqtt implemented

master
Dirk Alders 2 years ago
parent
commit
5140a7c51f
3 changed files with 13 additions and 20 deletions
  1. 3
    0
      .gitmodules
  2. 9
    20
      mpd_monitor.py
  3. 1
    0
      mqtt

+ 3
- 0
.gitmodules View File

1
 [submodule "report"]
1
 [submodule "report"]
2
 	path = report
2
 	path = report
3
 	url = https://git.mount-mockery.de/pylib/report.git
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 View File

1
 import config
1
 import config
2
 import logging
2
 import logging
3
-import paho.mqtt.client as paho
3
+import mqtt
4
 import report
4
 import report
5
 import socket
5
 import socket
6
 import subprocess
6
 import subprocess
14
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('librespot')
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
 class mpc(object):
19
 class mpc(object):
18
     PLAYING_TXT = 'playing'
20
     PLAYING_TXT = 'playing'
19
 
21
 
51
 
53
 
52
 
54
 
53
 def send_state_msg_mqtt(state):
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
 def send_title_msg_mqtt(title):
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
 if __name__ == '__main__': 
67
 if __name__ == '__main__': 
79
     
69
     
80
     mpd = mpc(send_state_msg_mqtt, send_title_msg_mqtt)
70
     mpd = mpc(send_state_msg_mqtt, send_title_msg_mqtt)
81
     mpd.run()
71
     mpd.run()
82
-import paho.mqtt.client as paho

+ 1
- 0
mqtt

1
+Subproject commit cf97fa066cdff0e2f7eda0ff4d3c8c0f59c3f2ec

Loading…
Cancel
Save