fix(controller): cleanup and fix logic for enable/off dependency.

This commit is contained in:
2025-12-09 16:32:44 +01:00
committed by Christian Appel
parent 69bfabce35
commit ef9568cb46
3 changed files with 13 additions and 9 deletions

View File

@@ -200,7 +200,7 @@ class Controller(OphydObject):
if axis is not None if axis is not None
) )
if all_disabled: if all_disabled:
self.off() self.off(update_config=False)
def set_all_devices_enable(self, enabled: bool) -> None: def set_all_devices_enable(self, enabled: bool) -> None:
""" """
@@ -281,14 +281,15 @@ class Controller(OphydObject):
else: else:
logger.info("The connection has already been established.") logger.info("The connection has already been established.")
def off(self) -> None: def off(self, update_config: bool = True) -> None:
"""Close the socket connection to the controller""" """Close the socket connection to the controller"""
if self.connected and self.sock is not None: if self.connected and self.sock is not None:
self.sock.close() self.sock.close()
self.connected = False self.connected = False
self.sock = None self.sock = None
# Disable all axes associated with this controller if update_config:
self.set_all_devices_enable(False) # Disable all axes associated with this controller
self.set_all_devices_enable(False)
else: else:
logger.info("The connection is already closed.") logger.info("The connection is already closed.")

View File

@@ -187,7 +187,7 @@ class SocketIO:
Args: Args:
timeout (int): Time in seconds to wait for connection timeout (int): Time in seconds to wait for connection
""" """
logger.info(f"Connecting to {self.host}:{self.port}") logger.info(f"Connecting to {self.host}:{self.port}.")
start_time = time.time() start_time = time.time()
while time.time() - start_time < timeout: while time.time() - start_time < timeout:
try: try:
@@ -197,11 +197,14 @@ class SocketIO:
break break
except Exception as exc: except Exception as exc:
self.sock = None self.sock = None
logger.warning(f"Connection failed, retrying after 0.2 seconds... {exc}") logger.warning(
f"Connection to {self.host}:{self.port} failed after {time.time()-start_time:.2f} seconds"
f" with exception: {exc}. Retrying after 1 second..."
)
time.sleep(1) time.sleep(1)
else: else:
raise ConnectionError( raise ConnectionError(
f"Could not connect to {self.host}:{self.port} within {time.time()-start_time} seconds" f"Could not connect to {self.host}:{self.port} within {time.time()-start_time:.2f} seconds"
) )
def _put(self, msg_bytes): def _put(self, msg_bytes):
@@ -224,7 +227,7 @@ class SocketIO:
return self._recv(buffer_length=buffer_length) return self._recv(buffer_length=buffer_length)
def open(self, timeout: int = 10): def open(self, timeout: int = 10):
""" " """
Open the socket connection to the host:port Open the socket connection to the host:port
Args: Args:

View File

@@ -77,7 +77,7 @@ def test_socket_open_with_timeout():
mock_connect.side_effect = Exception("Connection failed") mock_connect.side_effect = Exception("Connection failed")
with pytest.raises(ConnectionError): with pytest.raises(ConnectionError):
socketio.open(timeout=0.4) socketio.open(timeout=0.4)
mock_connect.assert_called_once() mock_connect.assert_called_once()
def test_close(): def test_close():