-
Notifications
You must be signed in to change notification settings - Fork 0
/
enterprised.py
276 lines (199 loc) · 8.23 KB
/
enterprised.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python
# coding: utf-8
#
# Authorization module software developed for the staircase door access control at Hackerspace Kraków.
# Copyright (C) 2016 Tadeusz Magura-Witkowski
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
New enterprise RFID authentication controller
Copyright (C) 2016 Tadeusz Magura-Witkowski
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import os
import json
import serial
import logging
import argparse
import threading
import ConfigParser
import paho.mqtt.client as paho
from constants import *
class EnterpriseDriverConfig(object):
def __init__(self, serial_url, serial_speed, mqtt_host, mqtt_port):
super(EnterpriseDriverConfig, self).__init__()
self.serial_url = serial_url
self.serial_speed = serial_speed
self.mqtt_host = mqtt_host
self.mqtt_port = mqtt_port
class EnterpriseDriver(object):
PING_TIMEOUT_THRESHOLD = 5
def __init__(self, config):
super(EnterpriseDriver, self).__init__()
self._config = config
def _connect_serial(self):
self._ser = serial.serial_for_url(self._config.serial_url, self._config.serial_speed)
def _send_to_io(self, message):
logging.debug('Sending: %s', message)
self._ser.write(message + '\n');
# TODO: replace with functools..
def _send_ping(self):
PING_PACKET = '*P#'
self._send_to_io(PING_PACKET)
def _test_serial(self):
PING_REPLY_PACKET = '*P'
self._ser.timeout = 1
self._send_ping()
response = self._ser.readline().strip()
if response != PING_REPLY_PACKET:
logging.critical('Invalid or no reply from IO board')
exit(0)
logging.info('IO board has replied to ping, looks like everything is okay')
def _mqtt_incoming_Accept(self, zone):
logging.info('Accept: Z: %s', zone)
self._ser.write('*A#{0}\n'.format(zone))
def _mqtt_incoming_Reject(self, zone):
logging.info('Reject: Z: %s', zone)
self._ser.write('*R#{0}\n'.format(zone))
def _mqtt_incoming(self, client, userdata, message):
msg = json.loads(message.payload)
zone = int(msg['zone'])
action = msg['action']
ACTION_MAPPING = {
'accept': self._mqtt_incoming_Accept,
'reject': self._mqtt_incoming_Reject
}
try:
ACTION_MAPPING[action](zone)
except KeyError:
logging.warning('MQTT has requested unknown action type: \'%s\'', action)
def _mqtt_connected(self, client, userdata, flags, rc):
client.subscribe('enterprised/reader/+')
def _connect_mqtt(self):
self._client = paho.Client()
self._client.on_message = self._mqtt_incoming
self._client.on_connect = self._mqtt_connected
self._client.will_set('enterprised/system', json.dumps({
'event': EVENT_SHUTDOWN
}))
self._client.connect(self._config.mqtt_host, port=self._config.mqtt_port)
self._client.loop_start()
def _io_to_mqtt_KeyPress(self, message):
# zone, keycode
zone = int(message[0])
keycode = int(message[1])
logging.info('KeyPress: Z: %s C: %s', zone, keycode)
return ('enterprised/reader/{0}/keypress'.format(zone), json.dumps({
'event': EVENT_KEYPRESS,
'zone': zone,
'keycode': keycode,
}))
def _io_to_mqtt_CardRead(self, message):
# zone, cardcode
zone = int(message[0])
cardcode = int(message[1])
logging.info('CardRead: Z: %s C: %s', zone, cardcode)
return ('enterprised/reader/{0}/cardread'.format(zone), json.dumps({
'event': EVENT_CARDREAD,
'zone': zone,
'cardcode': cardcode,
}))
def _io_to_mqtt_Tamper(self, message):
# zone
zone = int(message[0])
logging.info('Tamper: Z: %s', zone)
return ('enterprised/reader/{0}/tamper'.format(zone), json.dumps({
'event': EVENT_TAMPER,
'zone': zone
}))
def _io_to_mqtt_Watchdog(self, message):
logging.warning('IO board has been reseted by watchdog')
return ('enterprised/system', json.dumps({
'event': EVENT_WATCHDOG
}))
def _io_to_mqtt_PingReply(self, message):
logging.debug('Ping reply')
self._ping_sent_without_response = 0
def _io_to_mqtt(self, message):
action = message[0]
ACTION_MAPPING = {
'K': self._io_to_mqtt_KeyPress,
'C': self._io_to_mqtt_CardRead,
'T': self._io_to_mqtt_Tamper,
'P': self._io_to_mqtt_PingReply,
'W': self._io_to_mqtt_Watchdog,
}
message = message[1:]
try:
return ACTION_MAPPING[action](message)
except KeyError:
logging.warning('IO board has sent unknown action type: \'%s\'', action)
def _process_io2mqtt(self):
self._ser.timeout = 5
self._ping_sent_without_response = 0
while True:
message_from_io = self._ser.readline().strip()
if len(message_from_io) == 0:
self._send_ping()
self._ping_sent_without_response += 1
if self._ping_sent_without_response > EnterpriseDriver.PING_TIMEOUT_THRESHOLD:
logging.warning('Ping timeout')
self._client.publish('enterprised/system', json.dumps({
'event': EVENT_TIMEOUT
}))
continue
logging.debug('Received: %s', message_from_io)
if message_from_io[0] != '*':
logging.warning('Invalid message from IO board')
continue
message_from_io = message_from_io[1:].split('#')
to_mqtt = self._io_to_mqtt(message_from_io)
if to_mqtt is None:
continue
self._client.publish(*to_mqtt)
def run(self):
self._connect_serial()
self._test_serial()
self._connect_mqtt()
self._process_io2mqtt()
def main():
parser = argparse.ArgumentParser(
description='Enterprise RFID Unique (EM4100) Access Controller Driver - MQTT<->IOboard link')
parser.add_argument('--log', default='INFO', choices=('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'),
help='log level')
args = parser.parse_args()
numeric_level = getattr(logging, args.log.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(level=numeric_level)
config_file = ConfigParser.RawConfigParser()
config_file.read(['config.ini', 'localconfig.ini'])
config = EnterpriseDriverConfig(
serial_url=config_file.get(CONFIG_SECTION_SERIAL, CONFIG_SERIAL_PORT),
serial_speed=config_file.getint(CONFIG_SECTION_SERIAL, CONFIG_SERIAL_SPEED),
mqtt_host=config_file.get(CONFIG_SECTION_MQTT, CONFIG_MQTT_HOST),
mqtt_port=config_file.getint(CONFIG_SECTION_MQTT, CONFIG_MQTT_PORT))
enterprise_driver = EnterpriseDriver(config=config)
enterprise_driver.run()
if __name__ == '__main__':
main()