2025-05-19 09:54:33 +08:00
|
|
|
|
#include "Application.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/Utils.h"
|
2025-07-18 09:08:09 +08:00
|
|
|
|
#include "Config.h"
|
|
|
|
|
|
#include "app/Device.h"
|
2025-08-20 19:00:22 +08:00
|
|
|
|
#include "database/DaoEntity.h"
|
|
|
|
|
|
#include "database/Dao.h"
|
|
|
|
|
|
#include "app/Station.h"
|
|
|
|
|
|
#include "app/Device.h"
|
2025-09-01 20:08:40 +08:00
|
|
|
|
#include "protocol/HttpEntity.h"
|
|
|
|
|
|
#include "common/Spdlogger.h"
|
|
|
|
|
|
#include "protocol/MqttEntity.h"
|
2025-08-31 14:38:53 +08:00
|
|
|
|
|
2025-05-19 09:54:33 +08:00
|
|
|
|
void Application::init()
|
|
|
|
|
|
{
|
2025-08-20 19:00:22 +08:00
|
|
|
|
// 初始化系统配置,读取配置文件
|
2025-07-18 09:08:09 +08:00
|
|
|
|
Config::init("assets/config/app.json");
|
|
|
|
|
|
|
2025-09-05 19:44:26 +08:00
|
|
|
|
// MQTT 数据结构
|
|
|
|
|
|
MqttClient::loadDataStruct("assets/config/registeraddr.json");
|
|
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
// 设置数据库配置
|
|
|
|
|
|
DaoEntity::setOption(Config::option.database.host,
|
|
|
|
|
|
Config::option.database.port,
|
|
|
|
|
|
Config::option.database.user,
|
|
|
|
|
|
Config::option.database.passwd,
|
|
|
|
|
|
Config::option.database.dbname);
|
2025-09-01 20:08:40 +08:00
|
|
|
|
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);
|
2025-07-18 09:08:09 +08:00
|
|
|
|
|
|
|
|
|
|
// 连接数据库,读取基础信息
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
// 初始化系统基础数据
|
2025-09-01 20:08:40 +08:00
|
|
|
|
appdata.init();
|
2025-08-20 19:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建设备处理线程
|
2025-07-18 09:08:09 +08:00
|
|
|
|
std::thread([=]() { runThreadDevice(); }).detach();
|
|
|
|
|
|
|
2025-09-01 20:08:40 +08:00
|
|
|
|
// 创建HTTP服务线程
|
|
|
|
|
|
std::thread([=]() {
|
|
|
|
|
|
while (!isQuit) {
|
|
|
|
|
|
HttpEntity http;
|
|
|
|
|
|
http.listen("0.0.0.0", Config::option.http.port); // 阻塞
|
|
|
|
|
|
}
|
|
|
|
|
|
}).detach();
|
|
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
// 创建主业务循环线程
|
|
|
|
|
|
std::thread([=]() { runThreadMain(); }).detach();
|
2025-09-09 19:26:05 +08:00
|
|
|
|
|
|
|
|
|
|
// 统计分析
|
|
|
|
|
|
std::thread([=]() { runThreadStat(); }).detach();
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-19 09:54:33 +08:00
|
|
|
|
|
2025-09-01 20:08:40 +08:00
|
|
|
|
void Application::runThreadDevice()
|
2025-05-19 09:54:33 +08:00
|
|
|
|
{
|
2025-09-01 20:08:40 +08:00
|
|
|
|
while (!isQuit)
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
2025-09-09 19:26:05 +08:00
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-01 20:08:40 +08:00
|
|
|
|
void Application::runThreadMain()
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
2025-09-01 20:08:40 +08:00
|
|
|
|
std::string addr = "tcp://localhost:1883";
|
2025-09-04 19:31:04 +08:00
|
|
|
|
//mqttCli = std::make_shared<MqttClient>();
|
|
|
|
|
|
//mqttCli->init(addr, "ESS", "", "", {});
|
2025-07-18 09:08:09 +08:00
|
|
|
|
|
2025-09-01 20:08:40 +08:00
|
|
|
|
while (!isQuit)
|
|
|
|
|
|
{
|
2025-09-04 19:31:04 +08:00
|
|
|
|
//// 连接场站
|
|
|
|
|
|
//static TimeTick ttStation;
|
|
|
|
|
|
//if (ttStation.elapse(10000))
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if (!mqttCli->isConnected)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// }
|
|
|
|
|
|
// else
|
|
|
|
|
|
// {
|
|
|
|
|
|
// for (auto& item: appdata.mapStation)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// auto station = item.second;
|
|
|
|
|
|
// if (station && !station->isConnected)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// std::string stationCode = station->code;
|
|
|
|
|
|
// std::vector<std::string> vecTopics = {
|
|
|
|
|
|
// "up/json" + stationCode + "/EMS_YX",
|
|
|
|
|
|
// "up/json" + stationCode + "/EMS_YC",
|
|
|
|
|
|
// "up/json" + stationCode + "/PCU_YX",
|
|
|
|
|
|
// "up/json" + stationCode + "/PCU_YC",
|
|
|
|
|
|
// };
|
|
|
|
|
|
// mqttCli->subscribe(vecTopics, [=](int id)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// station->isConnected = (id == 0);
|
|
|
|
|
|
// });
|
|
|
|
|
|
// }
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
2025-05-19 09:54:33 +08:00
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
|
|
}
|
2025-09-09 19:26:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
if (delta >=0 && delta < 86400 && offset <= 10 && n != nCachePos)
|
|
|
|
|
|
{
|
|
|
|
|
|
nCachePos = n;
|
|
|
|
|
|
std::string dt = Utils::dateStr(tDate);
|
|
|
|
|
|
for (auto item: appdata.mapStation)
|
|
|
|
|
|
{
|
|
|
|
|
|
item.second->writeRuntimeData(dt, nCachePos);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
spdlog::info("保存历史数据倒计时: {}", 600 - offset);
|
|
|
|
|
|
}
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
|
|
|
|
|
}
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|