Skip to content
Snippets Groups Projects
Commit b2d69266 authored by Jan David Mol's avatar Jan David Mol
Browse files

move disconnect/reconnect calls to state functions (Off/Fault)

parent 2cdb2c04
No related branches found
No related tags found
No related merge requests found
...@@ -62,8 +62,6 @@ def fault_on_opcua_error(func): ...@@ -62,8 +62,6 @@ def fault_on_opcua_error(func):
except Exception as e: except Exception as e:
self.error_stream("Communication with the OPC-UA server %s:%d failed. Reconnecting. Trace: %s" % (self.OPC_Server_Name, self.OPC_Server_Port, traceback.format_exc())) self.error_stream("Communication with the OPC-UA server %s:%d failed. Reconnecting. Trace: %s" % (self.OPC_Server_Name, self.OPC_Server_Port, traceback.format_exc()))
self.Fault() self.Fault()
self.opcua_connector.reconnect()
return None return None
return opcua_error_wrapper return opcua_error_wrapper
...@@ -93,14 +91,15 @@ class BackgroundConnector(Thread): ...@@ -93,14 +91,15 @@ class BackgroundConnector(Thread):
self.connected = True self.connected = True
except socket.error as e: except socket.error as e:
self.debug_stream("Could not connect: %s" % (e,)) self.debug_stream("Could not connect: %s" % (e,))
finally:
self.connecting = False
def run(self): def run(self):
while not self.connected and not self.stopping: try:
self.try_connect() while not self.connected and not self.stopping:
if not self.connected: self.try_connect()
time.sleep(self.try_interval) if not self.connected:
time.sleep(self.try_interval)
finally:
self.connecting = False
def stop(self): def stop(self):
self.stopping = True self.stopping = True
...@@ -358,6 +357,7 @@ class RCUSCC(Device): ...@@ -358,6 +357,7 @@ class RCUSCC(Device):
def init_device(self): def init_device(self):
"""Initialises the attributes and properties of the RCUSCC.""" """Initialises the attributes and properties of the RCUSCC."""
Device.init_device(self) Device.init_device(self)
# PROTECTED REGION ID(RCUSCC.init_device) ENABLED START # # PROTECTED REGION ID(RCUSCC.init_device) ENABLED START #
self.set_state(DevState.INIT) self.set_state(DevState.INIT)
...@@ -400,17 +400,11 @@ class RCUSCC(Device): ...@@ -400,17 +400,11 @@ class RCUSCC(Device):
self.attribute_mapping["Temperature_R"] = {} self.attribute_mapping["Temperature_R"] = {}
# Set defaults to property values. # Connect to OPC-UA -- will set ON state on success
self.opcua_connector = BackgroundReconnector(self._init_opcua, self._disconnect, self.debug_stream)
try: if not self.opcua_connector.wait_connected(5.0): # allow 5 seconds to connect
# Connect to OPC-UA
self.opcua_connector = BackgroundReconnector(self._init_opcua, self._disconnect, self.debug_stream)
if not self.opcua_connector.wait_connected(5.0): # allow 5 seconds to connect
self.Fault()
except Exception as e:
self.error_stream("Connection init to the OPC-UA server %s:%d failed. Trace: %s" % (self.OPC_Server_Name, self.OPC_Server_Port, traceback.format_exc()))
self.Fault() self.Fault()
raise e
# PROTECTED REGION END # // RCUSCC.init_device # PROTECTED REGION END # // RCUSCC.init_device
def always_executed_hook(self): def always_executed_hook(self):
...@@ -438,10 +432,6 @@ class RCUSCC(Device): ...@@ -438,10 +432,6 @@ class RCUSCC(Device):
# PROTECTED REGION ID(RCUSCC.delete_device) ENABLED START # # PROTECTED REGION ID(RCUSCC.delete_device) ENABLED START #
self.debug_stream("Shutting down...") self.debug_stream("Shutting down...")
# stop reconnecting before disconnect
self.opcua_connector.stop()
self._disconnect()
self.Off() self.Off()
self.debug_stream("Shut down. Good bye.") self.debug_stream("Shut down. Good bye.")
# PROTECTED REGION END # // RCUSCC.delete_device # PROTECTED REGION END # // RCUSCC.delete_device
...@@ -616,19 +606,12 @@ class RCUSCC(Device): ...@@ -616,19 +606,12 @@ class RCUSCC(Device):
:return:None :return:None
""" """
self.set_state(DevState.OFF) self.set_state(DevState.OFF)
# PROTECTED REGION END # // RCUSCC.Off
@command( # stop reconnecting before disconnect
) self.opcua_connector.stop()
@DebugIt() self._disconnect()
def Init(self):
# PROTECTED REGION ID(RCUSCC.Init) ENABLED START #
"""
:return:None # PROTECTED REGION END # // RCUSCC.Off
"""
self.set_state(DevState.INIT)
# PROTECTED REGION END # // RCUSCC.Init
@command( @command(
) )
...@@ -636,10 +619,17 @@ class RCUSCC(Device): ...@@ -636,10 +619,17 @@ class RCUSCC(Device):
def Fault(self): def Fault(self):
# PROTECTED REGION ID(RCUSCC.On) ENABLED START # # PROTECTED REGION ID(RCUSCC.On) ENABLED START #
""" """
FAULT state is used to indicate our connection with the OPC-UA server is down.
This device will try to reconnect, and transition to the ON state on success.
:return:None :return:None
""" """
self.set_state(DevState.FAULT) self.set_state(DevState.FAULT)
# try to reconnect
self.opcua_connector.reconnect()
# PROTECTED REGION END # // RCUSCC..On # PROTECTED REGION END # // RCUSCC..On
@command( @command(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment