-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfpmain.cpp
More file actions
199 lines (172 loc) · 4.56 KB
/
fpmain.cpp
File metadata and controls
199 lines (172 loc) · 4.56 KB
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
#include "fpmain.h"
#include "fpthread.h"
#include "defs.h"
#include <QDebug>
#include <QJsonDocument>
#include <stdlib.h>
FpMain::FpMain(QObject *parent) : QObject(parent)
{
// read config
QSettings conf(CONFIG_FILE, QSettings::IniFormat, this);
SINGLE_OPEN_TIME = conf.value("SINGLE_OPEN_TIME", 5).toUInt();
BUZZ_OPEN_PWM = conf.value("BUZZ_OPEN_PWM", 256).toUInt();
BUZZ_PULSE_TIME = conf.value("BUZZ_PULSE_TIME", 100).toUInt();
// start fingerprint thread
connect(this, SIGNAL(enroll(bool)), &fpThread, SLOT(enroll(bool)));
connect(this, SIGNAL(del(int)), &fpThread, SLOT(del(int)));
connect(&fpThread, SIGNAL(match(int,int,bool)), this, SLOT(fpMatch(int,int,bool)));
connect(&fpThread, SIGNAL(enrollFinished(int, bool)), this, SLOT(fpEnrollFinished(int, bool)));
fpThread.start();
// start MQTT connection
connect(&mClient, SIGNAL(stateChanged(ClientState)), this, SLOT(mqttStateChanged()));
connect(&mClient, SIGNAL(messageReceived(QByteArray,QMqttTopicName)), this, SLOT(mqttReceive(QByteArray,QMqttTopicName)));
mClient.setHostname("localhost");
mClient.setPort(1883);
mClient.connectToHost();
// configure GPIO
system("gpio mode 1 pwm"); // door buzzer
system("gpio mode 4 out"); // green LED
system("gpio mode 5 out"); // red LED
system("gpio mode 7 in"); // sensor button
lock();
}
void FpMain::mqttStateChanged()
{
auto state = mClient.state();
switch(state)
{
case QMqttClient::Disconnected: qDebug() << "MQTT disconnected"; break;
case QMqttClient::Connecting: qDebug() << "MQTT connecting"; break;
case QMqttClient::Connected:
{
qDebug() << "MQTT connected, subscribe to topics";
// subscribe to MQTT topics
mClient.subscribe(QMqttTopicFilter("ENROLL"), 1);
mClient.subscribe(QMqttTopicFilter("DELETE"), 1);
mClient.subscribe(QMqttTopicFilter("UNLOCK"), 1);
mClient.subscribe(QMqttTopicFilter("LOCK"), 1);
break;
}
}
}
void FpMain::fpMatch(int id, int score, bool button)
{
//qDebug() << "fpMatch";
QJsonObject obj(
{
{"pattern", "MATCH"},
{"data", QJsonObject(
{
{"externalFingerId", id},
{"score", score},
{"button", button}
})
}
});
QJsonDocument doc(obj);
mClient.publish(QMqttTopicName("MATCH"), doc.toJson(), 1);
}
void FpMain::fpEnrollFinished(int id, bool success)
{
//qDebug() << "fpEnrollFinished";
QJsonObject obj(
{
{"pattern", "ENROLL_FINISHED"},
{"data", QJsonObject(
{
{"externalFingerId", id},
{"success", success}
})
}
});
QJsonDocument doc(obj);
mClient.publish(QMqttTopicName("ENROLL_FINISHED"), doc.toJson(), 1);
}
void FpMain::mqttReceive(const QByteArray &message, const QMqttTopicName &topic)
{
//qDebug() << "MQTT message received";
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(message, &error);
if(doc.isNull())
{
qWarning() << "mqttReceive(): JSON parsing error at offset" << error.offset << ":" << error.errorString();
return;
}
if(!doc.isObject())
{
qWarning() << "mqttReceive(): message does not contain a JSON object";
return;
}
QJsonObject obj=doc.object();
if(!obj.contains("data"))
{
qWarning() << "mqttReceive() message does not contain 'data'";
return;
}
obj = obj["data"].toObject();
if(topic.name() == "ENROLL")
{
if(obj.contains("run"))
{
bool run = obj["run"].toBool();
emit enroll(run);
}
else
{
qWarning() << "mqttReceive(): ENROLL: 'run' not found";
}
}
else if(topic.name() == "DELETE")
{
if(obj.contains("externalFingerId"))
{
int id = obj["externalFingerId"].toInt();
emit del(id);
}
else
{
qWarning() << "mqttReceive(): DELETE: 'externalFingerId' not found";
}
}
else if(topic.name() == "UNLOCK")
{
bool keepOpen = false;
if(obj.contains("keepOpen"))
{
keepOpen = obj["keepOpen"].toBool();
}
unlock(keepOpen);
}
else if(topic.name() == "LOCK")
{
lock();
}
else
{
qWarning() << "mqttReceive(): unknown topic" << topic.name();
}
}
void FpMain::unlock(bool keepOpen)
{
qDebug() << "UNLOCK, keepOpen:" << keepOpen;
system("gpio pwm 1 1024"); // door buzzer full power
system("gpio write 4 1"); // green LED on
system("gpio write 5 0"); // red LED off
QThread::msleep(BUZZ_PULSE_TIME);
system(QString("gpio pwm 1 %1").arg(BUZZ_OPEN_PWM).toStdString().c_str()); // reduce buzzer pwm to minimize power dissipation
if(!keepOpen)
{
QTimer::singleShot(int32_t(SINGLE_OPEN_TIME) * 1000, this, SLOT(lock()));
}
}
void FpMain::lock()
{
qDebug() << "LOCK";
system("gpio pwm 1 0"); // buzzer off
system("gpio write 4 0"); // green LED off
system("gpio write 5 1"); // red LED on
}
FpMain::~FpMain()
{
mClient.disconnectFromHost();
}