Last active
July 29, 2018 00:23
-
-
Save blacklight/ec5c651336ca34cb632e80a16670905c to your computer and use it in GitHub Desktop.
Read Platypush commands from an NFC card and deliver them to the local Redis queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
import json | |
import logging | |
import nfc | |
import ndef | |
import sys | |
import time | |
from redis import Redis | |
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) | |
def get_queue_name(): | |
return sys.argv[1] if len(sys.argv) > 1 else 'platypush_bus_mq' | |
def on_connect(tag): | |
has_msg_records = False | |
tag_id = ':'.join([('%02x' % ord(c)) for c in tag.identifier]) | |
logging.info('Tag ID {} connected'.format(tag_id)) | |
if not tag or not tag.ndef: | |
return | |
queue_name = get_queue_name() | |
for record in tag.ndef.records: | |
if isinstance(record, ndef.TextRecord): | |
try: | |
msg = json.loads(record.text) | |
has_msg_records = True | |
except: | |
continue | |
logging.info('Executing: {}'.format(record.text)) | |
redis = Redis() | |
redis.rpush(queue_name, msg) | |
return has_msg_records | |
def main(): | |
while True: | |
with nfc.ContactlessFrontend('usb') as clf: | |
tag = clf.connect( | |
rdwr={'on-connect': on_connect} | |
) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment