-
Notifications
You must be signed in to change notification settings - Fork 1
/
parking_upload_httpservice.cpp
397 lines (302 loc) · 13.1 KB
/
parking_upload_httpservice.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
/**
* Created by zach 2022/11/1
*
* Handle http request to third-party server
*/
#include "util/sion.h"
#include "util/md5.h"
#include "util/timer.h"
#include "util/base64.h"
#include "carplate_main.h"
#include "parking_upload.h"
using namespace sion;
using namespace std;
using Json = nlohmann::json;
extern GPSData g_gpsData;
static const string PASSWORD = "1f76618858J7CXz38nDw3t2A0b200ddfaca";
static const string identity = "szqyws";
static const string BASE_URL = "http://mid.qa.sparkingcd.com/api/rest/" + identity + "/v1";
// parking lot No. for testing
static const string LOT_CODE = "51010400000030107";
static const string SPACE_CODE = "3107010"; // 3107001 ~ 3107054
static const int ERROR_TOKEN = 101;
static char TOKEN[1024] = {0};
char devId[13] = {0};
int retryTime = 0;
Async async_thread_pool;
void startUploadService() {
async_thread_pool.Start();
int8ToHexStr(devSeriNum, 0, 6, devId);
cout << "devId: " << devId << endl;
logfile("devId: %s\n", devId);
std::thread tokenThread(requestToken);
tokenThread.detach();
std::thread heartbeatThread(keepAlive);
heartbeatThread.detach();
std::thread pushTrackingThread(pushTrackingInfo);
pushTrackingThread.detach();
}
Header getHttpHeader(string token) {
Header header;
header.Add("Accept", "application/json");
header.Add("Content-Type", "application/json");
header.Add("Authorization", devId);
if (!token.empty()) {
token.erase(0, 1);
token.erase(token.size() - 1, 1);
header.Add("token", token);
}
return header;
}
string getSign(String params) {
size_t pos = params.find("image_content");
if (pos == string::npos) {
printf("params: %s\n", params.c_str());
}
String sign = md5(params);
//cout << "md5 sign:" << sign.ToLowerCase() << endl;
return sign.ToLowerCase();
}
void requestToken() {
string url = BASE_URL + "/system/token";
this_thread::sleep_for(chrono::milliseconds(10 * 1000));
cout << "requestToken url:" << url << endl;
logfile("requestToken url: %s\n", url.c_str());
String tag = md5(devId + PASSWORD + identity);
long time = getCurrentTimeStamp() / 1000;
cout << "tag: " << tag << ", time: " << dec << time << endl;
while (true) {
try {
Json params;
params["client_tag"] = tag;
params["event_time"] = to_string(time);
url = url + "?client_tag=" + tag + "&event_time=" + to_string(time) + "&sign=" + getSign(params.dump());
PRINT("http_request", "url: %s\n", url.c_str());
auto id = async_thread_pool.Run([=] {
return Request().SetUrl(url).SetHeader(getHttpHeader("")).SetHttpMethod(Method::Get);
});
auto pkg = async_thread_pool.Await(id);
// cout << "AsyncWait: " << pkg.resp.GetHeader().Data().size() << pkg.err_msg << endl;
// cout << "resq status: " << pkg.resp.Status() << endl;
// cout << "AsyncWait resp: " << pkg.resp.StrBody() << endl;
PRINT("", "Http Response:\n");
Json reqObj = Json::parse(pkg.resp.StrBody());
if (reqObj.contains("code")) {
cout << "code: " << reqObj["code"] << endl;
cout << "msg: " << reqObj["msg"] << endl;
}
cout << "token: " << reqObj["token"] << endl;
memset(TOKEN, 0, sizeof(TOKEN));
strcpy(TOKEN, reqObj["token"].dump().c_str());
cout << "expire time: " << reqObj["expire"] << endl;
} catch (const exception& e) {
cerr << e.what() << '\n';
sleep(10);
requestToken();
}
this_thread::sleep_for(chrono::milliseconds(30 * 60 * 1000));
}
}
void keepAlive() {
this_thread::sleep_for(chrono::milliseconds(20 * 1000));
String requestUrl = BASE_URL + "/patrolParking/heart";
while (true) {
try {
PRINT("[keepAlive]", "requestUrl: %s\n", requestUrl.c_str());
logfile("[keepAlive] requestUrl: %s\n", requestUrl.c_str());
long currentTime = getCurrentTimeStamp() / 1000;
Json params;
params["device_id"] = devId;
params["gps_value"] = to_string(g_gpsData.latlon.lon) + "," + to_string(g_gpsData.latlon.lat);
params["event_time"] = to_string(currentTime);
params["status"] = "1";
Payload::FormData form;
form.Append("device_id", devId);
form.Append("gps_value", to_string(g_gpsData.latlon.lon) + "," + to_string(g_gpsData.latlon.lat));
form.Append("event_time", to_string(currentTime));
form.Append("status", "1");
form.Append("sign", getSign(params.dump()));
auto id = async_thread_pool.Run([=] {
return Request().SetUrl(requestUrl).SetHeader(getHttpHeader(TOKEN)).SetHttpMethod(Method::Post).SetBody(form);
});
auto pkg = async_thread_pool.Await(id);
PRINT("", "Http Response:\n");
Json reqObj = Json::parse(pkg.resp.StrBody());
string code = reqObj.value("code", "");
string msg = reqObj.value("msg", "");
cout << "code: " << code << endl;
cout << "msg: " << msg << endl;
logfile("[keepAlive] code: %s, msg: %s\n", code.c_str(), msg.c_str());
if (stoi(code) == ERROR_TOKEN) {
cout << "[keepAlive] retryToken" << endl;
requestToken();
}
} catch (const exception& e) {
cerr << e.what() << '\n';
}
this_thread::sleep_for(chrono::milliseconds(20 * 1000));
}
}
void pushCarImage(ParkingCarMedia carMedia) {
String requestUrl = BASE_URL + "/patrolParking/image";
cout << "pushCarImage requestUrl: " << requestUrl << endl;
logfile("[pushCarImage] requestUrl: %s\n", requestUrl.c_str());
try {
vector<uint8_t> data = readDataFromFile(carMedia.imageUrl);
string image(data.begin(), data.end());
Json params;
params["device_id"] = devId;
params["task_id"] = carMedia.taskID;
params["image_content"] = base64_encode(image, false);
params["event_type"] = to_string(carMedia.eventType);
params["event_time"] = to_string(carMedia.eventTime);
Payload::FormData form;
form.Append("device_id", devId);
form.Append("task_id", carMedia.taskID);
form.Append("image_content", base64_encode(image, false));
form.Append("event_type", to_string(carMedia.eventType));
form.Append("event_time", to_string(carMedia.eventTime));
form.Append("sign", getSign(params.dump()));
cout << "device_id:" << devId << ", task_id:" << carMedia.taskID << ", event_type:" << carMedia.eventType << ", event_time:"
<< carMedia.eventTime << ", sign:" << getSign(params.dump()) << endl;
auto id = async_thread_pool.Run([=] {
return Request().SetUrl(requestUrl).SetHeader(getHttpHeader(TOKEN)).SetHttpMethod(Method::Post).SetBody(form);
});
auto pkg = async_thread_pool.Await(id, 3000);
PRINT("", "Http Response:\n");
Json reqObj = Json::parse(pkg.resp.StrBody());
string code = reqObj.value("code", "");
string msg = reqObj.value("msg", "");
cout << "code: " << code << endl;
cout << "msg: " << msg << endl;
logfile("[pushCarImage] code: %s, msg: %s\n\n", code.c_str(), msg.c_str());
carMedia.printInfo();
int ret = stoi(code);
if (ret == 1) { // request failed
if (retryTime > 3) {
return;
}
retryTime++;
cout << "upload image failed! retry " << retryTime << " times" << endl;
this_thread::sleep_for(chrono::milliseconds(3 * 1000));
pushCarImage(carMedia);
} else if (ret == 0) {
retryTime = 0;
}
if (ret == ERROR_TOKEN) {
cout << "[pushCarImage] retryToken" << endl;
requestToken();
}
} catch (const exception& e) {
cerr << e.what() << '\n';
}
}
void pushParkingEvent(ParkingCarInfo carInfo, ParkingCarMedia carMedia) {
String requestUrl = BASE_URL + "/patrolParking/event";
cout << "pushParkingEvent requestUrl: " << requestUrl << endl;
logfile("[pushParkingEvent] requestUrl: %s\n", requestUrl.c_str());
carInfo.printInfo();
// for (auto& x : carInfo.carNo) {
// cout << hex << x << " ";
// }
// cout << endl;
printf("plate no: %s\n", carInfo.carNo);
char utf8_plate[30] = "";
if (carInfo.plateLen != 0 && carInfo.plateLen != 0xFF) {
std::stringstream ss;
ss << std::hex;
for (auto& b : carInfo.carNo) {
if (b != 0) {
ss << (int)b << " ";
}
}
std::string str = ss.str();
std::cout << str << std::endl;
gbk_to_utf8(carInfo.carNo, utf8_plate, 30);
cout << "utf8 plate no: " << utf8_plate << endl;
}
try {
Json params;
string plate(utf8_plate);
params["device_id"] = devId;
params["space_code"] = SPACE_CODE;
params["car_number"] = plate.empty() ? "" : plate;
params["car_color"] = to_string(carInfo.color);
params["car_type"] = to_string(carInfo.plateType);
params["lot_code"] = LOT_CODE;
params["car_probability"] = to_string(carInfo.carProbability);
params["event_type"] = to_string(carInfo.eventType);
params["event_time"] = carInfo.eventTime;
Payload::FormData form;
form.Append("device_id", devId);
form.Append("space_code", SPACE_CODE);
form.Append("car_number", plate.empty() ? "" : plate);
form.Append("car_color", to_string(carInfo.color));
form.Append("car_type", to_string(carInfo.plateType));
form.Append("lot_code", LOT_CODE);
form.Append("car_probability", to_string(carInfo.carProbability));
form.Append("event_type", to_string(carInfo.eventType));
form.Append("event_time", carInfo.eventTime);
form.Append("sign", getSign(params.dump()));
auto id = async_thread_pool.Run([=] {
return Request().SetUrl(requestUrl).SetHeader(getHttpHeader(TOKEN)).SetHttpMethod(Method::Post).SetBody(form);
});
auto pkg = async_thread_pool.Await(id);
PRINT("", "Http Response:\n");
Json reqObj = Json::parse(pkg.resp.StrBody());
string code = reqObj.value("code", "");
string msg = reqObj.value("msg", "");
string taskID = reqObj.value("task_id", "");
if (!taskID.empty()) {
logfile("start push car image for taskID: %s\n", taskID.c_str());
carMedia.taskID = taskID;
pushCarImage(carMedia);
}
if (stoi(code) == ERROR_TOKEN) {
cout << "[pushParkingEvent] retryToken" << endl;
requestToken();
}
} catch (const exception& e) {
cerr << e.what() << '\n';
}
}
void pushTrackingInfo() {
this_thread::sleep_for(chrono::milliseconds(40 * 1000));
String requestUrl = BASE_URL + "/patrolParking/trajectory";
while (true) {
try {
PRINT("[pushTrackingInfo]", "requestUrl: %s\n", requestUrl.c_str());
logfile("[pushTrackingInfo] requestUrl: %s\n", requestUrl.c_str());
long currentTime = getCurrentTimeStamp() / 1000;
Json params;
params["device_id"] = devId;
params["gps_value"] = to_string(g_gpsData.latlon.lon) + "," + to_string(g_gpsData.latlon.lat);
params["event_time"] = to_string(currentTime);
params["status"] = "1";
Payload::FormData form;
form.Append("device_id", devId);
form.Append("gps_value", to_string(g_gpsData.latlon.lon) + "," + to_string(g_gpsData.latlon.lat));
form.Append("event_time", to_string(currentTime));
form.Append("status", "1");
form.Append("sign", getSign(params.dump()));
auto id = async_thread_pool.Run([=] {
return Request().SetUrl(requestUrl).SetHeader(getHttpHeader(TOKEN)).SetHttpMethod(Method::Post).SetBody(form);
});
auto pkg = async_thread_pool.Await(id);
PRINT("", "Http Response:\n");
Json reqObj = Json::parse(pkg.resp.StrBody());
string code = reqObj.value("code", "");
string msg = reqObj.value("msg", "");
cout << "code: " << code << endl;
cout << "msg: " << msg << endl;
logfile("[pushTrackingInfo] code: %s, msg: %s\n", code.c_str(), msg.c_str());
if (stoi(code) == ERROR_TOKEN) {
cout << "[pushTrackingInfo] retryToken" << endl;
requestToken();
}
} catch (const exception& e) {
cerr << e.what() << '\n';
}
this_thread::sleep_for(chrono::milliseconds(2 * 60 * 1000));
}
}