Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion umqtt.robust/umqtt/robust.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class MQTTClient(simple.MQTTClient):
DELAY = 2
DEBUG = False

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.subscriptions = []

def delay(self, i):
utime.sleep(self.DELAY)

Expand All @@ -20,7 +24,13 @@ def reconnect(self):
i = 0
while 1:
try:
return super().connect(False)
ret = super().connect(False)
if not ret:
for topic, qos in self.subscriptions:
if self.DEBUG:
print("mqtt resubscribe: %r" % topic)
super().subscribe(topic, qos)
return ret
except OSError as e:
self.log(True, e)
i += 1
Expand All @@ -34,6 +44,16 @@ def publish(self, topic, msg, retain=False, qos=0):
self.log(False, e)
self.reconnect()

def subscribe(self, topic, qos=0):
while 1:
try:
ret = super().subscribe(topic, qos)
self.subscriptions.append((topic, qos))
return ret
except OSError as e:
self.log(False, e)
self.reconnect()

def wait_msg(self):
while 1:
try:
Expand Down