调试修改MQTT通讯问题

This commit is contained in:
lixiaoyuan
2025-09-17 19:55:59 +08:00
parent 94d6d8a8db
commit c55da0bddc
18 changed files with 661 additions and 494 deletions

View File

@@ -16,6 +16,11 @@ void Application::init()
{
// 初始化系统配置,读取配置文件
Config::init("assets/config/app.json");
if (Config::option.debug)
{
spdlog::set_level(spdlog::level::debug); // 设置全局日志等级为 debug
spdlog::debug("[app] spdlog debug enable.");
}
// MQTT 数据结构
REGAddr::load("assets/config/regaddrs.json");
@@ -77,8 +82,8 @@ void Application::runThreadMain()
if (!this->isInit) { continue; }
}
static TimeTick ttMqtt(1); // 检查 场站的 MQTT 连接
if (ttMqtt.elapse(20))
static TimeTick ttMqtt; // 检查 场站的 MQTT 连接
if (ttMqtt.elapse(30))
{
auto& optionMqtt = Config::option.mqtt;
if (!optionMqtt.host.empty())
@@ -86,22 +91,21 @@ void Application::runThreadMain()
for (auto& item : appdata.mapStation)
{
auto& station = item.second;
if (station && station->isOpen)
if (station)
{
// 该函数检查连接状态,若已经连接,则无操作;若未连接,则进行连接操作
item.second->initMqtt();
// 召测
item.second->polling();
if (station->isOpen)
{
// 该函数检查连接状态,若已经连接,则无操作;若未连接,则进行连接操作
item.second->initMqtt();
// 召测
item.second->polling();
}
// 检查设备的在线状态
station->checkDevice();
}
}
}
}
static TimeTick ttData(1); // 检查数据
if (ttData.elapse(20))
{
//appdata.initFromDB();
}
}
}
@@ -110,20 +114,14 @@ void Application::runThreadStat()
int nCachePos = 0;
while (!isQuit)
{
int64_t tTime = Utils::time();
int64_t tDate = Utils::date();
int64_t delta = tTime-tDate;
int n = delta / 600;
int offset = delta % 600;
bool flagStore = (delta >=0 && delta < 86400 && offset <= 10 && n != nCachePos);
if (flagStore)
static TimeTick ttStat(1);
if(ttStat.elapse(10))
{
nCachePos = n;
std::string dt = Utils::dateStr(tDate);
// // 设备历史数据(电压、电流、功率),存储到 history_day
// 设备历史数据(电压、电流、功率),存储到 history_day
// 统计数据,存储到 stat_station
for (auto item: appdata.mapStation)
{
item.second->writeRuntimeData(dt, nCachePos);
item.second->writeStatistic();
}
}
else
@@ -131,12 +129,6 @@ void Application::runThreadStat()
//spdlog::info("保存历史数据倒计时: {}", 600 - offset);
}
// 统计计算,存储到 stat_station
for (auto& station : appdata.mapStation)
{
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}