Commit 523f8acc authored by chrissi^'s avatar chrissi^
Browse files

MqttConnector: Improve Error Handling


Signed-off-by: chrissi^'s avatarChris Fiege <chris@tinyhost.de>
parent 99021435
......@@ -34,8 +34,12 @@ class MqttConnector:
self.client.set_callback(self._cb)
except:
print("MQTT: Initial connection to server failed")
self.client = None
def test_conn(self):
if not self.client:
return
try:
print("MQTT: Ping upstream")
self.client.ping()
......@@ -47,22 +51,30 @@ class MqttConnector:
except:
pass
print("MQTT: Trying Reconnect...")
self.client = mqtt.MQTTClient(self.machine_id, self._host)
self.client.connect()
self.client.set_callback(self._cb)
for sub in self._sub_list:
self.client.subscribe(sub)
print("...Success!")
try:
self.client = mqtt.MQTTClient(self.machine_id, self._host)
self.client.connect()
self.client.set_callback(self._cb)
for sub in self._sub_list:
self.client.subscribe(sub)
print("...Success!")
except Exception as e:
print("MQTT reconnect failed: {}".format(e))
self.client = None
def publish(self, topic, value):
self.client.publish(topic, value)
if self.client:
self.client.publish(topic, value)
def subscribe(self, topic):
self._sub_list.append(topic)
self.client.subscribe(topic)
if self.client:
self.client.subscribe(topic)
def check_msg(self):
self.client.check_msg()
if self.client:
self.client.check_msg()
time.sleep_ms(10)
def add_cb(self, cb):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment