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

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 "";
}

View File

@@ -8,6 +8,7 @@
class Station;
class Device;
class MyPolicy;
using VecPairSS = std::vector<std::pair<std::string, std::string>>;
@@ -25,27 +26,32 @@ struct Role
bool isOpen {false};
};
class ElectPeriod
{
public:
double priceSuperPeak {};
double pricePeak {};
double priceShoulder {};
double priceOffPeak {};
std::vector<std::vector<std::string>> vecPeriods;
void parse(std::string jsonstr);
std::string dump();
};
class AppData
{
public:
void init();
void initFromDB();
std::shared_ptr<Station> getStation(int stationId);
std::shared_ptr<Station> getStationByName(std::string name);
std::shared_ptr<Device> getDevice(int stationId, int deviceId);
///////////////////////////////////////////////////////////////////////////////////////////////
// 获取角色名称列表
std::vector<std::string> getRoleNames();
// 获取场站名称列表
std::vector<std::string> getStationNames();
// 获取设备类型
std::vector<std::string> getDeviceTypes();
std::shared_ptr<Device> getDevice(int stationId, int deviceId);
// 获取设备类型定义
std::unordered_map<int, std::shared_ptr<DeviceType>>& getDeviceTypeDef();
@@ -55,6 +61,25 @@ public:
void initUser();
///////////////////////////////////////////////////////////////////////////////////////////////
// 获取角色名称列表
std::vector<std::string> getRoleNames();
// 获取场站名称列表
std::vector<std::string> getStationNames();
// 获取设备类型
std::vector<std::string> getDeviceTypeNames();
// 获取工作模式
std::vector<std::string> getWorkModes();
// 获取策略类型定义
std::vector<std::string> getPolicyTypeNames();
// 获取策略名称
std::vector<std::string> getPolicyNames();
std::vector<std::string> getElectPreiodVals(int month);
std::string getElectPreiodVal(int month, int hour);
public:
///////////////////////////////////////////////////////////////////////////////////////////////
// === 系统 ===
@@ -77,18 +102,33 @@ public:
VecPairSS deviceType;
VecPairSS workMode;
} mapping;
///////////////////////////////////////////////////////////////////////////////////////////////
// === 场站信息 ===
double electPriceSuperPeak {};
double electPricePeak {};
double electPriceShoulder {};
double electPriceOffPeak {};
// 场站信息
std::unordered_map<int, std::shared_ptr<Station>> mapStation;
///////////////////////////////////////////////////////////////////////////////////////////////
// === 角色定义 ===
// 角色信息
std::unordered_map<int, std::shared_ptr<Role>> mapRole;
///////////////////////////////////////////////////////////////////////////////////////////////
// === 设备类型定义 ===
// 设备类型定义
std::unordered_map<int, std::shared_ptr<DeviceType>> mapDeviceType;
// 工作模式定义
std::unordered_map<int, std::string> mapWorkMode;
// 策略类型定义
std::unordered_map<int, std::string> mapPolicyType;
// 策略信息
std::unordered_map<int, std::shared_ptr<MyPolicy>> mapPolicy;
// 电力峰谷分段 (12个月每个月按小时分成24个时段)
std::vector<std::vector<std::string>> vecElectPeriods;
};

View File

@@ -9,8 +9,8 @@ AppOption Config::option;
bool Config::init(std::string filename)
{
NJson jsonroot;
bool ret = NJsonLoad(filename, jsonroot);
NJsonNode jsonroot;
bool ret = NJson::load(filename, jsonroot);
if (!ret)
{
XLOGE() << "[APP] load config failed, filename=" << filename;
@@ -20,7 +20,7 @@ bool Config::init(std::string filename)
if (jsonroot.contains("database"))
{
NJson json = jsonroot.at("database");
NJsonNode json = jsonroot.at("database");
option.database.host = json.contains("host") ? json.at("host") : "";
option.database.port = json.contains("port") ? json.at("port") : 0;
option.database.user = json.contains("user") ? json.at("user") : "";

View File

@@ -47,7 +47,7 @@ Errcode DAO1::login(std::shared_ptr<DaoEntity> dao, std::string account, std::st
}
Fields& fields = res[0];
std::string userId = fields.value("user_id");
int loginCount = fields.getInt("login_count");
int loginCount = fields.get<int>("login_count");
// 判断密码
if (passwd != fields.value("passwd"))

View File

@@ -67,16 +67,16 @@ int Device::startComm()
std::shared_ptr<Device> Device::create(Fields& fields)
{
auto device = std::make_shared<Device>();
device->deviceId = fields.getInt("device_id");
device->type = fields.getInt("type");
device->deviceId = fields.get<int>("device_id");
device->type = fields.get<int>("type");
device->name = fields.value("name");
device->code = fields.value("code");
device->isOpen = fields.getInt("is_open");
device->isOpen = fields.get<int>("is_open");
device->attrsJson = fields.value("attrs");
// 解析属性的JSON字符串转换成键值对
NJson jsonroot;
bool ret = NJsonParse(device->attrsJson, jsonroot);
NJsonNode jsonroot;
bool ret = NJson::parse(device->attrsJson, jsonroot);
if (!ret) // 解析错误
{
XLOGE() << "device attr json parse error, device_id=" << device->deviceId;

0
src/app/Policy.cpp Normal file
View File

15
src/app/Policy.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
#include <string>
#include "common/Fields.h"
class MyPolicy
{
public:
int policyId {0};
int type {0};
std::string name;
std::string value;
int isOpen {0};
Fields fields;
};

View File

@@ -1,5 +1,6 @@
#include "Station.h"
#include "database/DAO.h"
#include "database/SQL.h"
Station::Station(int id) : id(id)
{
@@ -18,4 +19,28 @@ std::shared_ptr<Device> Station::getDevice(int deviceId)
return iter->second;
}
return nullptr;
}
void Station::setWorkMode(int modeId)
{
std::string sql = SQL(SQL::TYPE::update).table(DMStation::TABLENAME)
.update(DMStation::WORK_MODE_ID, std::to_string(modeId))
.where(DMStation::STATION_ID + "=" + std::to_string(id)).str();
Errcode err = DAO::exec(NULL, sql);
if (err != Errcode::OK)
{
XLOGE() << "set station work mode failed.";
}
}
void Station::setPolicy(int policyId)
{
std::string sql = SQL(SQL::TYPE::update).table(DMStation::TABLENAME)
.update(DMStation::POLICY_ID, std::to_string(policyId))
.where(DMStation::STATION_ID + "=" + std::to_string(id)).str();
Errcode err = DAO::exec(NULL, sql);
if (err != Errcode::OK)
{
XLOGE() << "set station policy failed.";
}
}

View File

@@ -13,10 +13,17 @@ public:
void addDevice(int deviceId, std::shared_ptr<Device> device);
std::shared_ptr<Device> getDevice(int deviceId);
void setWorkMode(int modeId);
void setPolicy(int policyId);
public:
int id {};
std::string name;
int workModeId; // 运行模式
int runPolicyId; // 运行策略
// 储能容量
double energyCapacity {};