Bladeren bron

Exception handling for MQTT

master
Dirk Alders 2 jaren geleden
bovenliggende
commit
826bd78926
1 gewijzigde bestanden met toevoegingen van 14 en 9 verwijderingen
  1. 14
    9
      spotify.py

+ 14
- 9
spotify.py Bestand weergeven

@@ -100,19 +100,24 @@ class librespot(object):
100 100
 def send_state_msg_mqtt(state):
101 101
     client= paho.Client("spotify")
102 102
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
103
-    client.connect(config.MQTT_SERVER, 1883)
104
-    topic = config.MQTT_TOPIC + "/state"
105
-    logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
106
-    client.publish(topic, "true" if state else "false")
103
+    try:
104
+        client.connect(config.MQTT_SERVER, 1883)
105
+        topic = config.MQTT_TOPIC + "/state"
106
+        logger.info("Sending Spotify status information to mqtt %s = %s", topic, str(state))
107
+        client.publish(topic, "true" if state else "false")
108
+    except (socket.timeout, OSError) as e:
109
+        logger.warning("Erro while sending state information")
107 110
 
108 111
 def send_title_msg_mqtt(title):
109 112
     client= paho.Client("spotify")
110 113
     client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
111
-    client.connect(config.MQTT_SERVER, 1883)
112
-    topic = config.MQTT_TOPIC + "/title"
113
-    logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
114
-    client.publish(topic, title)
115
-
114
+    try:
115
+        client.connect(config.MQTT_SERVER, 1883)
116
+        topic = config.MQTT_TOPIC + "/title"
117
+        logger.info("Sending Spotify status information to mqtt %s = %s", topic, title)
118
+        client.publish(topic, title)
119
+    except (socket.timeout, OSError) as e:
120
+        logger.warning("Erro while sending title information")
116 121
 
117 122
 
118 123
 if __name__ == '__main__': 

Laden…
Annuleren
Opslaan