attempt to fix intermitant startup bug - where the gui crashes as it fails to connect to the lights

This commit is contained in:
John Henry Beale
2026-05-15 14:10:58 +02:00
parent 0aa124b912
commit 960b48f0a4
+33 -19
View File
@@ -56,13 +56,19 @@ class IlluminationControl(object):
def connect(self):
"""creates a socket and connects to illumination controller"""
# connect to XT-PICO
self._socket=s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(1)
try:
s.connect((self._hostname, self._port))
except socket.timeout as e:
_log.error(f"{e} can't connect to: {self._hostname}:{self._port}")
return True
except (socket.timeout, ConnectionRefusedError, OSError) as e:
_log.error(f"can't connect to illumination controller {self._hostname}:{self._port} : {e}")
try:
s.close()
except Exception:
pass
self._socket=None
return False
def disconnect(self):
try:
@@ -76,30 +82,38 @@ class IlluminationControl(object):
return bool(s & led_flags)
def send_command(self, cmd: bytes):
try:
s=self._socket
except AttributeError: #simulated mode
_log.info('simulated mode:{}'.format(cmd))
return
if getattr(self, "_socket", None) is None:
if not self.connect():
return False
try:
self._socket.sendall(cmd)
except (BrokenPipeError, OSError) as e:
_log.warning("reconnecting lights")
self.connect()
return True
except (BrokenPipeError, TimeoutError, OSError) as e:
_log.warning(f"reconnecting lights: {e}")
self.disconnect()
if not self.connect():
return False
try:
self._socket.sendall(cmd)
return True
except Exception as e:
_log.error("unrecoverable socket error: {}".format(e.args))
_log.error(f"unrecoverable socket error: {e}")
self.disconnect()
return False
def status(self):
if getattr(self, "_socket", None) is None:
if not self.connect():
return 0
if not self.send_command(bytes((stat,all))):
return 0
try:
s=self._socket
except AttributeError: #simulated mode
_log.debug('simulated mode')
return self._sim['stat']
self.send_command(bytes((stat,all)))
resp = self._socket.recv(1024)
return resp[0]&0x1f
resp=self._socket.recv(1024)
return resp[0]&0x1f
except Exception as e:
_log.error(f"illumination recv failed: {e}")
self.disconnect()
return 0
def all(self,val):
cmd = on if val else off