Selaa lähdekoodia

Module mqtt implemented

master
Dirk Alders 2 vuotta sitten
vanhempi
commit
a0984df2ab
3 muutettua tiedostoa jossa 13 lisäystä ja 19 poistoa
  1. 3
    0
      .gitmodules
  2. 1
    0
      mqtt
  3. 9
    19
      spotify.py

+ 3
- 0
.gitmodules Näytä tiedosto

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

+ 1
- 0
mqtt

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

+ 9
- 19
spotify.py Näytä tiedosto

@@ -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
@@ -18,6 +18,8 @@ except ImportError:
18 18
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
19 19
 
20 20
 
21
+mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
22
+
21 23
 class librespot(object):
22 24
     ON_CMD = ['play', ]
23 25
     OFF_CMD = ['pause', 'stop', ]
@@ -99,26 +101,14 @@ class librespot(object):
99 101
             
100 102
 
101 103
 def send_state_msg_mqtt(state):
102
-    client= paho.Client(config.APP_NAME)
103
-    client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
104
-    try:
105
-        client.connect(config.MQTT_SERVER, 1883)
106
-        topic = config.MQTT_TOPIC + "/state"
107
-        logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
108
-        client.publish(topic, "true" if state else "false")
109
-    except (socket.timeout, OSError) as e:
110
-        logger.warning("Erro while sending state information")
104
+    topic = config.MQTT_TOPIC + "/state"
105
+    logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
106
+    mc.send(topic, "true" if state else "false")
111 107
 
112 108
 def send_title_msg_mqtt(title):
113
-    client= paho.Client(config.APP_NAME)
114
-    client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
115
-    try:
116
-        client.connect(config.MQTT_SERVER, 1883)
117
-        topic = config.MQTT_TOPIC + "/title"
118
-        logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
119
-        client.publish(topic, title)
120
-    except (socket.timeout, OSError) as e:
121
-        logger.warning("Erro while sending title information")
109
+    topic = config.MQTT_TOPIC + "/title"
110
+    logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
111
+    mc.send(topic, title)
122 112
 
123 113
 
124 114
 if __name__ == '__main__': 

Loading…
Peruuta
Tallenna