Browse Source

Exception handling for MQTT

master
Dirk Alders 2 years ago
parent
commit
ee201df901
1 changed files with 15 additions and 8 deletions
  1. 15
    8
      mpd_monitor.py

+ 15
- 8
mpd_monitor.py View File

52
 def send_state_msg_mqtt(state):
52
 def send_state_msg_mqtt(state):
53
     client= paho.Client("mpd")
53
     client= paho.Client("mpd")
54
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
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
 def send_title_msg_mqtt(title):
64
 def send_title_msg_mqtt(title):
61
     client= paho.Client("mpd")
65
     client= paho.Client("mpd")
62
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
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
 if __name__ == '__main__': 
76
 if __name__ == '__main__': 

Loading…
Cancel
Save