-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.hpp
More file actions
93 lines (80 loc) · 2.96 KB
/
Copy pathutils.hpp
File metadata and controls
93 lines (80 loc) · 2.96 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
#pragma once
#include "config.h"
#include "script_executor.hpp"
#include <string>
#include <sstream>
#include <iomanip>
#include <chrono>
#include <ctime>
#include "logger.hpp"
inline std::string timestamp_now() {
auto now = std::chrono::system_clock::now();
auto t = std::chrono::system_clock::to_time_t(now);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
std::tm tm;
localtime_r(&t, &tm);
std::ostringstream o;
o << std::put_time(&tm, "%Y-%m-%d %H:%M:%S")
<< '.' << std::setfill('0') << std::setw(3) << ms.count();
return o.str();
}
inline std::string hexToIp(const std::string& hex) {
if (hex.length() < 10) return "0.0.0.0";
try {
uint32_t v = std::stoul(hex.substr(2), nullptr, 16);
char buf[16];
std::snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
(v >> 24) & 0xFF,
(v >> 16) & 0xFF,
(v >> 8) & 0xFF,
v & 0xFF);
return buf;
} catch (...) {
return "0.0.0.0";
}
}
inline void execute_script_async(const std::string& cmd, const CameraConfig& cfg, const AlarmEvent* evt=nullptr, const std::string& fp="") {
if (cmd.empty()) return;
std::string r = cmd;
std::time_t now = std::time(nullptr);
std::tm tm;
localtime_r(&now, &tm);
char time_buf[32];
auto rep = [&](const std::string& t, const std::string& v) {
size_t p = 0;
while ((p = r.find(t, p)) != std::string::npos) {
r.replace(p, t.length(), v);
p += v.length();
}
};
std::strftime(time_buf, sizeof(time_buf), "%Y", &tm);
rep("%Y", time_buf);
std::strftime(time_buf, sizeof(time_buf), "%m", &tm);
rep("%m", time_buf);
std::strftime(time_buf, sizeof(time_buf), "%d", &tm);
rep("%d", time_buf);
std::strftime(time_buf, sizeof(time_buf), "%H", &tm);
rep("%H", time_buf);
std::strftime(time_buf, sizeof(time_buf), "%M", &tm);
rep("%M", time_buf);
std::strftime(time_buf, sizeof(time_buf), "%S", &tm);
rep("%S", time_buf);
std::strftime(time_buf, sizeof(time_buf), "%H:%M:%S", &tm);
rep("%T", time_buf);
rep("%{cam_name}", cfg.name);
rep("%{cam_id}", cfg.serialid);
rep("%{cam_ip}", extract_ip_hex_from_rtsp(cfg.rtsp_url));
if (evt) {
rep("%{json_addr}", evt->address);
rep("%{json_chan}", std::to_string(evt->channel));
rep("%{json_desc}", evt->description);
rep("%{json_event}", evt->event_type);
rep("%{json_serialid}", evt->serial_id);
rep("%{json_starttime}", evt->start_time);
rep("%{json_status}", evt->status);
rep("%{json_alarm}", evt->alarm_type);
}
rep("%f", fp);
LOG_DEBUG("SCRIPT", "Submitting to pool: ", r);
ScriptExecutor::instance().submit(std::move(r));
}