-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.cpp
290 lines (244 loc) · 8.86 KB
/
webserver.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#define CROW_MAIN
#include "webserver.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <err.h>
#include <nlohmann/json.hpp>
#include "index.html.hpp"
#include "script.js.hpp"
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
using json = nlohmann::json;
void sendPOITemp(crow::response &res, std::vector<POI> poi)
{
std::stringstream ss;
for (auto p : poi)
ss << p.name << "=" << std::fixed << std::setprecision(2) << p.temp << "\n";
res.write(ss.str());
}
void sendHeatSources(crow::response &res, std::vector<HeatSource> poi)
{
std::stringstream ss;
ss << "heat_sources=";
for (auto p : poi)
ss << p.location.x << "," << p.location.y << "," <<
std::fixed << std::setprecision(2) << p.neg_laplacian << ";";
std::string s = ss.str();
s.pop_back();
s += "\n";
res.write(s);
}
void sendCameraComponentTemps(crow::response &res, std::vector<std::pair<std::string, double>> cameraComponentTemps)
{
std::stringstream ss;
for (auto el : cameraComponentTemps)
ss << el.first << "=" << std::fixed << std::setprecision(2) << el.second << "\n";
res.write(ss.str());
}
void sendPOIPosStd(crow::response &res, std::vector<POI> poi)
{
std::stringstream ss;
for (auto p : poi)
ss << p.name << "=" << std::fixed << std::setprecision(4) << p.rolling_std << "\n";
res.write(ss.str());
}
std::string get_poi_name(const std::string &poi_path)
{
fs::path p(poi_path);
if (fs::is_symlink(p))
p = fs::read_symlink(p);
return p.stem();
}
Webserver::Webserver(const std::string &poi_path)
: poi_name(get_poi_name(poi_path))
, web_thread(&Webserver::start, this)
{}
void Webserver::terminate()
{
if (!finished)
pthread_kill(web_thread.native_handle(), SIGINT);
web_thread.join();
}
void Webserver::update(const thermo_img &ti)
{
{
std::lock_guard<std::mutex> lk(lock);
this->ti = ti;
frame_cnt++;
}
noticeClients();
}
void Webserver::update_temps(const std::vector<std::pair<string, double> > &cct)
{
std::lock_guard<std::mutex> lk(lock);
this->cameraComponentTemps = cct;
}
void to_json(json& j, const HeatSource& p) {
j = json::array({p.location.x, p.location.y, int(p.neg_laplacian * 1000)/1000.0});
}
// Called from update() - no need to lock this->lock
void Webserver::noticeClients() {
json msg;
json msg_lwi = json::array();
msg["type"] = "update";
for (const auto &lwi : ti.get_webimgs()) {
json msg_wi = json::array();
for (const auto& wi : lwi)
msg_wi.push_back({{"name", wi.name}, {"title", wi.title}, {"desc", wi.html_desc}});
msg_lwi.push_back(msg_wi);
}
msg["imgs"] = msg_lwi;
json msg_hs = json::array();
for (const auto &p : ti.get_heat_sources())
msg_hs.push_back(json(p));
msg["heat_sources"] = msg_hs;
json msg_pt = json::object();
for (const POI &poi : ti.get_poi()) {
msg_pt[poi.name] = int(poi.temp*100)/100.0;
}
msg["poi_temp"] = msg_pt;
std::lock_guard<std::mutex> _(this->usr_mtx);
std::string msg_str = msg.dump();
for(crow::websocket::connection* u : this->users) {
u->send_text(msg_str);
}
}
crow::response Webserver::send_img(const cv::Mat &img, const std::string &ext)
{
lock.lock();
cv::Mat curr_img = img;
lock.unlock();
crow::response res;
res.add_header("Cache-Control", "no-store"); // Images should always be fresh.
std::vector<uchar> img_v;
cv::imencode(ext, img, img_v);
std::string img_s(img_v.begin(), img_v.end());
res.write(img_s);
return res;
}
std::string Webserver::prometheus_metics()
{
this->lock.lock();
std::vector<POI> curr_poi = ti.get_poi();
std::vector<std::pair<std::string,double>> curr_cct = this->cameraComponentTemps;
this->lock.unlock();
std::stringstream ss;
ss << "# TYPE thermocam_point_temp gauge\n";
for (auto p : curr_poi)
ss << "thermocam_point_temp{name=\""<< poi_name <<"\", point=\""<< p.name <<"\"} " << std::fixed << std::setprecision(2) << p.temp << "\n";
ss << "# TYPE thermocam_point_pos_stddev gauge\n";
for (auto p : curr_poi)
ss << "thermocam_point_pos_stddev{name=\""<< poi_name <<"\", point=\""<< p.name <<"\"} " << std::fixed << std::setprecision(4) << p.rolling_std << "\n";
ss << "# TYPE thermocam_temp gauge\n";
for (auto el : cameraComponentTemps)
ss << "thermocam_temp{component=\""<< el.first <<"\"} " << std::fixed << std::setprecision(2) << el.second << "\n";
{
using namespace std::chrono;
steady_clock::time_point now = std::chrono::steady_clock::now();
ss << "# TYPE thermocam_uptime gauge\n";
ss << "thermocam_uptime " << duration_cast<seconds>(now - start_time).count() << "\n";
}
ss << "# TYPE thermocam_frame counter\n";
ss << "thermocam_frame " << frame_cnt << "\n";
ss << "# TYPE thermocam_users gauge\n";
ss << "thermocam_users " << users.size() << "\n";
return ss.str();
}
void Webserver::start()
{
crow::mustache::set_base(".");
app.loglevel(crow::LogLevel::Warning);
CROW_ROUTE(app, "/")
([]{ return index_html; });
CROW_ROUTE(app, "/script.js")
([]{
crow::response res(script_js);
res.set_header("Content-Type", "text/javascript");
return res;
});
CROW_ROUTE(app, "/thermocam-current.jpg")
([this](){return send_img(ti.get_preview());});
CROW_ROUTE(app, "/temperatures.txt")
([this](const crow::request& req, crow::response& res){
this->lock.lock();
std::vector<POI> curr_poi = ti.get_poi();
std::vector<std::pair<std::string,double>> curr_cct = this->cameraComponentTemps;
this->lock.unlock();
sendPOITemp(res, curr_poi);
sendCameraComponentTemps(res, curr_cct);
res.end();
});
CROW_ROUTE(app, "/heat-sources.txt")
([this](const crow::request& req, crow::response& res){
this->lock.lock();
std::vector<HeatSource> curr_heat_sources = ti.get_heat_sources();
this->lock.unlock();
sendHeatSources(res, curr_heat_sources);
res.end();
});
CROW_ROUTE(app, "/points.txt")
([this](const crow::request& req, crow::response& res){
this->lock.lock();
std::vector<POI> curr_poi = ti.get_poi();
std::vector<std::pair<std::string,double>> curr_cct = this->cameraComponentTemps;
std::vector<HeatSource> curr_heat_sources = ti.get_heat_sources();
this->lock.unlock();
sendPOITemp(res, curr_poi);
sendCameraComponentTemps(res, curr_cct);
sendHeatSources(res, curr_heat_sources);
res.end();
});
CROW_ROUTE(app, "/position-std.txt")
([this](const crow::request& req, crow::response& res){
this->lock.lock();
std::vector<POI> curr_poi = ti.get_poi();
this->lock.unlock();
sendPOIPosStd(res, curr_poi);
res.end();
});
CROW_ROUTE(app, "/ws")
.websocket()
.onaccept([&](const crow::request &req) {
std::cout << "New websocket connection from " << req.remoteIpAddress << std::endl;
return true;
})
.onopen([&](crow::websocket::connection& conn){
std::lock_guard<std::mutex> _(this->usr_mtx);
this->users.insert(&conn);
})
.onclose([&](crow::websocket::connection& conn, const std::string& reason){
std::cout << "Websocket connection closed." << std::endl;
std::lock_guard<std::mutex> _(this->usr_mtx);
this->users.erase(&conn);
});
CROW_ROUTE(app, "/uptime.txt")
([this]() {
using namespace std::chrono;
steady_clock::time_point now = std::chrono::steady_clock::now();
return to_string(duration_cast<seconds>(now - start_time).count());
});
CROW_ROUTE(app, "/frame.txt")
([this]() { return to_string(frame_cnt); });
CROW_ROUTE(app, "/users.txt")
([this]() { return to_string(users.size()); });
CROW_ROUTE(app, "/metrics")
([this]() { return prometheus_metics(); });
CROW_ROUTE(app, "/<path>")
([this](const string &path) {
for (const auto &webimg_list : ti.get_webimgs()) {
for (const auto &webimg : webimg_list) {
if (path == webimg.name + ".jpg") {
return send_img(webimg.rgb);
} else if (path == webimg.name + ".tiff") {
return send_img(webimg.mat, ".tiff");
};
}
}
return crow::response(404);
});
app.port(8080)
.multithreaded()
.run();
this->finished = true;
std::cout << "Shutting down webserver thread" << std::endl;
}