Files
energy_storage/src/app/Policy.cpp
2025-09-14 16:00:30 +08:00

146 lines
3.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "Policy.h"
#include "common/Spdlogger.h"
#include "Utils.h"
void SysPolicy::setFields(Fields& fields)
{
this->policyId = fields.get<int>("policy_id");
this->type = fields.get<int>("policy_type");
this->name = fields.value("policy_name");
this->value = fields.value("value");
this->parseValue(value);
}
/// type=1: 峰谷套利
static void ParseJsonType1(njson& json)
{
}
void SysPolicy::getGatewayJsonPrice(njson& json)
{
}
// 1峰谷套利2配网增容3应急供电4并网保电5自定时段
void SysPolicy::parseValue(std::string jsonstr)
{
njson jsonroot;
if (!JSON::parse(jsonstr, jsonroot))
{
spdlog::error("[policy] json parse policy value error, policy_id={}, value={}", policyId, jsonstr);
return;
}
// 读取电价
if (jsonroot.contains("price"))
{
auto& jsonArrayPrice = jsonroot["price"];
for (int i = 0; i<vecPrice.size(); ++i)
{
if (i<jsonArrayPrice.size())
{
vecPrice[i] = Utils::toFloat(jsonArrayPrice[i].get<std::string>());
}
}
}
if (this->type == 1 || this->type == 5)
{
this->parseJsonPeriods(jsonroot);
}
}
void SysPolicy::parseJsonPeriods(njson& jsonroot)
{
if (!jsonroot.contains("period"))
{
spdlog::error("[policy] json parse policy value error, [period] is not exist, value={}", jsonroot.dump());
return;
}
auto& json = jsonroot["period"];
if (!json.is_array())
{
spdlog::error("[policy] json parse policy value error, [period] is not array, value={}", json.dump());
return;
}
// 1: 谷234
if (this->type == 1) // 峰谷套利
{
vecPeriods1.clear();
for (int i = 0; i<json.size() && i<12; ++i) // 月份信息最多12个月
{
if (json[i].size() > 0)
{
vecPeriods1.push_back(std::vector<std::pair<std::string, std::string>>());
for (auto& itemMonth: json[i])
{
if (itemMonth.size() >=2) { vecPeriods1[i].push_back({itemMonth[0], itemMonth[1]}); }
}
}
}
}
else if (this->type == 5)
{
//{
// "period":[
// {"charge_time":[],"discharge_time":[],"charge_power":"","discharge_power":""},
// {"charge_time":[],"discharge_time":[],"charge_power":"","discharge_power":""}
// ],
// "price":["0.00","0.00","0.00","0.00"]
//}
vecPeriods1.clear();
for (int i = 0; i<json.size(); ++i) // 一充一放或二充二放
{
vecPeriods1.push_back(std::vector<std::pair<std::string, std::string>>());
auto& item = json[i];
if (item.contains("charge_time") && item["charge_time"].size() >= 2)
{
auto& jsonP = item["charge_time"];
vecPeriods1[i].push_back({jsonP[0], ""}); // 第一/二次充电开始
vecPeriods1[i].push_back({jsonP[1], ""}); // 第一/二次充电结束
}
if (item.contains("discharge_time") && item["discharge_time"].size() >= 2)
{
auto& jsonP = item["discharge_time"];
vecPeriods1[i].push_back({jsonP[0], ""}); // 第一/二次放电开始
vecPeriods1[i].push_back({jsonP[1], ""}); // 第一/二次放电结束
}
}
}
}
static void PeriodsTimeStrToInt(std::string str, int& h, int& m)
{
int pos = str.find(":");
if (pos != std::string::npos)
{
h = Utils::toInt(str.substr(0, pos));
m = Utils::toInt(str.substr(pos+1));
}
}
void SysPolicy::getGatewayJsonPeriods(njson& json)
{
if (type == 1 || type == 5)
{
// std::vector<std::vector<std::pair<std::string, std::string>>>
for (auto& itemMonth: vecPeriods1)
{
njson jsonArrayMonth = njson::array();
for (auto& item: itemMonth)
{
int h = 0; int m = 0;
PeriodsTimeStrToInt(item.first, h, m);
int p = 1;
if (item.second == "") p = 1;
else if (item.second == "") p = 2;
else if (item.second == "") p = 3;
else if (item.second == "") p = 4;
jsonArrayMonth.push_back({h, m, p});
}
json.push_back(jsonArrayMonth);
}
}
}