添加http获取imei接口api
This commit is contained in:
parent
664652a5b1
commit
d14778df59
12337
include/httplib.h
Normal file
12337
include/httplib.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -10,3 +10,6 @@ void init_serial_at(const std::string& device, int baudrate);
|
|||||||
|
|
||||||
// 停止 AT 串口(停止线程,join)
|
// 停止 AT 串口(停止线程,join)
|
||||||
void stop_serial_at();
|
void stop_serial_at();
|
||||||
|
|
||||||
|
void start_http_server(int port);
|
||||||
|
void stop_http_server();
|
||||||
|
|||||||
@ -53,6 +53,7 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
init_serial_at("/dev/ttyUSB3", 115200);
|
init_serial_at("/dev/ttyUSB3", 115200);
|
||||||
|
start_http_server(8080);
|
||||||
|
|
||||||
// ---------- 初始化 GStreamer ----------
|
// ---------- 初始化 GStreamer ----------
|
||||||
RTMPManager::init();
|
RTMPManager::init();
|
||||||
@ -87,6 +88,7 @@ int main()
|
|||||||
if (g_record_manager) g_record_manager->stopAutoScan();
|
if (g_record_manager) g_record_manager->stopAutoScan();
|
||||||
|
|
||||||
stop_serial_at();
|
stop_serial_at();
|
||||||
|
stop_http_server();
|
||||||
|
|
||||||
RTMPManager::stop_all();
|
RTMPManager::stop_all();
|
||||||
|
|
||||||
|
|||||||
@ -7,11 +7,13 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
#include "httplib.h"
|
||||||
#include "logger.hpp"
|
#include "logger.hpp"
|
||||||
#include "serial_port.h"
|
#include "serial_port.h"
|
||||||
|
|
||||||
// ================== 全局 IMEI ==================
|
// ================== 全局 IMEI ==================
|
||||||
std::string IMEI;
|
std::string IMEI;
|
||||||
|
std::mutex imei_mutex;
|
||||||
|
|
||||||
// ================== 运行控制 ==================
|
// ================== 运行控制 ==================
|
||||||
static std::atomic<bool> serial_at_running{false};
|
static std::atomic<bool> serial_at_running{false};
|
||||||
@ -29,9 +31,82 @@ static std::unique_ptr<SerialPort> serial_at;
|
|||||||
static std::thread serial_at_sender;
|
static std::thread serial_at_sender;
|
||||||
static std::mutex at_tasks_mutex;
|
static std::mutex at_tasks_mutex;
|
||||||
|
|
||||||
|
static std::unique_ptr<httplib::Server> http_server;
|
||||||
|
static std::thread http_thread;
|
||||||
|
|
||||||
|
void start_http_server(int port)
|
||||||
|
{
|
||||||
|
http_server = std::make_unique<httplib::Server>();
|
||||||
|
|
||||||
|
http_server->Post("/api/v1/device/register",
|
||||||
|
[](const httplib::Request& req, httplib::Response& res)
|
||||||
|
{
|
||||||
|
LOG_INFO("[http] /device/register called");
|
||||||
|
|
||||||
|
// 解析 A 的信息(可选,先简单点)
|
||||||
|
LOG_INFO("[http] payload: " + req.body);
|
||||||
|
|
||||||
|
if (!is_imei_ready())
|
||||||
|
{
|
||||||
|
res.status = 503;
|
||||||
|
res.set_content(R"({"ok":false,"error":"imei_not_ready"})", "application/json");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string imei = get_imei();
|
||||||
|
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << R"({
|
||||||
|
"ok": true,
|
||||||
|
"server": {
|
||||||
|
"device_type": "tbox",
|
||||||
|
"imei": ")"
|
||||||
|
<< imei << R"(",
|
||||||
|
"fw": "b-v2.1.0"
|
||||||
|
}
|
||||||
|
})";
|
||||||
|
|
||||||
|
res.set_content(oss.str(), "application/json");
|
||||||
|
});
|
||||||
|
|
||||||
|
http_thread = std::thread(
|
||||||
|
[port]()
|
||||||
|
{
|
||||||
|
LOG_INFO("[http] Server listening on port " + std::to_string(port));
|
||||||
|
http_server->listen("0.0.0.0", port);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop_http_server()
|
||||||
|
{
|
||||||
|
if (http_server)
|
||||||
|
{
|
||||||
|
http_server->stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (http_thread.joinable())
|
||||||
|
{
|
||||||
|
http_thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
http_server.reset();
|
||||||
|
}
|
||||||
|
|
||||||
// 只保留一个任务:AT+GSN
|
// 只保留一个任务:AT+GSN
|
||||||
static std::vector<AtTask> at_tasks = {{"AT+GSN", 3, 0, {}}};
|
static std::vector<AtTask> at_tasks = {{"AT+GSN", 3, 0, {}}};
|
||||||
|
|
||||||
|
static bool is_imei_ready()
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(imei_mutex);
|
||||||
|
return !IMEI.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string get_imei()
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(imei_mutex);
|
||||||
|
return IMEI;
|
||||||
|
}
|
||||||
|
|
||||||
// ================== 发送线程 ==================
|
// ================== 发送线程 ==================
|
||||||
static void serial_at_send_loop()
|
static void serial_at_send_loop()
|
||||||
{
|
{
|
||||||
@ -93,10 +168,13 @@ static void handle_serial_at_data(const std::string& data)
|
|||||||
// IMEI:14~17 位纯数字
|
// IMEI:14~17 位纯数字
|
||||||
if (line.size() >= 14 && line.size() <= 17 && std::all_of(line.begin(), line.end(), ::isdigit))
|
if (line.size() >= 14 && line.size() <= 17 && std::all_of(line.begin(), line.end(), ::isdigit))
|
||||||
{
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(imei_mutex);
|
||||||
IMEI = line;
|
IMEI = line;
|
||||||
LOG_INFO("[serial_at] IMEI = " + IMEI);
|
}
|
||||||
|
|
||||||
|
LOG_INFO("[serial_at] IMEI = " + line);
|
||||||
|
|
||||||
// 成功后清空任务,不再发送
|
|
||||||
std::lock_guard<std::mutex> lock(at_tasks_mutex);
|
std::lock_guard<std::mutex> lock(at_tasks_mutex);
|
||||||
at_tasks.clear();
|
at_tasks.clear();
|
||||||
return;
|
return;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user