From 94ef650f803df60ca75f7edd9cff2b38f1bb933c Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Wed, 26 Nov 2025 00:05:24 +0100 Subject: [PATCH] Fix monitor notifier. Signed-off-by: Pol Henarejos --- picokey/PicoKey.py | 8 ++++++-- picokey/RescueMonitor.py | 13 +++++++------ picokey/RescuePicoKey.py | 4 ++++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/picokey/PicoKey.py b/picokey/PicoKey.py index a619672..88614e1 100644 --- a/picokey/PicoKey.py +++ b/picokey/PicoKey.py @@ -25,6 +25,7 @@ from .RescuePicoKey import RescuePicoKey from .RescueMonitor import RescueMonitor, RescueMonitorObserver from .PhyData import PhyData from .core import NamedIntEnum +import usb.core class Platform(NamedIntEnum): RP2040 = 0 @@ -130,7 +131,7 @@ class PicoKey: def __init__(self, device): self.__device = device - def update(self, actions): + def update(self, actions: tuple[Optional[usb.core.Device], Optional[usb.core.Device]]): (connected, disconnected) = actions if connected: pass @@ -139,7 +140,7 @@ class PicoKey: try: self.__card = RescuePicoKey() self.__connection_type = ConnectionType.RESCUE - self.__observer = PicoRescueObserver(self.__card) + self.__observer = PicoRescueObserver(self) self.__monitor = RescueMonitor(device=self.__card, cls_callback=self.__observer) except Exception: raise Exception('time-out: no card inserted') @@ -169,6 +170,9 @@ class PicoKey: if (not self.__card): return if isinstance(self.__card, RescuePicoKey): + self.__monitor.stop() + self.__monitor = None + self.__observer = None self.__card.close() else: self.__card.disconnect() diff --git a/picokey/RescueMonitor.py b/picokey/RescueMonitor.py index 4ae7eca..5d7b850 100644 --- a/picokey/RescueMonitor.py +++ b/picokey/RescueMonitor.py @@ -17,6 +17,7 @@ */ """ +from typing import Optional import usb.core import threading import time @@ -26,15 +27,15 @@ class RescueMonitorObserver: def __init__(self): pass - def notifyObservers(self, actions): + def notifyObservers(self, actions: tuple[Optional[usb.core.Device], Optional[usb.core.Device]]): func = getattr(self, "update", None) if callable(func): func(actions) - def on_connect(self, device): + def on_connect(self, device: Optional[usb.core.Device]): self.notifyObservers((device, None)) - def on_disconnect(self, device): + def on_disconnect(self, device: Optional[usb.core.Device]): self.notifyObservers((None, device)) class RescueMonitor: @@ -56,8 +57,8 @@ class RescueMonitor: def stop(self): self._running = False - if self._thread: - self._thread.join() + #if self._thread: + # self._thread.join() def _run(self): while self._running: @@ -73,6 +74,6 @@ class RescueMonitor: # Device disconnected self._device_present = False if self._cls_callback: - self._cls_callback.on_disconnect(self._dev) + self._cls_callback.on_disconnect(self._dev.device) time.sleep(self.interval) diff --git a/picokey/RescuePicoKey.py b/picokey/RescuePicoKey.py index 2f3388a..3b66f06 100644 --- a/picokey/RescuePicoKey.py +++ b/picokey/RescuePicoKey.py @@ -86,6 +86,10 @@ class RescuePicoKey: def close(self): if self.__dev: usb.util.dispose_resources(self.__dev) + self.__dev = None + + def has_card(self): + return self.__dev is not None def __exit__(self, exc_type, exc, tb): self.close()