From 12fa16ea2a86f474c1f79cfb73bab109a2e2225e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Pol=C3=A1=C4=8Dek?= Date: Wed, 18 Sep 2024 22:56:41 +0200 Subject: [PATCH] Catch exceptions The USB detection can fail if SRR dongle is inserted and removed very quickly. --- python/yaroc/sources/si.py | 56 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/python/yaroc/sources/si.py b/python/yaroc/sources/si.py index d86b69f..006bb0e 100644 --- a/python/yaroc/sources/si.py +++ b/python/yaroc/sources/si.py @@ -157,38 +157,38 @@ async def loop(self, queue: Queue[SiPunch], status_queue: Queue[DeviceEvent]): action, parent_device_info = await self._device_queue.get() parent_device_node = parent_device_info[DEVNAME] - if action == "add": - await asyncio.sleep(3.0) # Give the TTY subystem more time - if platform.system().startswith("Linux"): - from pyudev import Context, Device - - context = Context() - parent_device = Device.from_device_file(context, parent_device_node) - lst = list(context.list_devices(subsystem="tty").match_parent(parent_device)) - if len(lst) == 0: - continue - device_node = lst[0].device_node - if device_node in self._udev_workers: - return - elif platform.system().startswith("win"): - device_node = UdevSiFactory.extract_com(parent_device_node) - - logging.info(f"Inserted SportIdent device {device_node}") - - try: + try: + if action == "add": + await asyncio.sleep(3.0) # Give the TTY subystem more time + if platform.system().startswith("Linux"): + from pyudev import Context, Device + + context = Context() + parent_device = Device.from_device_file(context, parent_device_node) + lst = list(context.list_devices(subsystem="tty").match_parent(parent_device)) + if len(lst) == 0: + continue + device_node = lst[0].device_node + if device_node in self._udev_workers: + return + elif platform.system().startswith("win"): + device_node = UdevSiFactory.extract_com(parent_device_node) + + logging.info(f"Inserted SportIdent device {device_node}") + worker = SerialSiWorker(device_node) task = asyncio.create_task(worker.loop(queue)) self._udev_workers[parent_device_node] = (worker, task, device_node) await status_queue.put(DeviceEvent(True, device_node)) - except Exception as e: - logging.error(e) - elif action == "remove": - if parent_device_node in self._udev_workers: - si_worker, _, device_node = self._udev_workers[parent_device_node] - logging.info(f"Removed device {device_node}") - si_worker.close() - del self._udev_workers[parent_device_node] - await status_queue.put(DeviceEvent(False, device_node)) + elif action == "remove": + if parent_device_node in self._udev_workers: + si_worker, _, device_node = self._udev_workers[parent_device_node] + logging.info(f"Removed device {device_node}") + si_worker.close() + del self._udev_workers[parent_device_node] + await status_queue.put(DeviceEvent(False, device_node)) + except Exception as e: + logging.error(e) @staticmethod def _is_silabs(device_info: dict[str, Any]):