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

Go back to ON state if we reconnect, stop reconnecting when shutting down,...

Go back to ON state if we reconnect, stop reconnecting when shutting down, wait 5 seconds max in INIT to connect and go to FAULT if not
parent 32d38b11
No related branches found
No related tags found
No related merge requests found
......@@ -69,6 +69,8 @@ class BackgroundConnector(Thread):
self.connecting = True
self.connected = False
self.stopping = False
def try_connect(self):
try:
self.connect_func()
......@@ -79,11 +81,15 @@ class BackgroundConnector(Thread):
self.connecting = False
def run(self):
while not self.connected:
while not self.connected and not self.stopping:
self.try_connect()
if not self.connected:
time.sleep(self.try_interval)
def stop(self):
self.stopping = True
self.join()
class BackgroundReconnector(object):
"""
Keeps a connection attempt going.
......@@ -100,6 +106,9 @@ class BackgroundReconnector(object):
self.connector = BackgroundConnector(self.connect_func, debug_stream=self.debug_stream)
self.connector.start()
def stop(self):
self.connector.stop()
def reconnect(self):
"""
Connect or reconnect to the OPC-UA server.
......@@ -121,17 +130,31 @@ class BackgroundReconnector(object):
self.debug_stream("connecting")
self._connect()
def wait_connected(self):
def is_connected(self):
return wait_connected(0.0)
def wait_connected(self, timeout=None):
"""
Wait until a connection is established.
`timeout' is an optional timeout, in seconds.
Returns True if the connection was established. False if not.
"""
# wait for attempt to finish
self.connector.join()
self.connector.join(timeout)
if self.connector.is_alive():
# timeout happened, so not connected yet
return False
if not self.connector.connected:
# thread finished but somehow didn't connect -> an exception got thrown perhaps?
raise Exception("Could not connect to server")
return True
class RCUSCC(Device):
"""
......@@ -312,6 +335,9 @@ class RCUSCC(Device):
self.debug_stream("Mapping OPC-UA MP/CP to attributes done.")
# we have connected, so are on-line.
self.set_state(DevState.ON)
def init_device(self):
"""Initialises the attributes and properties of the RCUSCC."""
......@@ -363,9 +389,10 @@ class RCUSCC(Device):
try:
# Connect to OPC-UA
self.opcua_connector = BackgroundReconnector(self._init_opcua, self._disconnect, self.debug_stream)
self.opcua_connector.wait_connected()
self.set_state(DevState.ON)
if self.opcua_connector.wait_connected(5.0): # allow 5 seconds to connect
self.set_state(DevState.ON)
else:
self.set_state(DevState.FAULT)
except Exception as e:
self.set_state(DevState.FAULT)
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()))
......@@ -396,8 +423,12 @@ class RCUSCC(Device):
"""
# PROTECTED REGION ID(RCUSCC.delete_device) ENABLED START #
self.debug_stream("Shutting down...")
self.set_state(DevState.OFF)
# stop reconnecting before disconnect
self.opcua_connector.stop()
self._disconnect()
self.set_state(DevState.OFF)
self.debug_stream("Shut down. Good bye.")
# PROTECTED REGION END # // RCUSCC.delete_device
# ------------------
......
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