修改场站策略解析及网关指令下发

This commit is contained in:
lixiaoyuan
2025-09-14 16:00:30 +08:00
parent d1a8fb0665
commit 6418335d9d
14 changed files with 252 additions and 68 deletions

View File

@@ -106,7 +106,8 @@ bool AppData::initFromDB()
}
{ // 数据库读取场站信息
str = "", result.clear();
DAO::queryStationList(dao, result);
std::string sql = "SELECT s.*, p.name policy_name, p.`type` policy_type, p.value FROM station s LEFT JOIN policy p ON s.policy_id=p.policy_id;";
dao->exec(sql, result);
for (auto& fields: result)
{
auto station = std::make_shared<Station>();
@@ -140,7 +141,7 @@ bool AppData::initFromDB()
DAO::queryPolicyList(dao, result);
for (auto& fields: result)
{
auto policy = std::make_shared<MyPolicy>();
auto policy = std::make_shared<SysPolicy>();
policy->policyId = fields.get<int>(DMPolicy::POLICY_ID);
policy->type = fields.get<int>(DMPolicy::TYPE);
policy->name = fields.value(DMPolicy::NAME);

View File

@@ -11,7 +11,7 @@
class Station;
class Device;
class MyPolicy;
class SysPolicy;
using VecPairSS = std::vector<std::pair<std::string, std::string>>;
@@ -155,7 +155,7 @@ public:
std::unordered_map<int, std::string> mapPolicyType;
// 策略信息
std::unordered_map<int, std::shared_ptr<MyPolicy>> mapPolicy;
std::unordered_map<int, std::shared_ptr<SysPolicy>> mapPolicy;
// 电力峰谷分段 (12个月每个月按小时分成24个时段)
std::vector<std::vector<std::string>> vecElectPeriods;

View File

@@ -0,0 +1,146 @@
#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);
}
}
}

View File

@@ -1,15 +1,32 @@
#pragma once
#include <string>
#include <vector>
#include "common/Fields.h"
#include "common/JsonN.h"
class MyPolicy
class SysPolicy
{
public:
int policyId {0};
// 1峰谷套利2配网增容3应急供电4并网保电5自定时段
int type {0};
std::string name;
std::string value;
int isOpen {0};
int isOpen {1};
Fields fields;
std::vector<float> vecPrice {0.0f, 0.0f, 0.0f, 0.0f};
std::vector<std::vector<std::pair<std::string, std::string>>> vecPeriods1;
void setFields(Fields& fields);
void parseValue(std::string jsonstr);
void parseJsonPeriods(njson& json);
void getGatewayJsonPrice(njson& json);
void getGatewayJsonPeriods(njson& json);
};

View File

@@ -40,6 +40,9 @@ void Station::setFields(Fields& fields)
this->workModeId = fields.get<int>(DMStation::WORK_MODE);
this->code = fields.value(DMStation::CODE);
this->status = fields.get<int>(DMStation::STATUS);
this->operationDate = fields.value(DMStation::OPERATION_DATE);
this->policy.setFields(fields);
}
void Station::addDevice(int deviceId, std::shared_ptr<Device> device)
@@ -210,7 +213,19 @@ void Station::setGarewayWorkMode()
json["ts"] = Utils::time();
json["no"] = 1; // 设备编号
json["40001"] = this->workModeId;
if (policy.type == 1)
{
json["40002"] = njson::array(); // 峰谷套利
policy.getGatewayJsonPeriods(json["40002"]);
}
else if (policy.type == 5)
{
json["40021"] = njson::array(); // 自定时段
policy.getGatewayJsonPeriods(json["40021"]);
}
std::string text = json.dump();
spdlog::info(text);
mqttCli->publish("Gateway_YT", text);
}
}

View File

@@ -3,6 +3,7 @@
#include <memory>
#include <unordered_map>
#include "common/Fields.h"
#include "Policy.h"
class Device;
class MqttClient;
@@ -106,7 +107,6 @@ public:
void setWorkMode(int modeId);
void setPolicy(int policyId);
void writeRuntimeData(std::string dt, int npos);
void initMqtt();
@@ -119,6 +119,9 @@ public:
std::string code;
bool isOpen {false};
int status {0};
std::string operationDate;
SysPolicy policy;
bool isConnected {false};
int workModeId {}; // 运行模式