-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
673 lines (642 loc) · 26.9 KB
/
server.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#include <iostream>
#include <dirent.h>
#include <sys/stat.h>
#include <string>
#include <cstring>
#include <vector>
#include "mongoose.h"
#include <json/json.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <map>
#include <boost/thread.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include "Util.h"
#include "log.h"
#include "powermgt.h"
using namespace std;
extern bool g_bForceExit;
extern void SaveLineThresholdThread();
#define ERROR_CODE -1
#define SUCCESS_CODE 0
bool listdir(string dir, vector<string>& vFiles)
{
DIR *dp;
struct dirent *entry;
char childpath[512] = {0};
if((dp = opendir(dir.c_str())) == NULL) {
return false;
}
vFiles.clear();
while((entry = readdir(dp)) != NULL) {
if(entry->d_type & DT_DIR) {
if(strcmp(entry->d_name, ".")==0 || strcmp(entry->d_name, "..")==0)
continue;
sprintf(childpath,"%s/%s", dir.c_str(), entry->d_name);
listdir(childpath, vFiles);
}
if(entry->d_type & DT_REG) {
sprintf(childpath,"%s/%s", dir.c_str(), entry->d_name);
vFiles.push_back(childpath);
}
}
closedir(dp);
return true;
}
static int process_req(struct mg_connection *conn) {
Json::Value out;
//printf("url = %s, content = %s\n", conn->uri, conn->content);
if (strcmp(conn->uri, "/test") == 0) {
out["test"] = "ok";
} else if (strcmp(conn->uri, "/connect") == 0) {
Json::Value message;
zone z = CUtil::GetMonitorZone();
message["info"]["init"]["zone"]["up"] = z.up;
message["info"]["init"]["zone"]["down"] = z.down;
message["info"]["init"]["zone"]["left"] = z.left;
message["info"]["init"]["zone"]["right"] = z.right;
message["info"]["init"]["alarm_threshold"]["width"] = z.alarm_threshold_width;
message["info"]["init"]["alarm_threshold"]["height"] = z.alarm_threshold_height;
message["info"]["init"]["alarm_voltage"] = CUtil::ini_query_float("init", "alarm_voltage", -1);
configure c = CUtil::GetConfigure();
message["info"]["configure"]["communicator_server"] = c.communicator_server;
message["info"]["configure"]["tower_name"] = c.tower_name;
message["info"]["configure"]["risk_count"] = c.risk_count;
message["info"]["configure"]["check_interval"] = c.check_interval;
message["info"]["configure"]["check_interval_night"] = c.check_interval_night;
message["info"]["configure"]["morning"] = c.morning;
message["info"]["configure"]["night"] = c.night;
message["info"]["configure"]["scenario_mode"] = c.scenario_mode;
status s = CUtil::GetStatus();
message["info"]["state"]["wifi"] = s.wifi;
message["info"]["state"]["3G"] = s._3G;
message["info"]["state"]["camera0"] = s.camera0;
message["info"]["state"]["camera1"] = s.camera1;
message["info"]["state"]["sms"] = s.sendsms;
message["info"]["state"]["camera0_power"] = s.camera0_power;
message["info"]["state"]["camera1_power"] = s.camera1_power;
message["info"]["state"]["alarm"] = s.alarm;
battery b = CUtil::GetBattery();
message["info"]["state"]["battery"]["solar_voltage"] = b.solar_voltage;
message["info"]["state"]["battery"]["battery_voltage"] = b.battery_voltage;
message["info"]["state"]["battery"]["charging_current"] = b.charging_current;
message["info"]["state"]["battery"]["discharing_current"] = b.discharging_current;
message["info"]["state"]["battery"]["llumination_intensity"] = b.illumination_intensity;
GPS g = CUtil::GetGPS();
message["info"]["GPS"]["latitude"] = g.latitude;
message["info"]["GPS"]["longitude"] = g.longitude;
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "connect success.";
out["message"] = message;
}
else if(strcmp(conn->uri, "/get_tower_name") == 0) {
Json::Value message;
message["tower_name"] = CUtil::GetLocation();
message["device_id"] = CUtil::GetBoardID();
message["rtsp0"] = "rtsp://192.168.11.1:8554/h264ESVideoTest";
message["rtsp1"] = "rtsp://192.168.11.1:554/proxyStream";
out["message"] = message ;
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "set success.";
}
else if(strcmp(conn->uri, "/set_tower_name") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body))
{
if (body.isMember("tower_name"))
{
string tower_name = body["tower_name"].asCString();
CUtil::SaveLocation(tower_name);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "set success.";
out["message"] = body;
}
else {
out["error_code"] = ERROR_CODE;
out["error_message"] = "json format invalid.";
out["message"] = message;
}
}
else {
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = message;
}
}
else if(strcmp(conn->uri, "/reportgps") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body))
{
if (body.isMember("longitude") && body.isMember("latitude"))
{
GPS gps;
gps.longitude = body["longitude"].asCString();
gps.latitude = body["latitude"].asCString();
CUtil::SetGPS(gps);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "set success.";
out["message"] = body;
}
else {
out["error_code"] = ERROR_CODE;
out["error_message"] = "json format invalid.";
out["message"] = message;
}
}
else {
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = message;
}
}
else if (strcmp(conn->uri, "/initconf") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body))
{
if (body.isMember("zone") &&
body["zone"].isMember("up") && body["zone"]["up"].isString() &&
body["zone"].isMember("down") && body["zone"]["down"].isString() &&
body["zone"].isMember("left") && body["zone"]["left"].isString() &&
body["zone"].isMember("right") && body["zone"]["right"].isString())
{
zone z;
z.up = body["zone"]["up"].asCString();
z.down = body["zone"]["down"].asCString();
z.left = body["zone"]["left"].asCString();
z.right = body["zone"]["right"].asCString();
z.alarm_threshold_width = body["alarm_threshold"]["width"].asCString();
z.alarm_threshold_height = body["alarm_threshold"]["height"].asCString();
CUtil::SetMonitorZone(z);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "initconf success.";
out["message"] = body;
}
else {
out["error_code"] = ERROR_CODE;
out["error_message"] = "json format invalid.";
out["message"] = message;
}
}
else {
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = message;
}
}
else if (strcmp(conn->uri, "/algorithm_threshold") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body) && body.isMember("ground_threshold") && body.isMember("line_threshold"))
{
double threshold1 = body["ground_threshold"].asDouble();
double threshold2 = body["line_threshold"].asDouble();
CUtil::ini_save_float("init", "ground_threshold", threshold1);
CUtil::ini_save_float("init", "line_threshold_delta", threshold2);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "set threshold success.";
out["message"]["ground_threshold"] = threshold1;
out["message"]["line_threshold"] = threshold2;
}else{
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = "";
}
}
else if (strcmp(conn->uri, "/alarm_voltage") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body) && body.isMember("alarm_voltage"))
{
double voltage = body["alarm_voltage"].asDouble();
CUtil::ini_save_float("init", "alarm_voltage", voltage);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "set alarm voltage success.";
out["message"]["alarm_voltage"] = CUtil::ini_query_float("init", "alarm_voltage", -1);
}else{
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = "";
}
}
else if (strcmp(conn->uri, "/paramconf") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body))
{
if (body.isMember("type") && string("common").compare(body["type"].asCString()) == 0 &&
body.isMember("data") &&
body["data"].isMember("communicator_server") && body["data"]["communicator_server"].isString() &&
body["data"].isMember("risk_count") && body["data"]["risk_count"].isString() &&
body["data"].isMember("check_interval") && body["data"]["check_interval"].isString() &&
body["data"].isMember("check_interval_night") && body["data"]["check_interval_night"].isString() &&
body["data"].isMember("morning") && body["data"]["morning"].isString() &&
body["data"].isMember("night") && body["data"]["night"].isString() &&
body["data"].isMember("tower_name") && body["data"]["tower_name"].isString())
{
configure c;
c.tower_name = body["data"]["tower_name"].asCString();
c.risk_count = body["data"]["risk_count"].asCString();
c.check_interval = body["data"]["check_interval"].asCString();
c.check_interval_night = body["data"]["check_interval_night"].asCString();
c.communicator_server = body["data"]["communicator_server"].asCString();
c.morning = body["data"]["morning"].asCString();
c.night = body["data"]["night"].asCString();
c.scenario_mode = body["data"]["scenario_mode"].asCString();
CUtil::SetConfigure(c);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "paramconf success.";
out["message"] = body;
}
else {
out["error_code"] = ERROR_CODE;
out["error_message"] = "json format invalid.";
out["message"] = message;
}
}
else {
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = message;
}
}
else if (strcmp(conn->uri, "/query") == 0) {
Json::Value message;
message["info"]["tower_name"] = CUtil::GetLocation();
message["info"]["device_id"] = CUtil::GetBoardID();
string rtsp = "rtsp://";
string http = "http://";
string ip = CUtil::GetIP();
message["info"]["ip"] = ip;
message["info"]["http"] = http + ip + ":8080/";
message["info"]["rtsp0"] = rtsp + ip + ":8554/h264ESVideoTest";
message["info"]["rtsp1"] = rtsp + ip + ":554/proxyStream";
status s = CUtil::GetStatus();
message["status"]["wifi"] = s.wifi;
message["status"]["3G"] = s._3G;
message["status"]["camera0"] = s.camera0;
message["status"]["camera1"] = s.camera1;
message["status"]["camera0_power"] = s.camera0_power;
message["status"]["camera1_power"] = s.camera1_power;
message["status"]["sms"] = s.sendsms;
message["status"]["alarm"] = s.alarm;
battery b = CUtil::GetBattery();
message["battery"]["solar_voltage"] = b.solar_voltage;
message["battery"]["battery_voltage"] = b.battery_voltage;
message["battery"]["charging_current"] = b.charging_current;
message["battery"]["discharing_current"] = b.discharging_current;
message["battery"]["llumination_intensity"] = b.illumination_intensity;
message["battery"]["alarm_voltage"] = CUtil::ini_query_float("init", "alarm_voltage", -1);
GPS g = CUtil::GetGPS();
message["GPS"]["latitude"] = g.latitude;
message["GPS"]["longitude"] = g.longitude;
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "query success";
out["message"] = message;
}
else if (strcmp(conn->uri, "/restart") == 0) {
g_bForceExit = true;
//system("./restart &");
SendRestartOSCmd(2000);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "restart success";
out["message"] = "";
}
else if (strcmp(conn->uri, "/wifi_ap") == 0) {
bool enable = false;
Json::Value message;
char buffer[10];
mg_get_var(conn, "enable", buffer, sizeof(buffer));
strcmp(buffer, "true") == 0 ? enable = true : enable = false;
if(enable) CUtil::OpenAP();
else CUtil::CloseAP();
message["enable"] = CUtil::IsAPEnabled() ? "1" : "0";
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "wifi ap config success";
out["message"] = message;
}
else if (strcmp(conn->uri, "/sms") == 0) {
bool enable = false;
Json::Value message;
char buffer[10];
mg_get_var(conn, "enable", buffer, sizeof(buffer));
strcmp(buffer, "true") == 0 ? enable = true : enable = false;
CUtil::SetSendSMSStatus(enable);
message["enable"] = CUtil::GetSendSMSStatus() ? "1" : "0";
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "sms config success";
out["message"] = message;
}
else if (strcmp(conn->uri, "/camera_power") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body) && body.isMember("uuid") &&
body.isMember("index") && body.isMember("poweron"))
{
int index = body["index"].asInt();
string uuid = body["uuid"].asCString();
bool poweron = body["poweron"].asBool();
if(poweron)
_open(uuid, index, false);
else
_close(uuid, index);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "success";
out["message"] = "";
}else{
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = "";
}
}
else if (strcmp(conn->uri, "/check_time_period") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body) && body.isMember("night") &&
body.isMember("morning"))
{
int morning = body["morning"].asInt();
int night = body["night"].asInt();
CUtil::SetNightInt(night);
CUtil::SetMorningInt(morning);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "success";
out["message"] = "";
}else{
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = "";
}
}
else if (strcmp(conn->uri, "/camera_power_query") == 0) {
Json::Value message;
status s = CUtil::GetStatus();
message["camera0_power"] = s.camera0_power;
message["camera1_power"] = s.camera1_power;
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "success";
out["message"] = message;
}
else if (strcmp(conn->uri, "/alarm") == 0) {
bool enable = false;
Json::Value message;
char buffer[10] = {0};
mg_get_var(conn, "enable", buffer, sizeof(buffer));
strcmp(buffer, "true") == 0 ? enable = true : enable = false;
CUtil::SetAlarmStatus(enable);
message["enable"] = CUtil::GetAlarmStatus() ? "1" : "0";
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "alarm config success";
out["message"] = message;
}
else if (strcmp(conn->uri, "/force_alarm") == 0) {
extern bool g_ForceAlarm;
Json::Value message;
g_ForceAlarm = true;
USER_PRINT(">> force alarm later.\n");
message["enable"] = g_ForceAlarm ? "1" : "0";
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "force alarm config success";
out["message"] = message;
}
else if (strcmp(conn->uri, "/force_train_bg") == 0) {
extern bool g_bInitModel;
Json::Value message;
g_bInitModel = false;
USER_PRINT(">> force train background later.\n");
message["enable"] = g_bInitModel ? "1" : "0";
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "force train background success";
out["message"] = message;
}
else if (strcmp(conn->uri, "/phonenumber") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body) && body.isMember("type"))
{
if(strcmp(body["type"].asCString(), "add") == 0)
{
Json::Value val_array = body["list"];
for(int i = 0; i < val_array.size(); i++)
CUtil::SavePhoneNumber(val_array[i].asCString());
}
else if(strcmp(body["type"].asCString(), "remove") == 0)
{
Json::Value val_array = body["list"];
for(int i = 0; i < val_array.size(); i++)
CUtil::RemovePhoneNumber(val_array[i].asCString());
}
vector<string> v = CUtil::GetPhoneNumber();
for(int i = 0; i < v.size(); i++)
message["list"].append(v.at(i));
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "success";
out["message"] = message;
}else{
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = "";
}
}
else if (strcmp(conn->uri, "/alarmcount") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body) && body.isMember("alarm_count"))
{
int alarm_count = body["alarm_count"].asInt();
CUtil::SetAlarmCount(alarm_count);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "set alarm time success.";
out["message"]["alarm_count"] = alarm_count;
}else{
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = "";
}
}
else if (strcmp(conn->uri, "/riskcount") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body) && body.isMember("risk_count"))
{
int alarm_count = body["risk_count"].asInt();
CUtil::SetRiskCount(alarm_count);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "set risk time success.";
out["message"]["risk_count"] = alarm_count;
}else{
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = "";
}
}
else if (strcmp(conn->uri, "/checkinterval") == 0) {
Json::Value message, body;
Json::Reader reader;
if (reader.parse(conn->content, body) && body.isMember("check_interval") &&
body.isMember("check_interval_night"))
{
int interval = body["check_interval"].asInt();
int interval_night = body["check_interval_night"].asInt();
CUtil::SetCheckInterval(interval);
CUtil::SetCheckInterval_Night(interval_night);
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "set risk check interval success.";
out["message"]["interval"] = interval;
out["message"]["interval_night"] = interval_night;
}else{
out["error_code"] = ERROR_CODE;
out["error_message"] = "parse json failed.";
out["message"] = "";
}
}
else if (strcmp(conn->uri, "/PTZ") == 0) {
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "success.";
Json::Value message;
string cmd;
char buffer[20] = {0};
char speed[10] = {0};
mg_get_var(conn, "action", buffer, sizeof(buffer));
mg_get_var(conn, "speed", speed, sizeof(speed));
cmd = "./ptz ";
cmd += buffer;
cmd += " ";
cmd += speed;
USER_PRINT("ptz cmd = %s\n", cmd.c_str());
message["action"] = buffer;
message["speed"] = speed;
out["message"] = message;
system(cmd.c_str());
if(strcmp("set_preset", buffer) == 0)
boost::thread thd(&SaveLineThresholdThread);
}
else if (strcmp(conn->uri, "/takephoto") == 0) {
char buffer[10] = {0};
char* cmd = NULL;
mg_get_var(conn, "camera", buffer, sizeof(buffer));
if(strcmp(buffer, "0") == 0)
{
cmd = "./capturejpg camera0.jpg > /dev/null 2>&1";
system(cmd);
mg_send_file(conn, "camera0.jpg", NULL);
remove("camera0.jpg");
return MG_MORE;
}
else if(strcmp(buffer, "1") == 0)
{
cmd = "./rtsp2jpg rtsp://192.168.1.60:554/1/h264minor camera1.jpg > /dev/null 2>&1";
system(cmd);
mg_send_file(conn, "camera1.jpg", NULL);
return MG_MORE;
}
else
{
return MG_FALSE;
}
}
else if(strcmp(conn->uri, "/updateProxyUrl") == 0) {
char buffer[1024];
Json::Value ret;
std::string strRet, strLocalAddr, strLocalAddrPort, strPublickUrl, strPublickUrlPort, strProtocol;
mg_get_var(conn, "local_addr", buffer, sizeof(buffer));
strLocalAddr = buffer;
strLocalAddrPort = strLocalAddr.substr(strLocalAddr.find_last_of(':')+1);
mg_get_var(conn, "public_url", buffer, sizeof(buffer));
strPublickUrl = buffer;
strPublickUrlPort = strPublickUrl.substr(strPublickUrl.find_last_of(':')+1);
mg_get_var(conn, "protocol", buffer, sizeof(buffer));
strProtocol = buffer;
if (strProtocol == "tcp" && strLocalAddrPort == "22")
CUtil::ini_save_string("proxy", "ssh", strPublickUrl.c_str());
if (strProtocol == "http" && strLocalAddrPort == "8080")
CUtil::ini_save_string("proxy", "http", strPublickUrl.c_str());
if (strProtocol == "tcp" && strLocalAddrPort == "8554")
{
std::string rtsp = strPublickUrl;
rtsp.replace(0, 3, "rtsp");
rtsp += "/h264ESVideoTest";
CUtil::ini_save_string("proxy", "rtsp0", rtsp.c_str());
}
if (strProtocol == "tcp" && strLocalAddrPort == "554")
{
std::string rtsp = strPublickUrl;
rtsp.replace(0, 3, "rtsp");
rtsp += "/proxyStream";
CUtil::ini_save_string("proxy", "rtsp1", rtsp.c_str());
}
ret["local_addr"] = strLocalAddr;
ret["public_url"] = strPublickUrl;
ret["protocol"] = strProtocol;
strRet = ret.toStyledString();
USER_PRINT(">>>>>>>>>>>>>>>>>>>>updateProxyUrl = \n%s\n", strRet.c_str());
mg_send_header(conn, "Content-Type", "application/json;charset=utf-8");
mg_printf_data(conn, strRet.c_str(), strlen(strRet.c_str()));
return MG_TRUE;
}
else if (strcmp(conn->uri, "/getphoto") == 0) {
char category[10] = {0}, filepath[255] = {0};
char* cmd = NULL;
mg_get_var(conn, "category", category, sizeof(category));
if(strcmp(category, "all") == 0)
{
vector<string> vFiles;
out["error_code"] = SUCCESS_CODE;
out["error_message"] = "success.";
Json::Value message;
listdir("/mnt/tf", vFiles);
for(int i = 0; i < vFiles.size(); i++)
message["files"].append(vFiles.at(i));
out["message"] = message;
}
else if(strcmp(category, "single") == 0)
{
mg_get_var(conn, "filepath", filepath, sizeof(filepath));
if(!boost::algorithm::starts_with(filepath, "/mnt/tf"))
{
return MG_FALSE;
}
mg_send_file(conn, filepath, NULL);
return MG_MORE;
}
else
{
return MG_FALSE;
}
}
else {
USER_PRINT("invalid url request, url = %s\n", conn->uri);
out["error_code"] = ERROR_CODE;
out["error_message"] = "invalid http request.";
}
mg_send_header(conn, "Content-Type", "application/json;charset=utf-8");
mg_send_data(conn, out.toStyledString().c_str(), strlen(out.toStyledString().c_str()));
return MG_TRUE;
}
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
if (ev == MG_REQUEST) {
return process_req(conn);
}
else if (ev == MG_AUTH) {
return MG_TRUE;
}
else {
return MG_FALSE;
}
}
void server_thread()
{
struct mg_server *server = mg_create_server(NULL, ev_handler);
mg_set_option(server, "document_root", ".");
mg_set_option(server, "listening_port", "8080"); // Open port 8080
USER_PRINT("Starting server on port %s\n", mg_get_option(server, "listening_port"));
for (;;) {
mg_poll_server(server, 1000); // Infinite loop, Ctrl-C to stop
if(g_bForceExit)
break;
}
mg_destroy_server(&server);
USER_PRINT("=====>>>>>server thread exit.\n");
}