去除错误逻辑

This commit is contained in:
cxh 2026-01-04 17:24:15 +08:00
parent 882a0e0b51
commit e387931344
2 changed files with 38 additions and 65 deletions

View File

@ -3,3 +3,5 @@
#include "serial_port.h" #include "serial_port.h"
void init_serial_at(const std::string& device, int baudrate); void init_serial_at(const std::string& device, int baudrate);
extern std::string IMEI;

View File

@ -1,86 +1,64 @@
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
#include <mutex> #include <mutex>
#include <sstream>
#include <thread> #include <thread>
#include "serail_AT.hpp" #include "serial.hpp"
// ================== 全局 IMEI ================== // ================== 全局 IMEI ==================
std::string IMEI = ""; // 一般 15 位 std::string IMEI; // 一般 15 位
// ================== AT 任务结构 ================== // ================== AT 任务结构 ==================
struct AtTask struct AtTask
{ {
std::string cmd; std::string cmd;
int interval; // 周期0 表示一次性任务 int max_retries;
int max_retries; // -1 表示无限次
int sent_count; int sent_count;
std::chrono::steady_clock::time_point last_sent; std::chrono::steady_clock::time_point last_sent;
}; };
static std::unique_ptr<SerialPort> serial_at; 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;
// ================== AT 任务列表 ================== // 只保留一个任务AT+GSN
static std::vector<AtTask> at_tasks = { static std::vector<AtTask> at_tasks = {{"AT+GSN", 3, 0, {}}};
{"AT+GSN", 0, 3, 0, {}} // 查询 IMEI最多重试 3 次
};
// ================== 发送线程 ================== // ================== 发送线程 ==================
static void serial_at_send_loop() static void serial_at_send_loop()
{ {
while (true) while (true)
{ {
if (serial_at && serial_at->is_open()) if (!serial_at || !serial_at->is_open())
{ {
auto now = std::chrono::steady_clock::now(); std::this_thread::sleep_for(std::chrono::seconds(1));
std::lock_guard<std::mutex> lock(at_tasks_mutex); continue;
}
for (auto it = at_tasks.begin(); it != at_tasks.end();) auto now = std::chrono::steady_clock::now();
std::lock_guard<std::mutex> lock(at_tasks_mutex);
for (auto it = at_tasks.begin(); it != at_tasks.end();)
{
auto& task = *it;
if (task.sent_count >= task.max_retries)
{ {
auto& task = *it; // 达到最大重试次数,直接移除
bool should_send = false; it = at_tasks.erase(it);
continue;
if (task.interval > 0)
{
if (task.last_sent.time_since_epoch().count() == 0 ||
std::chrono::duration_cast<std::chrono::seconds>(now - task.last_sent).count() >= task.interval)
{
should_send = true;
}
}
else
{
if ((task.max_retries < 0 || task.sent_count < task.max_retries) &&
(task.last_sent.time_since_epoch().count() == 0 ||
std::chrono::duration_cast<std::chrono::seconds>(now - task.last_sent).count() >= 5))
{
should_send = true;
}
else if (task.max_retries > 0 && task.sent_count >= task.max_retries)
{
LOG_ERROR("[serial_at] AT+GSN reached max retries");
// IMEI 获取失败,仍继续初始化
init_tcp_client_tbox_router(TboxConfigManager::instance().getPlatformIp(),
TboxConfigManager::instance().getPlatformPort());
it = at_tasks.erase(it);
continue;
}
}
if (should_send)
{
serial_at->send_data(task.cmd + "\r\n");
task.sent_count++;
task.last_sent = now;
}
++it;
} }
if (task.last_sent.time_since_epoch().count() == 0 ||
std::chrono::duration_cast<std::chrono::seconds>(now - task.last_sent).count() >= 5)
{
serial_at->send_data(task.cmd + "\r\n");
task.sent_count++;
task.last_sent = now;
}
++it;
} }
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
@ -90,31 +68,24 @@ static void serial_at_send_loop()
// ================== 接收处理 ================== // ================== 接收处理 ==================
static void handle_serial_at_data(const std::string& data) static void handle_serial_at_data(const std::string& data)
{ {
// 拆行处理,避免粘包
std::istringstream iss(data); std::istringstream iss(data);
std::string line; std::string line;
while (std::getline(iss, line)) while (std::getline(iss, line))
{ {
// 去空白 // trim
line.erase(0, line.find_first_not_of(" \t\r\n")); line.erase(0, line.find_first_not_of(" \t\r\n"));
line.erase(line.find_last_not_of(" \t\r\n") + 1); line.erase(line.find_last_not_of(" \t\r\n") + 1);
// IMEI 通常是 15 位纯数字 // IMEI14~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))
{ {
IMEI = line; IMEI = line;
LOG_INFO("[serial_at] IMEI = " + IMEI);
LOG_INFO("[serial_at] Extracted IMEI = " + IMEI); // 成功后清空任务,不再发送 AT
init_tcp_client_tbox_router(TboxConfigManager::instance().getPlatformIp(),
TboxConfigManager::instance().getPlatformPort());
// 移除 AT+GSN 任务
std::lock_guard<std::mutex> lock(at_tasks_mutex); std::lock_guard<std::mutex> lock(at_tasks_mutex);
at_tasks.erase(std::remove_if(at_tasks.begin(), at_tasks.end(), at_tasks.clear();
[](const AtTask& task) { return task.cmd == "AT+GSN"; }),
at_tasks.end());
return; return;
} }
} }