实现削峰套利策略的编辑页面

This commit is contained in:
lixiaoyuan
2025-08-26 18:36:25 +08:00
parent 7fe51ea362
commit 8f6c83147b
37 changed files with 1506 additions and 729 deletions

View File

@@ -2,88 +2,215 @@
#include "common/Utils.h"
#include "app/Station.h"
#include "app/Device.h"
#include "app/Policy.h"
#include "database/Dao.h"
#include "common/JsonN.h"
#include "common/Utils.h"
void InitStation(AppData* appdata)
void ElectPeriod::parse(std::string jsonstr)
{
// 读取数据库
NJsonNode jsonroot;
NJson::parse(jsonstr, jsonroot);
NJson::read(jsonroot, "price_super_peak", this->priceSuperPeak);
NJson::read(jsonroot, "price_peak", this->pricePeak);
NJson::read(jsonroot, "price_shoulder", this->priceShoulder);
NJson::read(jsonroot, "price_off_peak", this->priceOffPeak);
NJson::read<std::vector<std::vector<std::string>>>(jsonroot, "periods", this->vecPeriods);
}
std::string ElectPeriod::dump()
{
NJsonNode jsonroot;
jsonroot["price_super_peak"] = this->priceSuperPeak;
jsonroot["price_peak"] = this->pricePeak;
jsonroot["price_shoulder"] = this->priceShoulder;
jsonroot["price_off_peak"] = this->priceOffPeak;
jsonroot["periods"] = this->vecPeriods;
return jsonroot.dump();
}
void AppData::initFromDB()
{
auto dao = DaoEntity::create("");
if (!dao->isConnected())
{
XLOGE() << "Database connected error.";
return;
}
std::string str;
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)
{ // 数据库读取工作模式定义
str = "", result.clear();
DAO::queryWorkModeDef(dao, result);
for (auto& fields: result)
{
auto device = Device::create(fields);
station->addDevice(deviceId, device);
std::string workModeId = fields.value(DMDefWorkMode::WORK_MODE_ID);
std::string name = fields.value(DMDefWorkMode::NAME);
this->mapping.workMode.push_back({workModeId, name});
str += ("工作模式: {" + workModeId + ":" + name + "},");
}
else
XLOGD() << str;
}
{ // 数据库读取策略类型定义
str = "", result.clear();
DAO::queryPolicyTypeDef(dao, result);
for (auto& fields: result)
{
XLOGE() << "init device error: unknown station_id:[" << stationId << "] device_id=" << deviceId;
std::string policyTypeId = fields.value(DMDefPolicyType::POLICY_TYPE_ID);
std::string name = fields.value(DMDefWorkMode::NAME);
this->mapping.workMode.push_back({policyTypeId, name});
str += ("策略类型: {" + policyTypeId + ":" + name + "},");
}
XLOGD() << str;
}
{ // 数据库读取设备类型定义
str = "", result.clear();
DAO::queryDeviceTypeDef(dao, result);
for (auto& fields: result)
{
auto item = std::make_shared<DeviceType>();
item->typeId = fields.get<int>(DMDefDeviceType::DEVICE_TYPE_ID);
item->name = fields.value(DMDefDeviceType::NAME);
item->attrs = fields.value(DMDefDeviceType::ATTRS);
mapDeviceType[item->typeId] = item;
mapping.deviceType.push_back({std::to_string(item->typeId), item->name});
str += ("设备类型: {" + std::to_string(item->typeId) + ":" + item->name + "},");
}
XLOGD() << str;
}
{ // 数据库读取角色定义
str = "", result.clear();
this->mapping.role.clear();
DAO::queryRoleList(dao, result);
for (auto& fields : result)
{
auto item = std::make_shared<Role>();
item->roleId = fields.get<int>(DMRole::ROLE_ID);
item->name = fields.value(DMRole::NAME);
item->isOpen = fields.get<int>(DMRole::IS_OPEN);
mapRole[item->roleId] = item;
mapping.role.push_back({std::to_string(item->roleId), item->name});
str += ("角色: {" + std::to_string(item->roleId) + ":" + item->name + "},");
}
XLOGD() << str;
}
{ // 数据库读取场站信息
str = "", result.clear();
DAO::queryStationList(dao, result);
for (auto& fields: result)
{
int stationId = fields.get<int>(DMStation::STATION_ID);
auto station = std::make_shared<Station>(stationId);
station->name = fields.value(DMStation::NAME);
station->energyCapacity = fields.get<double>(DMStation::CAPACITY);
this->mapStation[stationId] = station;
str += ("场站: {" + std::to_string(stationId) + ":" + station->name + "},");
}
XLOGD() << str;
}
{ // 数据库读取设备信息
str = "", result.clear();
DAO::queryDeviceList(dao, result);
for (auto& fields: result)
{
int deviceId = fields.get<int>(DMDevice::DEVICE_ID);
int stationId = fields.get<int>(DMDevice::STATION_ID);
auto station = this->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)
{ // 数据库读取策略信息
str = "", result.clear();
DAO::queryPolicyList(dao, result);
for (auto& fields: result)
{
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;
auto policy = std::make_shared<MyPolicy>();
policy->policyId = fields.get<int>(DMPolicy::POLICY_ID);
policy->type = fields.get<int>(DMPolicy::TYPE);
policy->name = fields.value(DMPolicy::NAME);
policy->value = fields.value(DMPolicy::VALUE);
this->mapPolicy[policy->policyId] = policy;
}
}
{ // 数据库读取电价分段信息
result.clear();
vecElectPeriods.resize(12);
DAO::exec(dao, "SELECT * FROM configure;", result);
Fields info;
for (auto& fields: result)
{
auto k = fields.value("key");
auto v = fields.value("val");
info.set(k, v);
}
for (int month = 1; month<=12; month++)
{
if (month-1 < vecElectPeriods.size())
{
auto& vecItems = vecElectPeriods[month-1];
std::string str = info.value("period_" + std::to_string(month));
std::vector<std::string> vec;
Utils::split(str, ",", vecItems);
}
}
electPriceSuperPeak = info.get<double>("price_super_peak");
electPricePeak = info.get<double>("price_peak");
electPriceShoulder = info.get<double>("price_shoulder");
electPriceOffPeak = info.get<double>("price_off_peak");
}
{ // 数据库读取统计数据
vector<Fields> result;
std::string curDate = Utils::dateStr();
DAO::queryStatDataList(dao, curDate, curDate, result);
for (auto& fields: result)
{
std::string dt = fields.value(DMStatStation::DT);
int stationId = fields.get<int>(DMStatStation::STATION_ID);
auto station = this->getStation(stationId);
if (station)
{
station->storageIn = fields.get<double>(DMStatStation::STORAGE_ELECT_IN);
station->storageOut = fields.get<double>(DMStatStation::STORAGE_ELECT_OUT);
//station->storageNumIn = fields.getFloat(DMStatStation::STORAGE_NUM);
//station->storageNumOut = fields.getFloat(DMStatStation::STORAGE_NUM);
station->storageNumErr = fields.get<int>(DMStatStation::STORAGE_NUM_ERR);
station->solarGen = fields.get<double>(DMStatStation::SOLAR_ELECT_GEN);
station->solarGrid = fields.get<double>(DMStatStation::SOLAR_ELECT_GRID);
station->solarNumErr = fields.get<int>(DMStatStation::SOLAR_NUM_ERR);
station->chargeElect = fields.get<double>(DMStatStation::CHARGE_ELECT);
station->chargeNum = fields.get<int>(DMStatStation::CHARGE_NUM);
station->chargeNumErr = fields.get<int>(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->initFromDB();
this->initUser();
}
@@ -135,31 +262,7 @@ 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()
@@ -186,7 +289,7 @@ std::vector<std::string> AppData::getStationNames()
return vec;
}
std::vector<std::string> AppData::getDeviceTypes()
std::vector<std::string> AppData::getDeviceTypeNames()
{
std::vector<std::string> vec(mapping.deviceType.size());
int i = 0;
@@ -196,4 +299,61 @@ std::vector<std::string> AppData::getDeviceTypes()
++i;
}
return vec;
}
std::vector<std::string> AppData::getWorkModes()
{
std::vector<std::string> vec(mapWorkMode.size());
int i = 0;
for (auto iter = mapWorkMode.begin(); iter!=mapWorkMode.end(); ++iter)
{
vec[i] = iter->second;
++i;
}
return vec;
}
std::vector<std::string> AppData::getPolicyTypeNames()
{
std::vector<std::string> vec(mapPolicyType.size());
int i = 0;
for (auto iter = mapPolicyType.begin(); iter!=mapPolicyType.end(); ++iter)
{
vec[i] = iter->second;
++i;
}
return vec;
}
std::vector<std::string> AppData::getPolicyNames()
{
std::vector<std::string> vec;
return vec;
}
std::vector<std::string> AppData::getElectPreiodVals(int month)
{
if (month > 0 && month-1 < vecElectPeriods.size())
{
return vecElectPeriods[month-1];
}
return {};
}
std::string AppData::getElectPreiodVal(int month, int hour)
{
if (month > 0 && month-1 < vecElectPeriods.size())
{
auto& vec = vecElectPeriods[month-1];
if (hour > 0 && hour-1 < vec.size())
{
auto& val = vec[hour-1];
if (val == "") return "尖峰";
if (val == "") return "高峰";
if (val == "") return "平段";
if (val == "") return "低谷";
return val;
}
}
return "";
}