From c7c31fb48d502119989ae04ab806d11616ef46a4 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 27 Feb 2024 00:02:30 +0100 Subject: [PATCH 1/4] Log the canonical name of plugins when starting them. --- platypush/plugins/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/platypush/plugins/__init__.py b/platypush/plugins/__init__.py index a4ceb73c..0fa7d62b 100644 --- a/platypush/plugins/__init__.py +++ b/platypush/plugins/__init__.py @@ -230,7 +230,9 @@ class RunnablePlugin(Plugin): if self.disable_monitor: return - self.logger.info('Starting %s', self.__class__.__name__) + self.logger.info( + 'Starting plugin [%s]', get_plugin_name_by_class(self.__class__) + ) while not self.should_stop(): try: From bbf755eef2b20fe69efc20f27395ed4cfd1a4fb9 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 27 Feb 2024 00:09:19 +0100 Subject: [PATCH 2/4] [#299] Refactored NFC backend into a runnable plugin. --- platypush/backend/nfc/manifest.yaml | 14 -------- .../{backend => plugins}/nfc/__init__.py | 35 ++++++++++++------- platypush/plugins/nfc/manifest.yaml | 12 +++++++ 3 files changed, 34 insertions(+), 27 deletions(-) delete mode 100644 platypush/backend/nfc/manifest.yaml rename platypush/{backend => plugins}/nfc/__init__.py (87%) create mode 100644 platypush/plugins/nfc/manifest.yaml diff --git a/platypush/backend/nfc/manifest.yaml b/platypush/backend/nfc/manifest.yaml deleted file mode 100644 index f8259dfc..00000000 --- a/platypush/backend/nfc/manifest.yaml +++ /dev/null @@ -1,14 +0,0 @@ -manifest: - events: - platypush.message.event.nfc.NFCDeviceConnectedEvent: when an NFC reader/writer - is connected - platypush.message.event.nfc.NFCDeviceDisconnectedEvent: when an NFC reader/writer - is disconnected - platypush.message.event.nfc.NFCTagDetectedEvent: when an NFC tag is detected - platypush.message.event.nfc.NFCTagRemovedEvent: when an NFC tag is removed - install: - pip: - - nfcpy>=1.0 - - ndeflib - package: platypush.backend.nfc - type: backend diff --git a/platypush/backend/nfc/__init__.py b/platypush/plugins/nfc/__init__.py similarity index 87% rename from platypush/backend/nfc/__init__.py rename to platypush/plugins/nfc/__init__.py index e6dc13a5..27c4c8ad 100644 --- a/platypush/backend/nfc/__init__.py +++ b/platypush/plugins/nfc/__init__.py @@ -1,20 +1,21 @@ import base64 import json -from platypush.backend import Backend from platypush.message.event.nfc import ( NFCTagDetectedEvent, NFCTagRemovedEvent, NFCDeviceConnectedEvent, NFCDeviceDisconnectedEvent, ) +from platypush.plugins import RunnablePlugin -class NfcBackend(Backend): +class NfcPlugin(RunnablePlugin): """ - Backend to detect NFC card events from a compatible reader. + Plugin to detect events from NFC devices. - Run the following to check if your device is compatible with nfcpy and the right permissions are set:: + Run the following command to check if your device is compatible with nfcpy + and the right permissions are set:: python -m nfc @@ -40,23 +41,23 @@ class NfcBackend(Backend): if not self._clf: self._clf = nfc.ContactlessFrontend() self._clf.open(self.device_id) - self.bus.post(NFCDeviceConnectedEvent(reader=self._get_device_str())) + assert self._clf and self._clf.device, 'NFC reader not initialized' + self._bus.post(NFCDeviceConnectedEvent(reader=self._get_device_str())) self.logger.info( - 'Initialized NFC reader backend on device {}'.format( - self._get_device_str() - ) + 'Initialized NFC reader on device %s', self._get_device_str() ) return self._clf def _get_device_str(self): + assert self._clf, 'NFC reader not initialized' return str(self._clf.device) def close(self): if self._clf: self._clf.close() self._clf = None - self.bus.post(NFCDeviceDisconnectedEvent(reader=self._get_device_str())) + self._bus.post(NFCDeviceDisconnectedEvent(reader=self._get_device_str())) @staticmethod def _parse_records(tag): @@ -212,7 +213,7 @@ class NfcBackend(Backend): tag_id = self._parse_id(tag) records = self._parse_records(tag) - self.bus.post( + self._bus.post( NFCTagDetectedEvent( reader=self._get_device_str(), tag_id=tag_id, records=records ) @@ -224,14 +225,15 @@ class NfcBackend(Backend): def _on_release(self): def callback(tag): tag_id = self._parse_id(tag) - self.bus.post( + self._bus.post( NFCTagRemovedEvent(reader=self._get_device_str(), tag_id=tag_id) ) return callback - def run(self): - super().run() + def main(self): + fail_wait = 5 + max_fail_wait = 60 while not self.should_stop(): try: @@ -242,6 +244,13 @@ class NfcBackend(Backend): 'on-release': self._on_release(), } ) + fail_wait = 5 + except Exception as e: + self.logger.warning( + 'NFC error, retrying in %d seconds: %s', e, fail_wait, exc_info=True + ) + self.wait_stop(fail_wait) + fail_wait = min(fail_wait * 2, max_fail_wait) finally: self.close() diff --git a/platypush/plugins/nfc/manifest.yaml b/platypush/plugins/nfc/manifest.yaml new file mode 100644 index 00000000..14123d8e --- /dev/null +++ b/platypush/plugins/nfc/manifest.yaml @@ -0,0 +1,12 @@ +manifest: + events: + - platypush.message.event.nfc.NFCDeviceConnectedEvent + - platypush.message.event.nfc.NFCDeviceDisconnectedEvent + - platypush.message.event.nfc.NFCTagDetectedEvent + - platypush.message.event.nfc.NFCTagRemovedEvent + install: + pip: + - nfcpy>=1.0 + - ndeflib + package: platypush.plugins.nfc + type: plugin From dfaa5447acc5d439ccc82167f2e12c2029ad702b Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 27 Feb 2024 00:09:19 +0100 Subject: [PATCH 3/4] [#299] Refactored NFC backend into a runnable plugin. --- platypush/plugins/nfc/__init__.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/platypush/plugins/nfc/__init__.py b/platypush/plugins/nfc/__init__.py index 27c4c8ad..011c226b 100644 --- a/platypush/plugins/nfc/__init__.py +++ b/platypush/plugins/nfc/__init__.py @@ -25,13 +25,11 @@ class NfcPlugin(RunnablePlugin): """ :param device: Address or ID of the device to be opened. Examples: - * `'usb:003:009'` opens device 9 on bus 3 - * `'usb:003'` opens the first available device on bus 3 - * `'usb'` opens the first available USB device (default) + * ``usb:003:009`` opens device 9 on bus 3 + * ``usb:003`` opens the first available device on bus 3 + * ``usb`` opens the first available USB device (default) """ - super().__init__(*args, **kwargs) - self.device_id = device self._clf = None From 850cbe4237423a999bd9fdb63fdf238abc393667 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 27 Feb 2024 00:53:10 +0100 Subject: [PATCH 4/4] Updated docs --- docs/source/backends.rst | 1 - docs/source/platypush/backend/nfc.rst | 6 ------ docs/source/platypush/plugins/nfc.rst | 5 +++++ docs/source/plugins.rst | 1 + 4 files changed, 6 insertions(+), 7 deletions(-) delete mode 100644 docs/source/platypush/backend/nfc.rst create mode 100644 docs/source/platypush/plugins/nfc.rst diff --git a/docs/source/backends.rst b/docs/source/backends.rst index e2e11ebf..c69afb06 100644 --- a/docs/source/backends.rst +++ b/docs/source/backends.rst @@ -13,7 +13,6 @@ Backends platypush/backend/music.mopidy.rst platypush/backend/music.mpd.rst platypush/backend/music.spotify.rst - platypush/backend/nfc.rst platypush/backend/nodered.rst platypush/backend/redis.rst platypush/backend/scard.rst diff --git a/docs/source/platypush/backend/nfc.rst b/docs/source/platypush/backend/nfc.rst deleted file mode 100644 index 697c0109..00000000 --- a/docs/source/platypush/backend/nfc.rst +++ /dev/null @@ -1,6 +0,0 @@ -``nfc`` -========================= - -.. automodule:: platypush.backend.nfc - :members: - diff --git a/docs/source/platypush/plugins/nfc.rst b/docs/source/platypush/plugins/nfc.rst new file mode 100644 index 00000000..da623969 --- /dev/null +++ b/docs/source/platypush/plugins/nfc.rst @@ -0,0 +1,5 @@ +``nfc`` +======= + +.. automodule:: platypush.plugins.nfc + :members: diff --git a/docs/source/plugins.rst b/docs/source/plugins.rst index cd556732..a021e64d 100644 --- a/docs/source/plugins.rst +++ b/docs/source/plugins.rst @@ -88,6 +88,7 @@ Plugins platypush/plugins/music.spotify.rst platypush/plugins/music.tidal.rst platypush/plugins/nextcloud.rst + platypush/plugins/nfc.rst platypush/plugins/ngrok.rst platypush/plugins/nmap.rst platypush/plugins/ntfy.rst