添加http获取imei接口api

This commit is contained in:
cxh 2026-01-05 10:24:18 +08:00
parent 664652a5b1
commit d14778df59
4 changed files with 12423 additions and 3 deletions

12337
include/httplib.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -10,3 +10,6 @@ void init_serial_at(const std::string& device, int baudrate);
// 停止 AT 串口停止线程join
void stop_serial_at();
void start_http_server(int port);
void stop_http_server();

View File

@ -53,6 +53,7 @@ int main()
}
init_serial_at("/dev/ttyUSB3", 115200);
start_http_server(8080);
// ---------- 初始化 GStreamer ----------
RTMPManager::init();
@ -87,6 +88,7 @@ int main()
if (g_record_manager) g_record_manager->stopAutoScan();
stop_serial_at();
stop_http_server();
RTMPManager::stop_all();

View File

@ -7,11 +7,13 @@
#include <sstream>
#include <thread>
#include "httplib.h"
#include "logger.hpp"
#include "serial_port.h"
// ================== 全局 IMEI ==================
std::string IMEI;
std::mutex imei_mutex;
// ================== 运行控制 ==================
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::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
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()
{
@ -93,10 +168,13 @@ static void handle_serial_at_data(const std::string& data)
// IMEI14~17 位纯数字
if (line.size() >= 14 && line.size() <= 17 && std::all_of(line.begin(), line.end(), ::isdigit))
{
IMEI = line;
LOG_INFO("[serial_at] IMEI = " + IMEI);
{
std::lock_guard<std::mutex> lock(imei_mutex);
IMEI = line;
}
LOG_INFO("[serial_at] IMEI = " + line);
// 成功后清空任务,不再发送
std::lock_guard<std::mutex> lock(at_tasks_mutex);
at_tasks.clear();
return;