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 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/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: diff --git a/platypush/backend/nfc/__init__.py b/platypush/plugins/nfc/__init__.py similarity index 85% rename from platypush/backend/nfc/__init__.py rename to platypush/plugins/nfc/__init__.py index e6dc13a5..011c226b 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 @@ -24,13 +25,11 @@ class NfcBackend(Backend): """ :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 @@ -40,23 +39,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 +211,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 +223,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 +242,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