add some error handling to mqtt relay

This commit is contained in:
Blake Blackshear 2021-02-13 11:34:20 -06:00
parent 57ced2c284
commit b6572b7272

View File

@ -39,25 +39,39 @@ class MqttBackend():
self.clients.append(client) self.clients.append(client)
def publish(self, message): def publish(self, message):
json_message = json.loads(message) try:
self.mqtt_client.publish(f"{self.topic_prefix}/{json_message['topic']}", json_message['payload'], retain=json_message['retain']) json_message = json.loads(message)
json_message = {
'topic': f"{self.topic_prefix}/{json_message['topic']}",
'payload': json_message.get['payload'],
'retain': json_message.get('retain', False)
}
except:
logger.warning("Unable to parse websocket message as valid json.")
return
logger.debug(f"Publishing mqtt message from websockets at {json_message['topic']}.")
self.mqtt_client.publish(json_message['topic'], json_message['payload'], retain=json_message['retain'])
def run(self): def run(self):
def send(client, userdata, message): def send(client, userdata, message):
"""Sends mqtt messages to clients.""" """Sends mqtt messages to clients."""
try: try:
logger.debug(f"Received mqtt message on {message.topic}.")
ws_message = json.dumps({ ws_message = json.dumps({
'topic': message.topic.replace(f"{self.topic_prefix}/",""), 'topic': message.topic.replace(f"{self.topic_prefix}/",""),
'payload': message.payload.decode() 'payload': message.payload.decode()
}) })
except: except:
# if the payload can't be decoded don't relay to clients # if the payload can't be decoded don't relay to clients
logger.debug(f"MQTT payload for {message.topic} wasn't text. Skipping...")
return return
for client in self.clients: for client in self.clients:
try: try:
client.send(ws_message) client.send(ws_message)
except: except:
logger.debug("Removing websocket client due to a closed connection.")
self.clients.remove(client) self.clients.remove(client)
self.mqtt_client.message_callback_add(f"{self.topic_prefix}/#", send) self.mqtt_client.message_callback_add(f"{self.topic_prefix}/#", send)