forked from AxisCommunications/door-controller-test-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PACSDoorManager.cpp
268 lines (237 loc) · 7.68 KB
/
PACSDoorManager.cpp
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
/*
Copyright (C) 2014 Axis Communications
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 2
of the License, or (at your option) 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "PACSDoorManager.h"
#include <serstream>
using namespace std;
/*
* Constructor.
*/
PACSDoorManager::PACSDoorManager() {}
/*
* Creates a new door and pushes it to the doors vector.
*/
PACSDoor* PACSDoorManager::createDoor(char* doorId) {
doors.push_back(PACSDoor(doorId));
return findDoorById(doorId);
}
/*
* Changes the id of a door.
*/
void PACSDoorManager::setDoorId(char* oldId, char* newId) {
PACSDoor* d = findDoorById(oldId);
if (d != NULL) {
strcpy(d->id, newId);
}
}
/*
* Initializes all the doors in the doors vector.
*/
void PACSDoorManager::initializeDoors() {
for (unsigned i=0; i < doors.size(); i++) {
doors[i].initialize();
}
}
/*
* Swipes a standard 26bit Wiegand card at the specified door and
*/
bool PACSDoorManager::swipeCard(char* doorId, char* readerId, unsigned long facilityCode, unsigned long cardNumber) {
PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
if (d->swipeCard(readerId, facilityCode, cardNumber)) {
cout << "[" << doorId << "|" << readerId << "]"<< F(": Card swiped. Facility code: ")
<< facilityCode << F(". Card number: ") << cardNumber << endl;
return true;
}
else {
cout << "Reader not found: " << readerId << endl;
return false;
}
}
cout << "Door not found: " << doorId << endl;
return false;
}
/*
* Enters a pin digit/sequence at the specified door and reader.
*/
bool PACSDoorManager::enterPIN(char* doorId, char* readerId, char* code) {
PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
if (d->enterPIN(readerId, code)) {
cout << "[" << doorId << "|" << readerId << "]" << F(": Entered PIN digit(s): ")
<< code << endl;
return true;
}
else {
cout << "Reader not found: " << readerId << endl;
return false;
}
}
cout << "Door not found: " << doorId << endl;
return false;
}
/*
* Opens the specified door monitor att the specified door.
*/
bool PACSDoorManager::openDoor(char* doorId, char* doorMonitorId) {
PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
if (d->openDoor(doorMonitorId)) {
cout << "[" << doorId << "|" << doorMonitorId << "]" << F(": Door opened.") << endl;
return true;
}
else {
cout << "Peripheral not found: " << doorMonitorId << endl;
return false;
}
}
cout << "Door not found: " << doorId << endl;
return false;
}
/*
* Closes the specified door monitor att the specified door.
*/
bool PACSDoorManager::closeDoor(char* doorId, char* doorMonitorId) {
PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
if (d->closeDoor(doorMonitorId)) {
cout << "[" << doorId << "|" << doorMonitorId << "]" << F(": Door closed.") << endl;
return true;
}
else {
cout << "Peripheral not found: " << doorMonitorId << endl;
return false;
}
}
cout << "Door not found: " << doorId << endl;
return false;
}
/*
* Pushes the specified REX device att the specified door.
*/
bool PACSDoorManager::pushREX(char* doorId, char* rexId) {
PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
if (d->pushREX(rexId)) {
cout << "[" << doorId << "|" << rexId << "]" << F(": REX pushed.") << endl;
return true;
}
else {
cout << "Peripheral not found: " << rexId << endl;
return false;
}
}
cout << "Door not found: " << doorId << endl;
return false;
}
/*
*
*/
bool PACSDoorManager::activateInput(char* doorId, char* inputId) {
PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
if (d->activateInput(inputId)) {
cout << "[" << doorId << "|" << inputId << "]" << F(": Input activated.") << endl;
return true;
}
else {
cout << "Peripheral not found: " << inputId << endl;
return false;
}
}
cout << "Door not found: " << doorId << endl;
return false;
}
/*
*
*/
bool PACSDoorManager::deactivateInput(char* doorId, char* inputId) {
PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
if (d->deactivateInput(inputId)) {
cout << "[" << doorId << "|" << inputId << "]" << F(": Input deactivated.") << endl;
return true;
}
else {
cout << "Peripheral not found: " << inputId << endl;
return false;
}
}
cout << "Door not found: " << doorId << endl;
return false;
}
/*
* Calls the updateLevels function for all the doors in the doors vector.
* This will read the current levels of all the peripherals connected to the Arduino,
* and if they have changed, the registered callback will be called.
*/
void PACSDoorManager::updateLevels() {
for (unsigned i=0; i < doors.size(); i++) {
doors[i].updateLevels();
}
}
/*
* Check if the specified peripheral is active (depends on the current state and
* the peripherals configured ActiveLevel in the doors.cfg file).
*/
int PACSDoorManager::isPeripheralActive(char* doorId, char* peripheralId) {
PACSPeripheral* p = findPeripheralById(doorId, peripheralId);
if (p != NULL) {
return (p->isActive() ? 1 : 0);
}
return -1;
}
/*
* For the specified id, we search the doors vector and if we find a match,
* return a pointer to the door (and NULL if no match is found.)
*/
PACSDoor* PACSDoorManager::findDoorById(char* doorId) {
for (unsigned i=0; i < doors.size(); i++) {
if ((strcmp(doors[i].id, doorId) == 0))
return &doors[i];
}
return NULL;
}
/*
* For the specified door id and reader id, we search the doors vector and its
* associated readers and if we find a match, return a pointer to the reader
* (and NULL if no match is found.)
*/
PACSReader* PACSDoorManager::findReaderById(char* doorId, char* readerId) {
PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
return d->findReaderById(readerId);
}
return NULL;
}
/*
* For the specified door id and peripheral id, we search the doors vector and its
* associated peripherals and if we find a match, return a pointer to the peripheral
* (and NULL if no match is found.)
*/
PACSPeripheral* PACSDoorManager::findPeripheralById(char* doorId, char* peripheralId) {
PACSDoor* d = findDoorById(doorId);
if (d != NULL) {
return d->findPeripheralById(peripheralId);
}
return NULL;
}
/*
* Registers a function to be called when a pin changes state.
*/
void PACSDoorManager::registerStateChangeCallback(StateChangeCallback *callback) {
for (unsigned i=0; i < doors.size(); i++) {
doors[i].registerStateChangeCallback(callback);
}
}