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):
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.Fault()
self.opcua_connector.reconnect()
return None
return opcua_error_wrapper
......@@ -93,14 +91,15 @@ class BackgroundConnector(Thread):
self.connected = True
except socket.error as e:
self.debug_stream("Could not connect: %s" % (e,))
finally:
self.connecting = False
def run(self):
while not self.connected and not self.stopping:
self.try_connect()
if not self.connected:
time.sleep(self.try_interval)
try:
while not self.connected and not self.stopping:
self.try_connect()
if not self.connected:
time.sleep(self.try_interval)
finally:
self.connecting = False
def stop(self):
self.stopping = True
......@@ -358,6 +357,7 @@ class RCUSCC(Device):
def init_device(self):
"""Initialises the attributes and properties of the RCUSCC."""
Device.init_device(self)
# PROTECTED REGION ID(RCUSCC.init_device) ENABLED START #
self.set_state(DevState.INIT)
......@@ -400,17 +400,11 @@ class RCUSCC(Device):
self.attribute_mapping["Temperature_R"] = {}
# Set defaults to property values.
try:
# 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()))
# Connect to OPC-UA -- will set ON state on success
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()
raise e
# PROTECTED REGION END # // RCUSCC.init_device
def always_executed_hook(self):
......@@ -438,10 +432,6 @@ class RCUSCC(Device):
# PROTECTED REGION ID(RCUSCC.delete_device) ENABLED START #
self.debug_stream("Shutting down...")
# stop reconnecting before disconnect
self.opcua_connector.stop()
self._disconnect()
self.Off()
self.debug_stream("Shut down. Good bye.")
# PROTECTED REGION END # // RCUSCC.delete_device
......@@ -616,19 +606,12 @@ class RCUSCC(Device):
:return:None
"""
self.set_state(DevState.OFF)
# PROTECTED REGION END # // RCUSCC.Off
@command(
)
@DebugIt()
def Init(self):
# PROTECTED REGION ID(RCUSCC.Init) ENABLED START #
"""
# stop reconnecting before disconnect
self.opcua_connector.stop()
self._disconnect()
:return:None
"""
self.set_state(DevState.INIT)
# PROTECTED REGION END # // RCUSCC.Init
# PROTECTED REGION END # // RCUSCC.Off
@command(
)
......@@ -636,10 +619,17 @@ class RCUSCC(Device):
def Fault(self):
# 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
"""
self.set_state(DevState.FAULT)
# try to reconnect
self.opcua_connector.reconnect()
# PROTECTED REGION END # // RCUSCC..On
@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