From 88d9cd4ba5e59706dbcb81f7699e44ab27c24213 Mon Sep 17 00:00:00 2001 From: enesbcs Date: Sun, 18 Oct 2020 21:11:31 +0200 Subject: [PATCH] C014 option reconnect added --- _C014_GenMQTT.py | 24 ++++++++++++++++++++---- rpieGlobals.py | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/_C014_GenMQTT.py b/_C014_GenMQTT.py index c1e1d4e..db8d6e2 100644 --- a/_C014_GenMQTT.py +++ b/_C014_GenMQTT.py @@ -12,7 +12,8 @@ # topic: %sysname%/taskname/valuename/set payload value will be forwarded to it! # (Two way communication) # -# Variables: %sysname% %tskname% %valname% +# Variables: %sysname% %tskname% %valname% %tskid% +# # = %tskname%/%valname% !!! # # Commands can be remotely executed through MQTT with writing to: # topic: %sysname%/cmd with payload: command @@ -158,6 +159,7 @@ def connect(self): self.mqttclient.loop_start() except Exception as e: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT controller: "+self.controllerip+":"+str(self.controllerport)+" connection failed "+str(e)) + self.laststatus = 0 return self.isconnected() def disconnect(self): @@ -182,7 +184,7 @@ def isconnected(self,ForceCheck=True): res = False if self.enabled and self.initialized: if ForceCheck==False: - return self.laststatus + return (self.laststatus==1) if self.mqttclient is not None: tstart = self.outch[:len(self.outch)-1] gtopic = tstart+"status" @@ -242,6 +244,7 @@ def webform_load(self): # create html page for settings webserver.addFormTextBox("Controller lwl topic","c014_lwt",lwt,255) webserver.addFormTextBox("LWT Connect Message","c014_cmsg",lwt1,255) webserver.addFormTextBox("LWT Disconnect Message","c014_dcmsg",lwt2,255) + webserver.addFormCheckBox("Check conn & reconnect if needed at every 30 sec","c014_reconnect",self.timer30s) return True def webform_save(self,params): # process settings post reply @@ -286,12 +289,23 @@ def webform_save(self,params): # process settings post reply self.lwtdisconnmsg = "Offline" if lwt!=self.lwt_topic or lwt1!= self.lwtconnmsg or lwt2!=self.lwtdisconnmsg: pchange = True + if (webserver.arg("c014_reconnect",params)=="on"): + self.timer30s = True + else: + self.timer30s = False if pchange and self.enabled: self.disconnect() time.sleep(0.1) self.connect() return True + def timer_thirty_second(self): + if self.enabled: + if self.isconnected()==False: + misc.addLog(rpieGlobals.LOG_LEVEL_DEBUG,"MQTT: Try to reconnect") + self.connect() + return self.timer30s + def on_message(self, msg): success = False tstart = self.outch[:len(self.outch)-1] @@ -367,7 +381,8 @@ def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,chang vname = Settings.Tasks[tasknum].valuenames[u] if vname != "": if ('%t' in self.inch) or ('%v' in self.inch): - gtopic = self.inch.replace('#','') + gtopic = self.inch.replace('#/','') + gtopic = gtopic.replace('#','') gtopic = gtopic.replace('%tskname%',tname) gtopic = gtopic.replace('%tskid%',str(tasknum+1)) gtopic = gtopic.replace('%valname%',vname) @@ -388,7 +403,8 @@ def senddata(self,idx,sensortype,value,userssi=-1,usebattery=-1,tasknum=-1,chang else: vname = Settings.Tasks[tasknum].valuenames[changedvalue-1] if ('%t' in self.inch) or ('%v' in self.inch): - gtopic = self.inch.replace('#','') + gtopic = self.inch.replace('#/','') + gtopic = gtopic.replace('#','') gtopic = gtopic.replace('%tskname%',tname) gtopic = gtopic.replace('%tskid%',str(tasknum+1)) gtopic = gtopic.replace('%valname%',vname) diff --git a/rpieGlobals.py b/rpieGlobals.py index 07ab8bd..4a529c1 100644 --- a/rpieGlobals.py +++ b/rpieGlobals.py @@ -6,7 +6,7 @@ # Copyright (C) 2018-2020 by Alexander Nagy - https://bitekmindenhol.blog.hu/ # PROGNAME = "RPIEasy" -BUILD = 20291 +BUILD = 20292 PROGVER = str(BUILD)[:1]+"."+str(BUILD)[1:2]+"."+str(BUILD)[2:] gpMenu = []