Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import shlex
- import subprocess
- class RFIDReaderWrapper(object):
- """runs rfid reader as a subprocess & parses tag serials
- from its output
- """
- def __init__(self, cmd):
- self._cmd_list = shlex.split(cmd)
- self._subprocess = None
- self._init_subprocess()
- def _init_subprocess(self):
- self._subprocess = subprocess.Popen(self._cmd_list,
- stderr=subprocess.PIPE)
- def read_tag_serial(self):
- """blocks until new tag is read
- returns serial of the tag once the read happens
- """
- if not self._subprocess:
- self._init_subprocess()
- while 1:
- line = self._subprocess.stderr.readline()
- if isinstance(line, bytes):
- # python3 compat
- line = line.decode("utf-8")
- if line == '':
- # EOF
- return None
- if not line.startswith("New tag"):
- continue
- serial = line.split()[-1].split("=", 1)[1]
- return serial
- def get_serialmap(filename):
- """read serial -> name mappings from the given file
- and return them as a dictionary.
- file is assumed to look like:
- [2f75b800]; Peter Pan
- """
- serials = {}
- with open(filename, "r") as fd:
- # lets build serial -> name mapping
- for line in fd.readlines():
- serial, name = line.strip().split(";", 1)
- # XXX: we strip [ ] from the serial
- serials[serial[1:-1]] = name
- return serials
- if __name__ == '__main__':
- reader = RFIDReaderWrapper("./rc522_reader -d")
- while 1:
- fobj = open("/root/status.txt","r+")
- serial = reader.read_tag_serial()
- # blink the lights etc.
- print(serial)
- print (get_serialmap(sys.argv[1]))
- fobj.close()
Advertisement
Add Comment
Please, Sign In to add comment