first commit
This commit is contained in:
parent
cd20dce210
commit
509684090a
@ -9,9 +9,6 @@ extern std::atomic<bool> g_running;
|
||||
std::string g_dispatch_id;
|
||||
std::mutex g_dispatch_id_mutex;
|
||||
|
||||
auto last_heartbeat = std::chrono::steady_clock::now();
|
||||
auto heartbeat_interval = std::chrono::milliseconds(static_cast<int>(g_app_config.mqtt.keep_alive * 0.9));
|
||||
|
||||
static void send_heartbeat()
|
||||
{
|
||||
if (!mqtt_client || !mqtt_client->isConnected())
|
||||
@ -29,14 +26,9 @@ static void send_heartbeat()
|
||||
}
|
||||
|
||||
nlohmann::json hb_data;
|
||||
hb_data["time"] = Logger::get_current_time_utc8();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_dispatch_id_mutex);
|
||||
hb_data["time"] = Logger::get_current_time_utc8(); // UTC+8
|
||||
if (!g_dispatch_id.empty())
|
||||
hb_data["dispatchId"] = g_dispatch_id;
|
||||
}
|
||||
|
||||
hb_data["status"] = status;
|
||||
|
||||
nlohmann::json msg;
|
||||
@ -209,6 +201,7 @@ static void on_mqtt_message_received(const std::string &topic, const std::string
|
||||
void mqtt_client_thread_func()
|
||||
{
|
||||
const auto &cfg = g_app_config.mqtt;
|
||||
auto heartbeat_interval = std::chrono::milliseconds(static_cast<int>(cfg.mqtt.keep_alive * 0.9));
|
||||
|
||||
while (g_running)
|
||||
{
|
||||
@ -222,8 +215,7 @@ void mqtt_client_thread_func()
|
||||
while (!mqtt_restart_required && g_running)
|
||||
{
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (now - last_heartbeat >= heartbeat_interval)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
send_heartbeat();
|
||||
@ -233,9 +225,8 @@ void mqtt_client_thread_func()
|
||||
LOG_ERROR(std::string("[MQTT] Heartbeat error: ") + e.what());
|
||||
}
|
||||
last_heartbeat = now;
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 轻量轮询
|
||||
std::this_thread::sleep_for(heartbeat_interval); // 固定周期
|
||||
}
|
||||
|
||||
// 需要重启或退出
|
||||
|
||||
Loading…
Reference in New Issue
Block a user