Files
energy_storage/src/app/Application.cpp

134 lines
3.8 KiB
C++
Raw Normal View History

2025-05-19 09:54:33 +08:00
#include "Application.h"
#include "common/Utils.h"
#include "Config.h"
#include "app/Device.h"
#include "database/DaoEntity.h"
#include "database/Dao.h"
#include "app/Station.h"
#include "app/Device.h"
#include "protocol/HttpEntity.h"
#include "common/Spdlogger.h"
#include "protocol/MqttEntity.h"
2025-09-16 19:38:46 +08:00
#include "DataStruct.h"
2025-08-31 14:38:53 +08:00
2025-05-19 09:54:33 +08:00
void Application::init()
{
// 初始化系统配置,读取配置文件
Config::init("assets/config/app.json");
2025-09-19 18:54:36 +08:00
if (Config::option.debug)
{
spdlog::set_level(spdlog::level::debug); // 设置全局日志等级为 debug
spdlog::debug("[app] spdlog debug enable.");
}
2025-09-05 19:44:26 +08:00
// MQTT 数据结构
2025-09-16 19:38:46 +08:00
REGAddr::load("assets/config/regaddrs.json");
// 设备读取寄存器的地址定义
2025-09-16 19:38:46 +08:00
Device::loadParamAddr("assets/config/regaddrsShow.json");
2025-09-05 19:44:26 +08:00
// 设置数据库配置
DaoEntity::setOption(Config::option.database.host,
Config::option.database.port,
Config::option.database.user,
Config::option.database.passwd,
Config::option.database.dbname);
spdlog::info("[app] set database option: host={}, port={}, user={}, dbname={}",
Config::option.database.host,
Config::option.database.port,
Config::option.database.user,
Config::option.database.dbname);
// 连接数据库,读取基础信息
// 初始化系统基础数据
this->isInit = appdata.init();
// 创建设备处理线程
std::thread([=]() { runThreadDevice(); }).detach();
// 创建HTTP服务线程
std::thread([=]() {
while (!isQuit) {
HttpEntity http;
http.listen("0.0.0.0", Config::option.http.port); // 阻塞
}
}).detach();
// 统计分析
std::thread([=]() { runThreadStat(); }).detach();
// 创建主业务循环线程
std::thread([=]() { runThreadMain(); }).detach();
}
2025-05-19 09:54:33 +08:00
void Application::runThreadDevice()
2025-05-19 09:54:33 +08:00
{
while (!isQuit)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void Application::runThreadMain()
{
while (!isQuit)
{
if (!this->isInit) // 初始化失败
{
2025-09-16 19:38:46 +08:00
std::this_thread::sleep_for(std::chrono::seconds(10));
this->isInit = appdata.init();
if (!this->isInit) { continue; }
}
2025-09-19 18:54:36 +08:00
static TimeTick ttMqtt; // 检查 场站的 MQTT 连接
if (ttMqtt.elapse(30))
{
auto& optionMqtt = Config::option.mqtt;
if (!optionMqtt.host.empty())
{
for (auto& item : appdata.mapStation)
{
2025-09-16 19:38:46 +08:00
auto& station = item.second;
2025-09-19 18:54:36 +08:00
if (station)
{
2025-09-19 18:54:36 +08:00
if (station->isOpen)
{
// 该函数检查连接状态,若已经连接,则无操作;若未连接,则进行连接操作
item.second->initMqtt();
// 召测
item.second->polling();
}
// 检查设备的在线状态
station->checkDevice();
}
}
}
}
}
}
void Application::runThreadStat()
{
int nCachePos = 0;
while (!isQuit)
{
2025-09-19 18:54:36 +08:00
static TimeTick ttStat(1);
if(ttStat.elapse(10))
{
2025-09-19 18:54:36 +08:00
// 设备历史数据(电压、电流、功率),存储到 history_day
// 统计数据,存储到 stat_day
for (auto item: appdata.mapStation)
{
2025-09-19 18:54:36 +08:00
item.second->writeStatistic();
}
}
else
{
//spdlog::info("保存历史数据倒计时: {}", 600 - offset);
}
2025-09-12 18:44:34 +08:00
2025-09-16 19:38:46 +08:00
std::this_thread::sleep_for(std::chrono::seconds(1));
}
2025-05-19 09:54:33 +08:00
}