diff --git a/devices/clients/comms_client.py b/devices/clients/comms_client.py index 6d411c5fa6a4f294530466ba9ac8d395a6de44e9..29ae89d6feec9221bc015948106c396925afb5a6 100644 --- a/devices/clients/comms_client.py +++ b/devices/clients/comms_client.py @@ -62,9 +62,10 @@ class CommClient(AbstractCommClient, Thread): def connect(self): """ Function used to connect to the client. + + Throws an Exception if the connection cannot be established. """ self.connected = True - return True def disconnect(self): """ @@ -73,22 +74,15 @@ class CommClient(AbstractCommClient, Thread): self.connected = False def run(self): - - # Explicitly connect - if not self.connect(): - # hardware or infra is down -- needs fixing first - self.fault_func() - return - self.stopping = False while not self.stopping: - # keep trying to connect if not self.connected: - if self.connect(): - pass - else: - # we retry only once, to catch exotic network issues. if the infra or hardware is down, - # our device cannot help, and must be reinitialised after the infra or hardware is fixed. + # we (re)try only once, to catch exotic network issues. if the infra or hardware is down, + # our device cannot help, and must be reinitialised after the infra or hardware is fixed. + try: + self.connect() + except Exception as e: + self.streams.error_stream("Fault condition in communication detected.", e) self.fault_func() return diff --git a/devices/clients/opcua_client.py b/devices/clients/opcua_client.py index 2f9978e76187e9f9cdb028dac0f4c444bad6ea9a..90d008f21701dda524c52cd9af940ac2a5487164 100644 --- a/devices/clients/opcua_client.py +++ b/devices/clients/opcua_client.py @@ -59,7 +59,6 @@ class OPCUAConnection(AsyncCommClient): try: await self.client.connect() - self.connected = True except (socket.error, IOError, OSError) as e: raise IOError(f"Could not connect to OPC-UA server {self._servername()}") from e diff --git a/devices/test/clients/test_client.py b/devices/test/clients/test_client.py index b577cf732fde7de2455e1590fdfdabd57ba0b323..4cdcc980217785e7b7e4352bba205586fefe77d5 100644 --- a/devices/test/clients/test_client.py +++ b/devices/test/clients/test_client.py @@ -24,10 +24,7 @@ class test_client(CommClient): super().__init__(fault_func, try_interval) # Explicitly connect - if not self.connect(): - # hardware or infra is down -- needs fixing first - fault_func() - return + self.connect() def connect(self): """ @@ -36,7 +33,6 @@ class test_client(CommClient): logger.debug("the example client doesn't actually connect to anything silly") self.connected = True # set connected to true - return True # if succesfull, return true. otherwise return false def disconnect(self): self.connected = False # always force a reconnect, regardless of a successful disconnect