Skip to content

Instantly share code, notes, and snippets.

@blacklight
Last active July 29, 2018 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blacklight/ec5c651336ca34cb632e80a16670905c to your computer and use it in GitHub Desktop.
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
#!/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