2025-08-20 19:00:22 +08:00
|
|
|
|
#include "AppData.h"
|
2025-08-22 19:06:50 +08:00
|
|
|
|
#include "common/Utils.h"
|
2025-08-20 19:00:22 +08:00
|
|
|
|
#include "app/Station.h"
|
2025-08-22 19:06:50 +08:00
|
|
|
|
#include "app/Device.h"
|
|
|
|
|
|
#include "database/Dao.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InitStation(AppData* appdata)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 读取数据库
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
DAO::queryStationList(NULL, result);
|
|
|
|
|
|
for (auto& fields: result)
|
|
|
|
|
|
{
|
|
|
|
|
|
int stationId = fields.getInt(DMStation::STATION_ID);
|
|
|
|
|
|
auto station = std::make_shared<Station>(stationId);
|
|
|
|
|
|
station->name = fields.value(DMStation::NAME);
|
|
|
|
|
|
station->energyCapacity = fields.getDouble(DMStation::CAPACITY);
|
|
|
|
|
|
appdata->mapStation[stationId] = station;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InitDevice(AppData* appdata)
|
|
|
|
|
|
{
|
|
|
|
|
|
vector<Fields> result;
|
|
|
|
|
|
DAO::queryDeviceList(NULL, 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)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string curDate = Utils::dateStr();
|
|
|
|
|
|
vector<Fields> result;
|
|
|
|
|
|
DAO::queryStatDataList(curDate, curDate, result);
|
|
|
|
|
|
for (auto& fields: result)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string dt = fields.value(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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AppData::init()
|
|
|
|
|
|
{
|
|
|
|
|
|
// 初始化场站信息
|
|
|
|
|
|
InitStation(this);
|
|
|
|
|
|
// 读取设备信息,连接设备
|
|
|
|
|
|
InitDevice(this);
|
|
|
|
|
|
// 读取基础统计信息,在系统总览中需要展示
|
|
|
|
|
|
InitStatData(this);
|
|
|
|
|
|
|
|
|
|
|
|
this->initUser();
|
|
|
|
|
|
}
|
2025-08-20 19:00:22 +08:00
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Station> AppData::getStation(int stationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto iter = mapStation.find(stationId);
|
|
|
|
|
|
if (iter!=mapStation.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
return iter->second;
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Station> AppData::getStationByName(std::string name)
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto iter = mapStation.begin(); iter!=mapStation.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (iter->second->name == name)
|
|
|
|
|
|
{
|
|
|
|
|
|
return iter->second;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Device> AppData::getDevice(int stationId, int deviceId)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto station = getStation(stationId);
|
|
|
|
|
|
if (station)
|
|
|
|
|
|
{
|
|
|
|
|
|
return station->getDevice(deviceId);
|
|
|
|
|
|
}
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-22 19:06:50 +08:00
|
|
|
|
std::unordered_map<int, std::shared_ptr<DeviceType>>& AppData::getDeviceTypeDef()
|
|
|
|
|
|
{
|
|
|
|
|
|
return mapDeviceType;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 19:00:22 +08:00
|
|
|
|
void AppData::loadStatData()
|
|
|
|
|
|
{
|
2025-08-22 19:06:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AppData::initUser()
|
|
|
|
|
|
{
|
|
|
|
|
|
auto dao = DaoEntity::create("");
|
|
|
|
|
|
std::vector<Fields> result;
|
|
|
|
|
|
|
|
|
|
|
|
// 数据库读取角色定义
|
|
|
|
|
|
mapping.role.clear();
|
|
|
|
|
|
DAO::queryRoleList(dao, result);
|
|
|
|
|
|
for (auto& fields : result)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto item = std::make_shared<Role>();
|
|
|
|
|
|
item->roleId = fields.getInt(DMRole::ROLE_ID);
|
|
|
|
|
|
item->name = fields.value(DMRole::NAME);
|
|
|
|
|
|
item->isOpen = fields.getInt(DMRole::IS_OPEN);
|
|
|
|
|
|
mapRole[item->roleId] = item;
|
|
|
|
|
|
mapping.role.push_back({std::to_string(item->roleId), item->name});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 数据库读取设备类型定义
|
|
|
|
|
|
result.clear();
|
|
|
|
|
|
DAO::queryDeviceTypeDef(dao, result);
|
|
|
|
|
|
for (auto& fields : result)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto item = std::make_shared<DeviceType>() ;
|
|
|
|
|
|
item->typeId = fields.getInt(DMDeviceTypeDef::TYPE_ID);
|
|
|
|
|
|
item->name = fields.value(DMDeviceTypeDef::NAME);
|
|
|
|
|
|
item->attrs = fields.value(DMDeviceTypeDef::ATTRS);
|
|
|
|
|
|
mapDeviceType[item->typeId] = item;
|
|
|
|
|
|
mapping.deviceType.push_back({std::to_string(item->typeId), item->name});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> AppData::getRoleNames()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::string> vec(mapRole.size());
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
for (auto iter = mapRole.begin(); iter!=mapRole.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
vec[i] = iter->second->name;
|
|
|
|
|
|
++i;
|
|
|
|
|
|
}
|
|
|
|
|
|
return vec;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> AppData::getStationNames()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::string> vec(mapStation.size());
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
for (auto iter = mapStation.begin(); iter!=mapStation.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
vec[i] = iter->second->name;
|
|
|
|
|
|
++i;
|
|
|
|
|
|
}
|
|
|
|
|
|
return vec;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> AppData::getDeviceTypes()
|
|
|
|
|
|
{
|
|
|
|
|
|
std::vector<std::string> vec(mapping.deviceType.size());
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
for (auto iter = mapping.deviceType.begin(); iter!=mapping.deviceType.end(); ++iter)
|
|
|
|
|
|
{
|
|
|
|
|
|
vec[i] = iter->second;
|
|
|
|
|
|
++i;
|
|
|
|
|
|
}
|
|
|
|
|
|
return vec;
|
2025-08-20 19:00:22 +08:00
|
|
|
|
}
|