Repository navigation
Expand file tree
/
Copy pathinterrupt_handler.py
More file actions
61 lines (51 loc) · 1.73 KB
/
Copy pathinterrupt_handler.py
File metadata and controls
61 lines (51 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/local/bin/python3
import sys
from timeit import default_timer as timer
import time
import signal
import zmq
import logging
from pirc522 import RFID
SECTOR = 4
ENDPOINT = "tcp://127.0.0.1:5557"
CACHE_PERIOD = 4
#Logging setup
logger = logging.getLogger('interrupt_handler')
# logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s:%(name)s:%(message)s")
file_handler = logging.FileHandler('interrupt_handler.log')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
rdr = RFID()
context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.bind(ENDPOINT)
publisher.setsockopt(zmq.CONFLATE,1)
def fexit(signal,frame):
rdr.cleanup()
publisher.close()
context.term()
sys.exit(0)
signal.signal(signal.SIGINT, fexit)
# May need to prevent sending of duplicate IDs within the cache period
prev_id = cur_id = None
last_sent = 0
while True:
rdr.wait_for_tag()
(error, tag_type) = rdr.request()
if not error:
# Tag detected
(error, uid) = rdr.anticoll()
if not error:
if not rdr.select_tag(uid):
# Using default shipping key A
if not rdr.card_auth(rdr.auth_a, SECTOR, [0xFF,0XFF,0XFF,0XFF,0XFF,0XFF], uid):
(error, byte_array) = rdr.read(SECTOR)
cur_id = sum(byte_array)
# print(f"Read ID {cur_id} from tag.\n")
logger.log(logging.DEBUG, f"Read ID {cur_id} from tag. Sending to cache.")
# if timer() - last_sent > 4 and cur_id != prev_id:
# publisher.send_pyobj(cur_id)
publisher.send_pyobj(cur_id)
rdr.stop_crypto()