Skip to content

Commit

Permalink
Fix MQTT controller reauth error 5
Browse files Browse the repository at this point in the history
  • Loading branch information
enesbcs committed Jan 14, 2019
1 parent a504023 commit a0f9951
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def loadcontrollers():
settingjson = f.read()
Controllers = jsonpickle.decode(settingjson)
except Exception as e:
print("Critical Jsonpickle error:",str(e))
# print("Critical Jsonpickle error:",str(e))
success = 0
return success

Expand Down
18 changes: 15 additions & 3 deletions _C002_DomoMQTT.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(self,controllerindex):
self.authmode = 0
self.certfile = ""
self.laststatus = -1
self.pwset = False

def controller_init(self,enablecontroller=None):
if enablecontroller != None:
Expand All @@ -51,6 +52,9 @@ def controller_init(self,enablecontroller=None):
self.mqttclient.controllercb = self.on_message
self.mqttclient.connectcb = self.on_connect
self.mqttclient.disconnectcb = self.on_disconnect
if self.controllerpassword=="*****":
self.controllerpassword=""
self.pwset=False
self.initialized = True
if self.enabled:
self.connect()
Expand All @@ -64,8 +68,9 @@ def connect(self):
self.disconnect()
self.connectinprogress = 1
self.lastreconnect = time.time()
if self.controlleruser!="" or self.controllerpassword!="":
if (self.controlleruser!="" or self.controllerpassword!="") and (self.pwset==False):
self.mqttclient.username_pw_set(self.controlleruser,self.controllerpassword)
self.pwset=True
try:
am = self.authmode
except:
Expand Down Expand Up @@ -101,7 +106,7 @@ def connect(self):
except:
pass
try:
self.mqttclient.connect_async(self.controllerip,int(self.controllerport))
self.mqttclient.connect(self.controllerip,int(self.controllerport))
self.mqttclient.loop_start()
except:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT controller: "+self.controllerip+":"+str(self.controllerport)+" connection failed")
Expand Down Expand Up @@ -286,13 +291,20 @@ class DMQTTClient(mqtt.Client):
connectcb = None

def on_connect(self, client, userdata, flags, rc):
try:
rc = int(rc)
except:
rc=-1
if rc==0:
self.subscribe(self.subscribechannel,0)
self.connected = True
if self.connectcb is not None:
self.connectcb()
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT connection error: "+str(rc))
estr = str(rc)
if rc==5:
estr += " Invalid user/pass!"
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT connection error: "+estr)

def on_disconnect(self, client, userdata, rc):
self.connected = False
Expand Down
18 changes: 15 additions & 3 deletions _C014_GenMQTT.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self,controllerindex):
self.authmode = 0
self.certfile = ""
self.laststatus = -1
self.pwset = False

def controller_init(self,enablecontroller=None):
if enablecontroller != None:
Expand All @@ -64,6 +65,9 @@ def controller_init(self,enablecontroller=None):
self.mqttclient.controllercb = self.on_message
self.mqttclient.connectcb = self.on_connect
self.mqttclient.disconnectcb = self.on_disconnect
if self.controllerpassword=="*****":
self.controllerpassword=""
self.pwset = False
self.initialized = True
if self.enabled:
self.connect()
Expand All @@ -77,8 +81,9 @@ def connect(self):
self.disconnect()
self.connectinprogress = 1
self.lastreconnect = time.time()
if self.controlleruser!="" or self.controllerpassword!="":
if (self.controlleruser!="" or self.controllerpassword!="") and (self.pwset==False):
self.mqttclient.username_pw_set(self.controlleruser,self.controllerpassword)
self.pwset=True
try:
am = self.authmode
except:
Expand Down Expand Up @@ -114,7 +119,7 @@ def connect(self):
except:
pass
try:
self.mqttclient.connect_async(self.controllerip,int(self.controllerport))
self.mqttclient.connect(self.controllerip,int(self.controllerport)) # connect_async() is faster but maybe not the best for user/pass method
self.mqttclient.loop_start()
except:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT controller: "+self.controllerip+":"+str(self.controllerport)+" connection failed")
Expand Down Expand Up @@ -242,13 +247,20 @@ class GMQTTClient(mqtt.Client):
connectcb = None

def on_connect(self, client, userdata, flags, rc):
try:
rc = int(rc)
except:
rc=-1
if rc==0:
self.subscribe(self.subscribechannel,0)
self.connected = True
if self.connectcb is not None:
self.connectcb()
else:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT connection error: "+str(rc))
estr = str(rc)
if rc==5:
estr += " Invalid user/pass!"
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"MQTT connection error: "+estr)

def on_disconnect(self, client, userdata, rc):
self.connected = False
Expand Down
54 changes: 36 additions & 18 deletions _P510_ITag.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ def webform_save(self,params): # process settings post reply

def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
if self.enabled:
self.set_value(1,0,False)
self.conninprogress = False
self.decimals[0]=0
self.decimals[1]=0
if self.enabled:
if (self.connected): # check status at startup
self.isconnected()
if (self.connected):
self.conninprogress = False
self.set_value(1,0,False)
self.set_value(2,self.connected,True) # advertise status at startup
if (self.connected == False and self.enabled): # connect if not connected
self.handshake = False
Expand All @@ -81,6 +84,8 @@ def plugin_init(self,enableplugin=None):
self.cproc = threading.Thread(target=self.connectproc)
self.cproc.daemon = True
self.cproc.start()
else:
self.__del__()

def connectproc(self):
prevstate = self.connected
Expand All @@ -90,17 +95,18 @@ def connectproc(self):
self.BLEPeripheral = btle.Peripheral(str(self.taskdevicepluginconfig[0]))
self.connected = True
self.afterconnection()
except Exception as e:
except:
self.setdisconnectstate() # disconnected!
# print(e) # DEBUG
self.conninprogress = False
self.isconnected()
publishchange = (self.connected != prevstate)
if self.connected:
self.set_value(2,self.connected,publishchange)
else:
self.set_value(1,0,False)
self.set_value(2,0,publishchange,suserssi=-100,susebattery=0)
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,"BLE connection failed "+str(self.taskdevicepluginconfig[0]))
return False
if self.connected and self.handshake:
misc.addLog(rpieGlobals.LOG_LEVEL_INFO,"BLE connected to "+str(self.taskdevicepluginconfig[0]))
self.waitnotifications = True
Expand All @@ -113,8 +119,7 @@ def connectproc(self):
self.setdisconnectstate(False) # disconnected!

def reconnect(self,tid):
if self.conninprogress==False:
if self.isconnected()==False:
if self.enabled and self.conninprogress==False and self.isconnected()==False:
# self.connected = False
if len(self.taskdevicepluginconfig[0])>10:
self.cproc = threading.Thread(target=self.connectproc)
Expand All @@ -126,12 +131,12 @@ def isconnected(self):
try:
namechar = self.BLEPeripheral.getServiceByUUID(self.ITAG_UUID_SVC_GENERIC).getCharacteristics(self.ITAG_UUID_NAME)[0]
self.connected = True
except Exception as e:
except:
self.connected = False
return self.connected

def afterconnection(self):
if self.connected:
if self.connected and self.enabled:
if self.handshake==False:
compat = False
name = ""
Expand Down Expand Up @@ -165,25 +170,38 @@ def setdisconnectstate(self,tryreconn=True):
self.connected = False
self.set_value(1,0,False)
self.set_value(2,0,True,suserssi=-100,susebattery=0)
if tryreconn:
if tryreconn and self.enabled:
rpieTime.addsystemtimer(int(self.taskdevicepluginconfig[1]),self.reconnect,[-1])

def callbackfunc(self,data="",data2=None):
battery = self.report_battery()
self.set_value(1,(1-int(self.uservar[0])),True,susebattery=battery)
if self.enabled:
battery = self.report_battery()
aval = self.uservar[0]
try:
aval = float(aval)
except:
aval = 0
if aval==0:
aval=1
else:
aval=0
if float(self.uservar[1])!=1:
self.set_value(2,1,False)
self.set_value(1,aval,True,susebattery=battery)

def __del__(self):
self.waitnotifications = False
try:
if self.BLEPeripheral:
self.BLEPeripheral.disconnect()
if self.cproc:
self.cproc._stop()
self.BLEPeripheral.disconnect()
self.cproc._stop()
except:
pass

def plugin_exit(self):
self.__del__()
try:
self.__del__()
except:
pass
return True

# def __exit__(self,type,value,traceback):
Expand All @@ -201,7 +219,7 @@ def report_battery(self): # imlemented but as i tested device always returns 99%
except:
battery = 255
self.setdisconnectstate()
return int(battery)
return float(battery)

class BLEEventHandler(btle.DefaultDelegate):
def __init__(self,keypressed_callback,KPHANDLE):
Expand Down
2 changes: 1 addition & 1 deletion rpieGlobals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
PROGNAME = "RPIEasy"
BUILD = 19011
BUILD = 19014
PROGVER = "0."+str(BUILD/1000)

gpMenu = []
Expand Down
2 changes: 1 addition & 1 deletion webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ def handle_tools(self):
TXBuffer += '"'
TXBuffer += " href='/?cmd=exit'>Exit</a>"
TXBuffer += "<TD>"
TXBuffer += "Exit from RPIEasy"
TXBuffer += "Exit from RPIEasy (or Restart if autostart script used)"

html_TR_TD_height(30)
addWideButton("log", "Log", "")
Expand Down

0 comments on commit a0f9951

Please sign in to comment.