first commit
This commit is contained in:
parent
e35890e5f8
commit
61b86ae589
@ -6,7 +6,9 @@ std::atomic<bool> mqtt_restart_required{false};
|
||||
|
||||
extern std::atomic<bool> g_running;
|
||||
|
||||
std::atomic<std::string> g_dispatch_id{""};
|
||||
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));
|
||||
|
||||
@ -28,8 +30,13 @@ static void send_heartbeat()
|
||||
|
||||
nlohmann::json hb_data;
|
||||
hb_data["time"] = Logger::get_current_time_utc8();
|
||||
if (!g_dispatch_id.load().empty())
|
||||
hb_data["dispatchId"] = g_dispatch_id.load();
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_dispatch_id_mutex);
|
||||
if (!g_dispatch_id.empty())
|
||||
hb_data["dispatchId"] = g_dispatch_id;
|
||||
}
|
||||
|
||||
hb_data["status"] = status;
|
||||
|
||||
nlohmann::json msg;
|
||||
@ -74,8 +81,10 @@ static void on_mqtt_message_received(const std::string &topic, const std::string
|
||||
return;
|
||||
}
|
||||
|
||||
// 写入 dispatchId 时加锁
|
||||
if (j["data"].contains("dispatchId"))
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_dispatch_id_mutex);
|
||||
g_dispatch_id = j["data"]["dispatchId"].get<std::string>();
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user