From b7a3387c5f3f4eea4fbcd0d8cc5d680142c8a08c Mon Sep 17 00:00:00 2001 From: arofarn Date: Sat, 16 May 2020 09:10:06 +0200 Subject: [PATCH] Add retry limits and "is_connected" property --- code/lib-src/umqtt/robust.py | 71 ++++++++++++++++++++++++----------- code/lib/umqtt/robust.mpy | Bin 595 -> 884 bytes 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/code/lib-src/umqtt/robust.py b/code/lib-src/umqtt/robust.py index 983834f..f648504 100644 --- a/code/lib-src/umqtt/robust.py +++ b/code/lib-src/umqtt/robust.py @@ -6,39 +6,68 @@ class MQTTClient(simple.MQTTClient): DELAY = 2 DEBUG = False + is_connected = False - def delay(self, i): - time.sleep(self.DELAY) - - def log(self, in_reconnect, e): + def log(self, in_reconnect, err): + """Log error if DEBUG is True""" if self.DEBUG: if in_reconnect: - print("mqtt reconnect: %r" % e) + print("mqtt reconnect: %r" % err) else: - print("mqtt: %r" % e) + print("mqtt: %r" % err) def reconnect(self): + """Try to connect or reconnect to MQTT broker""" i = 0 while 1: try: + self.is_connected = True return super().connect(False) - except OSError as e: - self.log(True, e) + except OSError as err: + self.is_connected = False + self.log(True, err) i += 1 - self.delay(i) + time.sleep(self.DELAY) + if i >= 5: + print("Mqtt reconnect : too much retry") + self.is_connected = False + break + def publish(self, topic, msg, retain=False, qos=0): - while 1: - try: - return super().publish(topic, msg, retain, qos) - except OSError as e: - self.log(False, e) - self.reconnect() + """Try to publish if connected to MQTT broker""" + if self.is_connected: + i = 0 + while 1: + try: + return super().publish(topic, msg, retain, qos) + except OSError as e: + i += 1 + self.log(False, e) + if i >= 5: + print("Mqtt publish : too much retry") + self.is_connected = False + break + else: + self.reconnect() + else: + print("Mqtt publish: not connected") def wait_msg(self): - while 1: - try: - return super().wait_msg() - except OSError as e: - self.log(False, e) - self.reconnect() + """Check if any subscribed messages is available""" + if self.is_connected: + i = 0 + while 1: + try: + return super().wait_msg() + except OSError as e: + i += 1 + self.log(False, e) + if i >= 5: + print("Mqtt wait_msg : too much retry") + self.is_connected = False + break + else: + self.reconnect() + else: + print("Mqtt wait_msg: not connected") diff --git a/code/lib/umqtt/robust.mpy b/code/lib/umqtt/robust.mpy index bebc63d13cf604080522b4184e708a38e5617b54..6021a3c0c1680ca73268ce42ad8d695cc3ac0fcc 100644 GIT binary patch literal 884 zcmZXSPfrs;7>D24rGErkw!39)D9B)2SH4hh`7dho;-@Z{0yQYw|xWZris^E~fwash6X)U`wuLSoHcUU%F( z)eX4{R8NLot88La7y2EmTrHUsC<$m7VU%B-0wJE$7Z)FuEVJUqc!{6pG13h{Ta$?~ z6e_(*r`4VCNAZ&c5QD22u)t#&+QPl+<7W@^T5DBaJk@Z*a*BpsshEaqzMTRbEZHj! zf)EyKX1lJ@3GR7gj6*nDU0*I)&T5132n+9)EVo#8RvHHM)MszuP>lAG@pKLvQbZl5 zo*JPdyif7XKhr3$^tCpEP+oK08Iq~_McqIEy9ujO>^v4UYc)q~og$rD>-=`l4^EEt zQHfk9sm$xl&Gh{nat7$(>>w<72}y*#40walbLK2E=I_Bv5hj1Bx9QOw0nJPBUOcw2 zpXKt+`stxK67O|Nrdjo*WESOh{Yd5u^9Tyfou8`UqjL~}+~s~a2u zW9@bAs$~ROO8pFN$*Kg`Y{wb#1=?X=!grDv+cTtMyQK3)w&N^^ltdr+YOnsO4X6dm y(irHdW#OXM5z}f-oxy6Y1iHt4rqv#Ix7L?)E&hU*bInIQ|AChkhP(RzIs66x68~5L delta 437 zcmeyuc9~^Dpn!>lA|rcFf{IIOD|@duOu-uznHk0S3QCi5GK(`dD6lY!36v*hmc-{4r*BARU`utFGe9ut8YVMW$h+3vYuEL$xyV zrXLfZYS%NR77OJTmXs*KZM9NREout6#3Uun-5{c*pd6#A!q9NYLx4euVU{YxMo~VP z>&zIFy|fv^g%}L(yC^jr_7r2-D8e7K*^DW2Q{&S^8$~2HfgBQeSc5s%i;*GLX@5P~ zRjdr1O%AWx7{z2c8{}LRl;e#VJcMC}GjH0wWvdxWvX?y6V6lc{o?JkK*#ou#4Gs)E zrojd_ZKDXc8AGJgL6Dh~qnM=pnsiPviKz-Us3|B%15MY}z5PY5W13z@{D2 OV1k;)4>Ij2+=T#%TzK37