mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
101 lines
2.9 KiB
C++
101 lines
2.9 KiB
C++
#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"
|
|
|
|
void Application::init()
|
|
{
|
|
// 初始化系统配置,读取配置文件
|
|
Config::init("assets/config/app.json");
|
|
|
|
// 设置数据库配置
|
|
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);
|
|
|
|
// 连接数据库,读取基础信息
|
|
|
|
// 初始化系统基础数据
|
|
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([=]() { runThreadMain(); }).detach();
|
|
}
|
|
|
|
|
|
void Application::runThreadDevice()
|
|
{
|
|
while (!isQuit)
|
|
{
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
}
|
|
|
|
void Application::runThreadMain()
|
|
{
|
|
std::string addr = "tcp://localhost:1883";
|
|
//mqttCli = std::make_shared<MqttClient>();
|
|
//mqttCli->init(addr, "ESS", "", "", {});
|
|
|
|
while (!isQuit)
|
|
{
|
|
//// 连接场站
|
|
//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;
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
}
|
|
} |