Pārlūkot izejas kodu

Exception handling for MQTT

master
Dirk Alders 2 gadus atpakaļ
vecāks
revīzija
ee201df901
1 mainītis faili ar 15 papildinājumiem un 8 dzēšanām
  1. 15
    8
      mpd_monitor.py

+ 15
- 8
mpd_monitor.py Parādīt failu

@@ -52,18 +52,25 @@ class mpc(object):
52 52
 def send_state_msg_mqtt(state):
53 53
     client= paho.Client("mpd")
54 54
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
55
-    client.connect(config.MQTT_SERVER, 1883)
56
-    topic = config.MQTT_TOPIC + "/state"
57
-    logger.info("Sending MPD status information to mqtt %s = %s", topic, str(state))
58
-    client.publish(topic, "true" if state else "false")
55
+    try:
56
+        client.connect(config.MQTT_SERVER, 1883)
57
+        topic = config.MQTT_TOPIC + "/state"
58
+        logger.info("Sending MPD status information to mqtt %s = %s", topic, str(state))
59
+        client.publish(topic, "true" if state else "false")
60
+    except (socket.timeout, OSError) as e:
61
+        logger.warning("Erro while sending mpd state information")
62
+
59 63
 
60 64
 def send_title_msg_mqtt(title):
61 65
     client= paho.Client("mpd")
62 66
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
63
-    client.connect(config.MQTT_SERVER, 1883)
64
-    topic = config.MQTT_TOPIC + "/title"
65
-    logger.info("Sending MPD title information to mqtt %s = \"%s\"", topic, title)
66
-    client.publish(topic, title)
67
+    try:
68
+        client.connect(config.MQTT_SERVER, 1883)
69
+        topic = config.MQTT_TOPIC + "/title"
70
+        logger.info("Sending MPD title information to mqtt %s = \"%s\"", topic, title)
71
+        client.publish(topic, title)
72
+    except (socket.timeout, OSError) as e:
73
+        logger.warning("Erro while sending mpd title information")
67 74
 
68 75
 
69 76
 if __name__ == '__main__': 

Notiek ielāde…
Atcelt
Saglabāt