bug msg disconnect fix

This commit is contained in:
tixi
2018-10-03 15:24:16 +02:00
parent a1feddacc4
commit 39d5b0905c

View File

@@ -26,7 +26,7 @@
######################################################################################## ########################################################################################
""" """
<plugin key="tixi_tuya_smartplug_plugin" name="Tuya SmartPlug" author="tixi" version="2.0.0" externallink=" https://github.com/tixi/Domoticz-Tuya-SmartPlug-Plugin"> <plugin key="tixi_tuya_smartplug_plugin" name="Tuya SmartPlug" author="tixi" version="2.0.1" externallink=" https://github.com/tixi/Domoticz-Tuya-SmartPlug-Plugin">
<params> <params>
<param field="Address" label="IP address" width="200px" required="true"/> <param field="Address" label="IP address" width="200px" required="true"/>
<param field="Mode1" label="DevID" width="200px" required="true"/> <param field="Mode1" label="DevID" width="200px" required="true"/>
@@ -49,15 +49,16 @@ class BasePlugin:
__UNIT = 1 __UNIT = 1
__HB_BASE_FREQ = 2 __HB_BASE_FREQ = 2
__VALID_CMD = ('status','On','Off')
def __init__(self): def __init__(self):
self.__address = None #ip address of the smartplug self.__address = None #IP address of the smartplug
self.__devID = None #devID of the smartplug self.__devID = None #devID of the smartplug
self.__localKey = None #localKey of the smartplug self.__localKey = None #localKey of the smartplug
self.__device = None #pytuya object of the smartplug self.__device = None #pytuya object of the smartplug
self.__runAgain = self.__HB_BASE_FREQ #heartbeat frequency (20 seconds) self.__runAgain = self.__HB_BASE_FREQ #heartbeat frequency (20 seconds)
self.__connection = None #connection to the tuya plug self.__connection = None #connection to the tuya plug
self.__last_cmd = None #last command (None/"On"/"Off") self.__last_cmd = None #last command (None/'On'/'Off'/'status')
return return
@@ -96,6 +97,7 @@ class BasePlugin:
if(self.__last_cmd != None): if(self.__last_cmd != None):
self.__command_to_execute(self.__last_cmd) self.__command_to_execute(self.__last_cmd)
else: else:
if(self.__connection.Connected()):
self.__connection.Disconnect() self.__connection.Disconnect()
self.__connection.Connect() self.__connection.Connect()
@@ -161,6 +163,10 @@ class BasePlugin:
def __command_to_execute(self,Command): def __command_to_execute(self,Command):
if(Command not in self.__VALID_CMD):
Domoticz.Error("Undefined command: " + Command)
return
if(Command == 'status'): if(Command == 'status'):
if(self.__last_cmd == None): if(self.__last_cmd == None):
self.__last_cmd = Command self.__last_cmd = Command
@@ -176,12 +182,8 @@ class BasePlugin:
payload = self.__device.generate_payload('set', {'1':False}) payload = self.__device.generate_payload('set', {'1':False})
self.__connection.Send(payload) self.__connection.Send(payload)
status_request = True status_request = True
elif(Command == 'status'): else: #(Command == 'status')
status_request = True status_request = True
else:
Domoticz.Error("Unknow Command received")
self.__last_cmd = None
status_request = False
if(status_request): if(status_request):
payload=self.__device.generate_payload('status') payload=self.__device.generate_payload('status')
@@ -195,9 +197,7 @@ class BasePlugin:
def onDisconnect(self, Connection): def onDisconnect(self, Connection):
Domoticz.Error("Disconnected from: "+Connection.Address+":"+Connection.Port) Domoticz.Debug("Disconnected from: "+Connection.Address+":"+Connection.Port)
#if (Connection == self.__connection):
#self.__connection.Connect()
def onHeartbeat(self): def onHeartbeat(self):
self.__runAgain -= 1 self.__runAgain -= 1
@@ -209,6 +209,7 @@ class BasePlugin:
def onStop(self): def onStop(self):
self.__device = None self.__device = None
self.__last_cmd = None self.__last_cmd = None
if(self.__connection.Connected()):
self.__connection.Disconnect() self.__connection.Disconnect()
self.__connection = None self.__connection = None