Merge pull request 'Transform NFC backend into a runnable plugin' (#369) from 299-nfc-backend-migration into master
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Reviewed-on: #369 Closes: #299
This commit is contained in:
commit
f51212dc97
|
@ -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
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
``nfc``
|
||||
=========================
|
||||
|
||||
.. automodule:: platypush.backend.nfc
|
||||
:members:
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
``nfc``
|
||||
=======
|
||||
|
||||
.. automodule:: platypush.plugins.nfc
|
||||
:members:
|
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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:
|
||||
|
|
|
@ -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()
|
||||
|
|
@ -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
|
Loading…
Reference in New Issue