umqtt.robust: Initial version of client with auto-reconnect on errors.

pull/82/head
Paul Sokolovsky 2016-07-24 03:26:52 +03:00
rodzic b5fb588144
commit 2fecbd0312
1 zmienionych plików z 43 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,43 @@
import utime
from . import simple
class MQTTClient(simple.MQTTClient):
DELAY = 2
DEBUG = False
def delay(self, i):
utime.sleep(self.DELAY)
def log(self, in_reconnect, exc):
if self.DEBUG:
if in_reconnect:
print("mqtt reconnect: %r" % e)
else:
print("mqtt: %r" % e)
def reconnect(self, i):
i = 0
while 1:
try:
return super().connect(False)
except OSError as e:
self.log(True, e)
i += 1
self.delay(i)
def publish(self, topic, msg, qos=0, retain=False):
while 1:
try:
return super().publish(topic, msg, qos, retain)
except OSError as e:
self.log(False, e)
self.reconnect()
def wait_msg(self):
while 1:
try:
return super().wait_msg()
except OSError as e:
self.log(False, e)
self.reconnect()