Files
energy_storage/src/app/Application.cpp

145 lines
4.2 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"
void InitStation()
{
AppData& appdata = Application::instance().getAppData();
// 读取数据库
std::vector<DataFields> result;
DAO::queryStationList(result);
for (auto& fields: result)
{
int stationId = fields.getInt(DMStation::STATION_ID);
auto station = std::make_shared<Station>(stationId);
station->name = fields.getStr(DMStation::NAME);
station->energyCapacity = fields.getDouble(DMStation::CAPACITY);
appdata.mapStation[stationId] = station;
}
}
void InitDevice()
{
AppData& appdata = Application::instance().getAppData();
vector<DataFields> result;
DAO::queryDeviceList(result);
for (auto& fields: result)
{
int deviceId = fields.getInt(DMDevice::DEVICE_ID);
int stationId = fields.getInt(DMDevice::STATION_ID);
auto station = appdata.getStation(stationId);
if (station)
{
auto device = Device::create(fields);
station->addDevice(deviceId, device);
}
else
{
XLOGE() << "init device error: unknown station_id:[" << stationId << "] device_id=" << deviceId;
}
}
}
void InitStatData()
{
AppData& appdata = Application::instance().getAppData();
std::string curDate = Utils::dateStr();
vector<DataFields> result;
DAO::queryStatDataList(curDate, curDate, result);
for (auto& fields: result)
{
std::string dt = fields.getStr(DMStatStation::DT);
int stationId = fields.getInt(DMStatStation::STATION_ID);
auto station = appdata.getStation(stationId);
if (station)
{
station->storageIn = fields.getFloat(DMStatStation::STORAGE_ELECT_IN);
station->storageOut = fields.getFloat(DMStatStation::STORAGE_ELECT_OUT);
//station->storageNumIn = fields.getFloat(DMStatStation::STORAGE_NUM);
//station->storageNumOut = fields.getFloat(DMStatStation::STORAGE_NUM);
station->storageNumErr = fields.getFloat(DMStatStation::STORAGE_NUM_ERR);
station->solarGen = fields.getFloat(DMStatStation::SOLAR_ELECT_GEN);
station->solarGrid = fields.getFloat(DMStatStation::SOLAR_ELECT_GRID);
station->solarNumErr = fields.getFloat(DMStatStation::SOLAR_NUM_ERR);
station->chargeElect = fields.getFloat(DMStatStation::CHARGE_ELECT);
station->chargeNum = fields.getFloat(DMStatStation::CHARGE_NUM);
station->chargeNumErr = fields.getFloat(DMStatStation::CHARGE_NUM_ERR);
}
else
{
XLOGE() << "init staticis data error: unknown station_id:[" << stationId << "] dt=" << dt;
}
}
}
2025-05-19 09:54:33 +08:00
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);
XLOGI() << "[APP] set database option: host=" << Config::option.database.host
<< ", port=" << Config::option.database.port
<< ", user=" << Config::option.database.user
<< ", dbname=" << Config::option.database.dbname;
// 连接数据库,读取基础信息
// 初始化场站信息
InitStation();
// 读取设备信息,连接设备
InitDevice();
// 读取基础统计信息,在系统总览中需要展示
InitStatData();
// 创建设备处理线程
std::thread([=]() { runThreadDevice(); }).detach();
// 创建主业务循环线程
std::thread([=]() { runThreadMain(); }).detach();
}
AppData& Application::getAppData()
2025-08-19 17:16:54 +08:00
{
return appdata_;
2025-05-19 09:54:33 +08:00
}
void Application::runThreadMain()
{
while (!isQuit())
{
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
void Application::runThreadDevice()
{
while (!isQuit())
{
2025-05-19 09:54:33 +08:00
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
2025-05-19 09:54:33 +08:00
}