Guest User

Untitled

a guest
Jan 14th, 2014
329
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. import shlex
  3. import subprocess
  4.  
  5. class RFIDReaderWrapper(object):
  6.     """runs rfid reader as a subprocess & parses tag serials
  7.    from its output
  8.    """
  9.     def __init__(self, cmd):
  10.         self._cmd_list = shlex.split(cmd)
  11.         self._subprocess = None
  12.         self._init_subprocess()
  13.  
  14.     def _init_subprocess(self):
  15.         self._subprocess = subprocess.Popen(self._cmd_list,
  16.             stderr=subprocess.PIPE)
  17.  
  18.     def read_tag_serial(self):
  19.         """blocks until new tag is read
  20.        returns serial of the tag once the read happens
  21.        """
  22.         if not self._subprocess:
  23.             self._init_subprocess()
  24.  
  25.         while 1:
  26.             line = self._subprocess.stderr.readline()
  27.             if isinstance(line, bytes):
  28.                 # python3 compat
  29.                 line = line.decode("utf-8")
  30.  
  31.             if line == '':
  32.                 # EOF
  33.                 return None
  34.  
  35.             if not line.startswith("New tag"):
  36.                 continue
  37.  
  38.             serial = line.split()[-1].split("=", 1)[1]
  39.             return serial
  40.    
  41.     def get_serialmap(filename):
  42.         """read serial -> name mappings from the given file
  43.        and return them as a dictionary.
  44.        file is assumed to look like:
  45.        [2f75b800]; Peter Pan
  46.        """
  47.         serials = {}
  48.         with open(filename, "r") as fd:
  49.             # lets build serial -> name mapping
  50.             for line in fd.readlines():
  51.                 serial, name = line.strip().split(";", 1)
  52.                 # XXX: we strip [ ] from the serial
  53.                 serials[serial[1:-1]] = name
  54.  
  55.         return serials
  56.  
  57.  
  58.  
  59. if __name__ == '__main__':
  60.     reader = RFIDReaderWrapper("./rc522_reader -d")
  61.     while 1:
  62.         fobj = open("/root/status.txt","r+")
  63.         serial = reader.read_tag_serial()
  64.         # blink the lights etc.
  65.         print(serial)
  66.         print (get_serialmap(sys.argv[1]))
  67.         fobj.close()
RAW Paste Data