-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiegand.py
executable file
·172 lines (122 loc) · 4.41 KB
/
wiegand.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 python
import pigpio
class decoder:
"""
A class to read Wiegand codes of an arbitrary length.
The code length and value are returned.
EXAMPLE
#!/usr/bin/env python
import time
import pigpio
import wiegand
def callback(bits, code):
print("bits={} code={}".format(bits, code))
pigpio.start()
w = wiegand.decoder(14, 15, callback)
time.sleep(300)
w.cancel()
pigpio.stop()
"""
def __init__(self, gpio_0, gpio_1, callback, bit_timeout=5):
"""
Instantiate with the gpio for 0 (green wire), the gpio for 1 (white wire),
the callback function, and the bit timeout in milliseconds which indicates
the end of a code.
The callback is passed the code length in bits and the value.
"""
self.gpio_0 = gpio_0
self.gpio_1 = gpio_1
self.callback = callback
self.bit_timeout = bit_timeout
self.in_code = False
pigpio.set_mode(gpio_0, pigpio.INPUT)
pigpio.set_mode(gpio_1, pigpio.INPUT)
pigpio.set_pull_up_down(gpio_0, pigpio.PUD_UP)
pigpio.set_pull_up_down(gpio_1, pigpio.PUD_UP)
self.cb_0 = pigpio.callback(gpio_0, pigpio.FALLING_EDGE, self._cb)
self.cb_1 = pigpio.callback(gpio_1, pigpio.FALLING_EDGE, self._cb)
def _cb(self, gpio, level, tick):
"""
Accumulate bits until both gpios 0 and 1 timeout.
"""
if level < pigpio.TIMEOUT:
if self.in_code == False:
self.bits = 1
self.facility_num = 0
self.id_num = 0
self.in_code = True
self.code_timeout = 0
pigpio.set_watchdog(self.gpio_0, self.bit_timeout)
pigpio.set_watchdog(self.gpio_1, self.bit_timeout)
else:
self.bits += 1
if (self.bits > 1) and (self.bits <= 9):
self.facility_num = self.facility_num << 1
if gpio == self.gpio_0:
self.code_timeout = self.code_timeout & 2 # clear gpio 0 timeout
else:
self.code_timeout = self.code_timeout & 1 # clear gpio 1 timeout
self.facility_num = self.facility_num | 1
elif (self.bits > 10) and (self.bits <= 25):
self.id_num = self.id_num << 1
if gpio == self.gpio_0:
self.code_timeout = self.code_timeout & 2 # clear gpio 0 timeout
else:
self.code_timeout = self.code_timeout & 1 # clear gpio 1 timeout
self.id_num = self.id_num | 1
else:
if self.in_code:
if gpio == self.gpio_0:
self.code_timeout = self.code_timeout | 1 # timeout gpio 0
else:
self.code_timeout = self.code_timeout | 2 # timeout gpio 1
if self.code_timeout == 3: # both gpios timed out
pigpio.set_watchdog(self.gpio_0, 0)
pigpio.set_watchdog(self.gpio_1, 0)
self.in_code = False
self.callback(self.bits, self.facility_num, self.id_num)
def cancel(self):
"""
Cancel the Wiegand decoder.
"""
self.cb_0.cancel()
self.cb_1.cancel()
if __name__ == "__main__":
import time
import pigpio
import wiegand
import urllib
import urllib2
import json
import dateutil.parser
from datetime import datetime
import pytz
cached_allows = {}
utc = pytz.utc
def open_door():
print "Opening door!"
def callback(bits, facility, id_num):
print("bits={} facility={} id={}".format(bits, facility, id_num))
if (bits == 26):
ident = str(facility) + str(id_num)
if ident in cached_allows and cached_allows[ident] > utc.localize(datetime.utcnow()):
print "Cached authorization is present and valid"
open_door()
else:
url = 'http://rpiambulance.com/doorauth.php'
values = {'facility_id': facility,
'card_id': id_num}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
html = response.read()
vars = json.loads(html)
print vars
if vars['authorized'] == True:
cached_allows[ident] = dateutil.parser.parse(vars['until']).astimezone(utc)
open_door()
pigpio.start()
w = wiegand.decoder(23, 24, callback)
time.sleep(300)
w.cancel()
pigpio.stop()