Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

umqtt.robust: Resubscribe to topics after doing reconnect. #186

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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