-
Notifications
You must be signed in to change notification settings - Fork 2
/
MFRC522-trigger.py
executable file
·171 lines (126 loc) · 4.31 KB
/
MFRC522-trigger.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import pirc522
import sys
from os import path
import urllib.request
import subprocess
import time
import json
import logging
import logging.config
import logging.handlers
from actions import NfcEvent, resolve
from config import validate_config
pathname = path.dirname(path.abspath(__file__))
logging.config.fileConfig(pathname + '/logging.ini')
config = json.load(open(pathname + '/config.json', encoding="utf-8"))
def execute_curl(url):
logging.info("Gonna curl '" + url + "'")
try:
urllib.request.urlopen(url)
except Exception:
logging.error("Unable to open url " + url, sys.exc_info()[0])
def execute_command(command):
logging.info("Gonna execute '" + command + "'")
subprocess.call(command, shell=True, stdout=subprocess.DEVNULL)
ACTION_MAP = {
"curl": lambda action: execute_curl(action["url"]),
"command": lambda action: execute_command(action["command"])
}
def execute_action(event: NfcEvent, tag_id: str):
resolved_actions = resolve(config, event, tag_id)
for action in resolved_actions:
ACTION_MAP[action["type"]](action)
def tag_id_from_bytes(uid):
return ':'.join(x.to_bytes(1, 'big').hex() for x in uid).upper()
# welcome message
logging.info("Welcome to MFRC522-trigger!")
if not validate_config(config):
sys.exit(1)
logging.info("Press Ctrl-C to stop.")
# create a reader
reader = pirc522.RFID()
current_nuid = ''
current_tag = ''
last_tag = ''
count = 0
polling = False
last_was_7_byte_id = False
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while True:
try:
# wait for reader to send an interrupt
if not polling:
reader.wait_for_tag()
count += 1
logging.debug("Reader loop %d", count)
# scan for cards
if not polling or last_was_7_byte_id:
(error, tag_type) = reader.request()
# on error continue and retry
if error:
logging.debug("error request")
if current_tag != '':
execute_action(NfcEvent.REMOVE, current_tag)
current_tag = ''
current_nuid = ''
polling = False
continue
# reset after request
last_was_7_byte_id = False
# get the UID of the card
(error, uid) = reader.anticoll()
# on error continue and retry
if error:
logging.debug("error anticoll")
if current_tag != '':
execute_action(NfcEvent.REMOVE, current_tag)
current_tag = ''
current_nuid = ''
polling = False
continue
nuid = tag_id_from_bytes(uid)
got_uid = True
tag_id = ''
# Do we have an incomplete UID?!
if uid[0] != 0x88:
got_uid = False
tag_id = tag_id_from_bytes(uid[0:4])
if got_uid and (current_nuid != nuid):
# Activate the tag with the incomplete UID
error = reader.select_tag(uid)
if error:
logging.debug("error select_tag")
polling = False
continue
# Get the remaining bytes
error, uid2 = reader.anticoll2()
if error:
logging.debug("error anticoll2")
polling = False
continue
reader.halt()
last_was_7_byte_id = True
# Build the final UID without checksums
tag_id = tag_id_from_bytes(uid[1:-1] + uid2[:-1])
logging.debug("Tag ID: " + tag_id)
polling = True
# when we're still reading the same tag
if current_nuid == nuid:
# don't busy wait while there's a rfid tag near the reader
logging.debug("Sleep!")
time.sleep(0.1)
continue
current_tag = tag_id
current_nuid = nuid
# execute an action for the reading tag
execute_action(NfcEvent.REDETECT if current_tag == last_tag else NfcEvent.DETECT, tag_id)
last_tag = current_tag
except KeyboardInterrupt:
logging.info("Shutdown!")
break
except Exception:
logging.exception("Unexpected exception '%s' occurred!", str(sys.exc_info()[0].__name__))
break
reader.cleanup()