-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathACL.py
202 lines (169 loc) · 6.75 KB
/
ACL.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
# -*- coding: utf-8 -*-
# --------------------------------------------------------------------------
# _____ ______________
# | __ \ /\|__ ____ __|
# | |__) | / \ | | | |
# | _ / / /\ \ | | | |
# | | \ \/ ____ \| | | |
# |_| \_\/ \_\_| |_| ... RFID ALL THE THINGS!
#
# A resource access control and telemetry solution for Makerspaces
#
# Developed at MakeIt Labs - New Hampshire's First & Largest Makerspace
# http://www.makeitlabs.com/
#
# Copyright 2018 MakeIt Labs
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# --------------------------------------------------------------------------
#
# Author: Steve Richardson (steve.richardson@makeitlabs.com)
#
from PyQt5.QtCore import Qt, QObject, QUrl, QIODevice, QByteArray, QDateTime, QMutex, pyqtSignal, pyqtSlot, pyqtProperty
from PyQt5.QtCore import QFile, QFileInfo
from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
from Logger import Logger
import logging
import hashlib
import time
import json
from CachedRemoteFile import CachedRemoteFile
class ACL(CachedRemoteFile):
recordUpdate = pyqtSignal()
@pyqtProperty(int, notify=recordUpdate)
def numRecords(self):
self.mutex.lock()
n = self._numRecords
self.mutex.unlock()
return n
@pyqtProperty(int, notify=recordUpdate)
def numActiveRecords(self):
self.mutex.lock()
n = self._numActiveRecords
self.mutex.unlock()
return n
@pyqtProperty(int, notify=recordUpdate)
def numMembers(self):
self.mutex.lock()
n = self._numMembers
self.mutex.unlock()
return n
@pyqtProperty(int, notify=recordUpdate)
def numActiveMembers(self):
self.mutex.lock()
n = self._numActiveMembers
self.mutex.unlock()
return n
@pyqtProperty(list, notify=recordUpdate)
def activeMemberList(self):
self.mutex.lock()
n = self._activeMemberList
self.mutex.unlock()
return n
@pyqtSlot()
def slotUpdate(self):
if self.mqtt:
o = { 'why': self._why, 'status': self.status, 'source': self.source, 'hash': self.hash, 'totalRecords': self._numRecords, 'activeRecords': self._numActiveRecords, 'totalMembers': self._numMembers, 'activeMembers': self.numActiveMembers}
self.mqtt.slotPublishSubtopic('acl/update', json.dumps(o))
self.recordUpdate.emit()
@pyqtProperty(str, notify=recordUpdate)
def why(self):
self.mutex.lock()
n = self._why
self.mutex.unlock()
return n
@pyqtSlot(str)
def setWhy(self, why):
self.mutex.lock()
self._why = why
self.mutex.unlock()
def __init__(self, loglevel='WARNING', netWorker = None, mqtt = None, url = '', cacheFile = None):
CachedRemoteFile.__init__(self)
self.logger = Logger(name='ratt.acl')
self.logger.setLogLevelStr('INFO')
self.debug = self.logger.isDebug()
self._numRecords = 0
self._numActiveRecords = 0
self._numMembers = 0
self._numActiveMembers = 0
self._activeMemberList = []
self._why = 'restart'
self.setup(logger=self.logger, netWorker=netWorker, url=url, cacheFile=cacheFile)
self.mqtt = mqtt
self.mqtt.broadcastEvent.connect(self.__slotBroadcastMQTTEvent)
self.mqtt.targetedEvent.connect(self.__slotTargetedMQTTEvent)
self.update.connect(self.slotUpdate)
def search(self, hash):
self.mutex.lock()
found_record = []
for record in self._obj:
if record['tagid'] == hash:
found_record = record
break
self.mutex.unlock()
return found_record
# this hook is called just after parsing but before hashes are checked
# the mutex is not held at this point
def parseJSON__hook_unlocked(self, parsed):
unique_members = []
active_members = []
num_records = 0
active_records = 0
for record in parsed:
num_records = num_records + 1
member_id = record['member']
allowed = record['allowed']
if not member_id in unique_members:
unique_members.append(member_id)
if allowed == 'allowed':
active_records = active_records + 1
if not member_id in active_members:
active_members.append(member_id)
o = {}
o['totalRecords'] = num_records
o['activeRecords'] = active_records
o['totalMembers'] = len(unique_members)
o['activeMembers'] = len(active_members)
o['activeMemberList'] = sorted(active_members)
return o
# this hook is called if the hashes differ, and the mutex IS held
def parseJSON__hook_locked(self, o):
self._numRecords = o['totalRecords']
self._numActiveRecords = o['activeRecords']
self._numMembers = o['totalMembers']
self._numActiveMembers = o['activeMembers']
self._activeMemberList = o['activeMemberList']
self.logger.info('updated ACL with %d records, %d active, hash=%s (why=%s)' % (self._numRecords, self._numActiveRecords, self._hash, self._why))
@pyqtSlot(str, str)
def __slotTargetedMQTTEvent(self, subtopic, message):
tsplit = subtopic.split('/')
if len(tsplit) >= 2 and tsplit[0] == 'acl':
cmd = tsplit[1]
if cmd == 'update':
self._why = 'mqttTargeted'
self.download()
# MQTT broadcast event
@pyqtSlot(str, str)
def __slotBroadcastMQTTEvent(self, subtopic, message):
tsplit = subtopic.split('/')
if len(tsplit) >= 2 and tsplit[0] == 'acl':
cmd = tsplit[1]
if cmd == 'update':
self._why = 'mqttBroadcast'
self.download()