|
| 1 | +import time |
| 2 | +import serial |
| 3 | +import logging |
| 4 | +import argparse |
| 5 | +import redis |
| 6 | +from functools import wraps |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +def throttle(seconds): |
| 12 | + def decorator(func): |
| 13 | + func.last_call = 0 |
| 14 | + |
| 15 | + def reset_timer(): |
| 16 | + func.reset_timer = 0 |
| 17 | + |
| 18 | + func.reset_timer = reset_timer |
| 19 | + |
| 20 | + @wraps(func) |
| 21 | + def wrapper(*args, **kwargs): |
| 22 | + if time.time() - func.last_call > seconds: |
| 23 | + func.last_call = time.time() |
| 24 | + return func(*args, **kwargs) |
| 25 | + return wrapper |
| 26 | + return decorator |
| 27 | + |
| 28 | + |
| 29 | +@throttle(seconds=1) |
| 30 | +def parse_code(data): |
| 31 | + try: |
| 32 | + return int('0x{}'.format(data.decode('ascii').strip()), 16) |
| 33 | + except ValueError: |
| 34 | + # Invalid value has been read. Discard it and do not wait before |
| 35 | + # reading the next value. |
| 36 | + parse_code.reset_timer() |
| 37 | + return None |
| 38 | + |
| 39 | + |
| 40 | +def parse_codes(ser): |
| 41 | + while True: |
| 42 | + code = parse_code(ser.readline()) |
| 43 | + if code is not None: |
| 44 | + yield code |
| 45 | + |
| 46 | + |
| 47 | +def main(dev, rate, host, port, channel, verbose): |
| 48 | + # Configure logging to the console if requested. |
| 49 | + if verbose: |
| 50 | + handler = logging.StreamHandler() |
| 51 | + handler.setLevel(logging.DEBUG) |
| 52 | + logger.addHandler(handler) |
| 53 | + logger.setLevel(logging.DEBUG) |
| 54 | + |
| 55 | + r = redis.StrictRedis( |
| 56 | + host=host, |
| 57 | + port=port |
| 58 | + ) |
| 59 | + |
| 60 | + logger.debug('Starting reading from %s with baud rate %d.', dev, rate) |
| 61 | + with serial.Serial(dev, rate) as ser: |
| 62 | + # Discard the first value as it contains just a welcome message |
| 63 | + ser.readline() |
| 64 | + logger.debug('The connection has been estabilished and the ' |
| 65 | + 'welcome message read.') |
| 66 | + |
| 67 | + try: |
| 68 | + for code in parse_codes(ser): |
| 69 | + r.publish(channel, code) |
| 70 | + logger.debug('Code %d has been sent to the \'%s\' channel.', |
| 71 | + channel, code) |
| 72 | + except KeyboardInterrupt: |
| 73 | + logger.debug('CTRL-C received. Shutting down...') |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + parser = argparse.ArgumentParser( |
| 78 | + description='Listens for input from a RFID reader and publishes it ' |
| 79 | + 'to a Redis channel.' |
| 80 | + ) |
| 81 | + parser.add_argument('device', type=str, help='A device file to read from.') |
| 82 | + parser.add_argument('--channel', type=str, default='cards', |
| 83 | + help='Redis channel to send messages to.') |
| 84 | + parser.add_argument('--host', type=str, default='localhost', |
| 85 | + help='Redis host (default localhost).') |
| 86 | + parser.add_argument('--port', type=int, default=6379, |
| 87 | + help='Redis port (default 6379).') |
| 88 | + parser.add_argument('--rate', type=int, default=115200, |
| 89 | + help='The baud rate (default: 115200).') |
| 90 | + parser.add_argument('-v', '--verbose', help='Increase output verbosity.', |
| 91 | + action='store_true') |
| 92 | + args = parser.parse_args() |
| 93 | + main(**vars(args)) |
0 commit comments