-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarmwidget.cpp
216 lines (187 loc) · 5.96 KB
/
alarmwidget.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
#include "alarmwidget.h"
#include "./ui_alarmwidget.h"
#include "httplib.h"
#include <QDebug>
#include <QJsonArray>
#include <QMessageBox>
#include <QJsonObject>
#include <QInputDialog>
#include <QJsonDocument>
#include <QNetworkReply>
#include <QNetworkInterface>
#include <QNetworkAccessManager>
using namespace httplib;
AlarmWidget::AlarmWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::AlarmWidget)
{
ui->setupUi(this);
connect(this, &AlarmWidget::newPostData, this, &AlarmWidget::postRecved);
listenThread = std::thread([this](){
httplib::Server *svr = new httplib::Server;
svr->Post("/my_test_ep", [&](const auto& req, Response& res, const ContentReader &content_reader) {
std::string body;
content_reader([&](const char *data, size_t data_length) {
body.append(data, data_length);
return true;
});
emit newPostData(QString::fromStdString(body));
});
svr->listen("0.0.0.0", 24567);
});
QGridLayout *layout = new QGridLayout;
for (int i = 0; i < 16; i++) {
QLabel *label = new QLabel(this);
layout->addWidget(label, i / 4, i % 4, Qt::AlignCenter);
alarmLabels << label;
label->hide();
}
ui->tabWidget->widget(1)->setLayout(layout);
}
AlarmWidget::~AlarmWidget()
{
delete ui;
}
void AlarmWidget::updateActiveEvents()
{
const auto &m = trackingObjects[ui->comboCameras->currentText()];
for (int i = 0; i < 16; i++)
alarmLabels[i]->hide();
int count = 0;
for (const auto &[key, value]: m) {
const auto &ba = QByteArray::fromBase64(value["image"].toString().toUtf8());
auto image = QImage::fromData(ba);
alarmLabels[count]->setToolTip(value["colorDistance"].toString());
alarmLabels[count]->setPixmap(QPixmap::fromImage(image));
alarmLabels[count++]->show();
}
}
void AlarmWidget::handleNewEvent(const QJsonObject &obj)
{
/* do nothing if this is not the currently selected camera */
auto cam = obj["cameraName"].toString();
if (cam != ui->comboCameras->currentText())
return;
/* update event details list */
auto key = QString("%1").arg(obj["trackId"].toString());
qDebug() << "new" << key;
ui->listEvents->addItem(key);
on_listEvents_currentRowChanged(ui->listEvents->count() - 1);
/* update active event grid */
updateActiveEvents();
}
void AlarmWidget::handleEventFinish(const QJsonObject &obj)
{
/* do nothing if this is not the currently selected camera */
auto cam = obj["cameraName"].toString();
if (cam != ui->comboCameras->currentText())
return;
/* delete event from details */
auto key = QString("%1").arg(obj["trackId"].toString());
qDebug() << "finish" << key;
for (int i = 0; i < ui->listEvents->count(); i++) {
if (ui->listEvents->item(i)->text() == key) {
delete ui->listEvents->takeItem(i);
if (ui->listEvents->count())
on_listEvents_currentRowChanged(0);
else
clearAlarm();
return;
}
}
/* update active event grid */
updateActiveEvents();
}
void AlarmWidget::postRecved(const QString &data)
{
const auto &doc = QJsonDocument::fromJson(data.toUtf8());
const auto &arr = doc.array();
if (!arr.size()) {
qDebug() << "zero array size";
return;
}
for (const auto &value: arr) {
const auto &obj = value.toObject();
auto type = obj["type"].toString();
auto trackId = obj["trackId"].toString();
auto cam = obj["cameraName"].toString();
/* add camera name to the camera filter combo */
if (ui->comboCameras->findText(cam) == -1)
ui->comboCameras->addItem(cam);
/* update our local alarm data structure */
if (type == "start") {
trackingObjects[cam][trackId] = obj;
handleNewEvent(obj);
} else if (type == "end") {
if (!trackingObjects[cam].count(trackId)) {
qDebug("no previous track event for '%s'", qPrintable(trackId));
} else {
trackingObjects[cam].erase(trackId);
handleEventFinish(obj);
}
} else
qDebug() << "unhandled event type" << type << obj;
}
}
void AlarmWidget::on_listEvents_currentRowChanged(int currentRow)
{
if (currentRow < 0)
return;
auto key = ui->listEvents->item(currentRow)->text();
auto obj = trackingObjects[ui->comboCameras->currentText()][key];
const auto &ba = QByteArray::fromBase64(obj["image"].toString().toUtf8());
obj.remove("image");
auto image = QImage::fromData(ba);
ui->labelImage->setPixmap(QPixmap::fromImage(image));
ui->plainEventJson->setPlainText(QString::fromUtf8(QJsonDocument(obj).toJson()));
}
void AlarmWidget::clearAlarm()
{
ui->labelImage->clear();
ui->plainEventJson->clear();
ui->listEvents->clear();
}
void AlarmWidget::on_comboCameras_currentIndexChanged(int index)
{
clearAlarm();
}
void AlarmWidget::on_pushSubscribe_clicked()
{
auto text = QInputDialog::getText(this, "Subscription", "Please enter your target IP");
if (text.isEmpty())
return;
QString myip;
if (myip.isEmpty()) {
auto addresses = QNetworkInterface::allAddresses();
for (auto &addr: addresses) {
if (addr.isLoopback() || addr.isBroadcast() || addr.isLinkLocal())
continue;
myip = addr.toString();
break;
}
}
/* register ourselves */
QNetworkRequest req(QUrl(QString("http://%1:50043/EventSub/Subscribe").arg(text)));
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QJsonObject obj;
obj["url"] = QString("http://%1:24567/my_test_ep").arg(myip);
auto *reply = nm.post(req, QJsonDocument(obj).toJson());
connect(reply, &QNetworkReply::errorOccurred, this, [this](QNetworkReply::NetworkError err) {
auto *reply = (QNetworkReply *)sender();
QMessageBox::warning(this, "Subscription error", QString("Error '%1' subscribing to alarm server").arg(err));
reply->deleteLater();
});
connect(reply, &QNetworkReply::finished, this, [this]() {
auto *reply = (QNetworkReply *)sender();
if (reply->error() == QNetworkReply::NoError)
QMessageBox::warning(this, "Subscription", QString("Successfully subscribed to alarm server"));
else
QMessageBox::warning(this, "Subscription error", QString("Error '%1' subscribing to alarm server").arg(reply->error()));
reply->deleteLater();
});
}
void AlarmWidget::on_tabWidget_currentChanged(int index)
{
if (index == 1)
updateActiveEvents();
}