修改运行监控场站及设备信息查询接口

This commit is contained in:
lixiaoyuan
2025-09-06 15:23:07 +08:00
parent aca9a8f0ae
commit 566a3b050c
17 changed files with 468 additions and 302 deletions

View File

@@ -11,19 +11,19 @@
void ElectPeriod::parse(std::string jsonstr)
{
NJsonNode jsonroot;
NJson::parse(jsonstr, jsonroot);
njson jsonroot;
JSON::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);
JSON::read(jsonroot, "price_super_peak", this->priceSuperPeak);
JSON::read(jsonroot, "price_peak", this->pricePeak);
JSON::read(jsonroot, "price_shoulder", this->priceShoulder);
JSON::read(jsonroot, "price_off_peak", this->priceOffPeak);
JSON::read<std::vector<std::vector<std::string>>>(jsonroot, "periods", this->vecPeriods);
}
std::string ElectPeriod::dump()
{
NJsonNode jsonroot;
njson jsonroot;
jsonroot["price_super_peak"] = this->priceSuperPeak;
jsonroot["price_peak"] = this->pricePeak;
jsonroot["price_shoulder"] = this->priceShoulder;
@@ -130,8 +130,6 @@ void AppData::initFromDB()
if (station)
{
auto device = Device::create(fields);
auto deviceTypeDef = this->getDeviceTypeDef(device->type);
device->group = deviceTypeDef->group;
station->addDevice(deviceId, device);
}
else

View File

@@ -10,8 +10,8 @@ AppOption Config::option;
bool Config::init(std::string filename)
{
NJsonNode jsonroot;
bool ret = NJson::load(filename, jsonroot);
njson jsonroot;
bool ret = JSON::load(filename, jsonroot);
if (!ret)
{
spdlog::error("[config] load config file failed, filename={}", filename);
@@ -21,7 +21,7 @@ bool Config::init(std::string filename)
if (jsonroot.contains("database"))
{
NJsonNode json = jsonroot.at("database");
njson 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") : "";
@@ -37,11 +37,11 @@ bool Config::init(std::string filename)
if (jsonroot.contains("http"))
{
NJsonNode json = jsonroot.at("http");
njson json = jsonroot.at("http");
std:string token;
NJson::read(json, "token", token);
JSON::read(json, "token", token);
option.http.useToken = !token.empty();
NJson::read(json, "port", option.http.port);
JSON::read(json, "port", option.http.port);
}
else
{
@@ -50,10 +50,10 @@ bool Config::init(std::string filename)
if (jsonroot.contains("mqtt"))
{
NJsonNode json = jsonroot.at("mqtt");
NJson::read(json, "host", option.mqtt.host);
NJson::read(json, "username", option.mqtt.username);
NJson::read(json, "password", option.mqtt.password);
njson json = jsonroot.at("mqtt");
JSON::read(json, "host", option.mqtt.host);
JSON::read(json, "username", option.mqtt.username);
JSON::read(json, "password", option.mqtt.password);
}
else
{

View File

@@ -2,37 +2,37 @@
void EMSYX::fromJson(const std::string& str)
{
NJsonNode jsonroot;
auto ret = NJson::parse(str, jsonroot);
njson jsonroot;
auto ret = JSON::parse(str, jsonroot);
if (!ret) { return; }
NJson::read(jsonroot, "mcu", mcu);
NJson::read(jsonroot, "pcs", pcs);
NJson::read(jsonroot, "electMeterMainPoint", electMeterMainPoint);
NJson::read(jsonroot, "electMeter", electMeter);
NJson::read(jsonroot, "fireSystem", fireSystem);
NJson::read(jsonroot, "ups", ups);
NJson::read(jsonroot, "temHumMainPoint", temHumMainPoint);
NJson::read(jsonroot, "temHum", temHum);
NJson::read(jsonroot, "aircMainPoint", aircMainPoint);
NJson::read(jsonroot, "airc", airc);
NJson::read(jsonroot, "controlDryContact", controlDryContact);
NJson::read(jsonroot, "statusDryContact", statusDryContact);
NJson::read(jsonroot, "bcuMain", bcuMain);
NJson::read(jsonroot, "pcuMain", pcuMain);
NJson::read(jsonroot, "electMeterMain", electMeterMain);
NJson::read(jsonroot, "fireSystemMain", fireSystemMain);
NJson::read(jsonroot, "upsMain", upsMain);
NJson::read(jsonroot, "temHumMain", temHumMain);
NJson::read(jsonroot, "aircMain", aircMain);
NJson::read(jsonroot, "emu", emu);
NJson::read(jsonroot, "chillerMain", chillerMain);
NJson::read(jsonroot, "chillerMainPoint", chillerMainPoint);
NJson::read(jsonroot, "chiller", chiller);
JSON::read(jsonroot, "mcu", mcu);
JSON::read(jsonroot, "pcs", pcs);
JSON::read(jsonroot, "electMeterMainPoint", electMeterMainPoint);
JSON::read(jsonroot, "electMeter", electMeter);
JSON::read(jsonroot, "fireSystem", fireSystem);
JSON::read(jsonroot, "ups", ups);
JSON::read(jsonroot, "temHumMainPoint", temHumMainPoint);
JSON::read(jsonroot, "temHum", temHum);
JSON::read(jsonroot, "aircMainPoint", aircMainPoint);
JSON::read(jsonroot, "airc", airc);
JSON::read(jsonroot, "controlDryContact", controlDryContact);
JSON::read(jsonroot, "statusDryContact", statusDryContact);
JSON::read(jsonroot, "bcuMain", bcuMain);
JSON::read(jsonroot, "pcuMain", pcuMain);
JSON::read(jsonroot, "electMeterMain", electMeterMain);
JSON::read(jsonroot, "fireSystemMain", fireSystemMain);
JSON::read(jsonroot, "upsMain", upsMain);
JSON::read(jsonroot, "temHumMain", temHumMain);
JSON::read(jsonroot, "aircMain", aircMain);
JSON::read(jsonroot, "emu", emu);
JSON::read(jsonroot, "chillerMain", chillerMain);
JSON::read(jsonroot, "chillerMainPoint", chillerMainPoint);
JSON::read(jsonroot, "chiller", chiller);
}
std::string EMSYX::toJson()
{
NJsonNode jsonroot;
njson jsonroot;
jsonroot["bms"] = bms;
jsonroot["bcu"] = bcu;
jsonroot["mcu"] = mcu;

View File

@@ -1,6 +1,6 @@
#include "Device.h"
#include "common/Logger.h"
#include "common/Spdlogger.h"
#include "common/Utils.h"
#include "protocol/CommEntity.h"
#include "common/JsonN.h"
@@ -44,26 +44,66 @@ int Device::startComm()
return 0;
}
// 从属性列表中获取通讯方式和通讯地址、端口
std::string commType = attrs.value("commType");
// 如果entity的通讯协议类型当前配置不一致需要关闭连接删除通讯后创建新的通讯
if (commEntity && commEntity->type != commType)
{
commEntity->close();
commEntity = nullptr;
}
// 创建新的通讯
if (!commEntity)
{
commEntity = CommEntity::create(attrs);
if (!commEntity) { return -1; }
}
commEntity->start();
//// 从属性列表中获取通讯方式和通讯地址、端口
//std::string commType = attrs.value("commType");
//
//// 如果entity的通讯协议类型当前配置不一致需要关闭连接删除通讯后创建新的通讯
//if (commEntity && commEntity->type != commType)
//{
// commEntity->close();
// commEntity = nullptr;
//}
//// 创建新的通讯
//if (!commEntity)
//{
// commEntity = CommEntity::create(attrs);
// if (!commEntity) { return -1; }
//}
//commEntity->start();
return 0;
}
void Device::getRuntimeParams(std::vector<std::pair<std::string, std::string>>& params)
{
params.push_back({"额定电压", "0.0V"});
params.push_back({"实时电压", "0.0V"});
params.push_back({"额定电流", "0.0A"});
params.push_back({"实时电流", "0.0A"});
params.push_back({"额定功率", "0.0kW"});
params.push_back({"实时功率", "0.0A"});
}
void Device::getCacheVoltage(std::vector<std::string>& vec)
{
vec.resize(mapCacheVoltage.size());
int i = 0;
for (auto iter = mapCacheVoltage.begin(); iter != mapCacheVoltage.end(); ++iter)
{
vec[i] = Utils::toStr(iter->second);
i++;
}
}
void Device::getCacheCurrent(std::vector<std::string>& vec)
{
vec.resize(mapCacheCurrent.size());
int i = 0;
for (auto iter = mapCacheCurrent.begin(); iter != mapCacheCurrent.end(); ++iter)
{
vec[i] = Utils::toStr(iter->second);
i++;
}
}
void Device::getCachePower(std::vector<std::string>& vec)
{
vec.resize(mapCachePower.size());
int i = 0;
for (auto iter = mapCachePower.begin(); iter != mapCachePower.end(); ++iter)
{
vec[i] = Utils::toStr(iter->second);
i++;
}
}
std::shared_ptr<Device> Device::create(Fields& fields)
{
auto device = std::make_shared<Device>();
@@ -73,13 +113,14 @@ std::shared_ptr<Device> Device::create(Fields& fields)
device->code = fields.value("code");
device->isOpen = fields.get<int>("is_open");
device->attrsJson = fields.value("attrs");
device->category = fields.get<int>("category");
// 解析属性的JSON字符串转换成键值对
NJsonNode jsonroot;
bool ret = NJson::parse(device->attrsJson, jsonroot);
njson jsonroot;
bool ret = JSON::parse(device->attrsJson, jsonroot);
if (!ret) // 解析错误
{
XLOGE() << "device attr json parse error, device_id=" << device->deviceId;
spdlog::error("[device] device attr json parse error, device_id={}", device->deviceId);
}
else
{
@@ -92,30 +133,23 @@ std::shared_ptr<Device> Device::create(Fields& fields)
device->attrs.set(key, val.get<int>());
}
else {
XLOGE() << key << ": [" << valType << "]";
spdlog::error("[device] device attr unknown type: key={}, valtype={}", key, valType);
}
}
}
int step = 600;
for (int i = 0; i*600<86400; ++i)
{
double voltage = double(Utils::random(20000, 30000))*0.01;
double current = double(Utils::random(1000, 2000))*0.01;
device->mapCacheVoltage[i*step] = voltage;
device->mapCacheCurrent[i*step] = current;
device->mapCachePower[i*step] = voltage * current;
}
// 启动通讯该函数中会自动判断isOpen状态选择是否进行通讯连接
device->startComm();
//device->startComm();
return device;
}
//
//std::vector<std::shared_ptr<DeviceEntity>> Device::getDeviceByType(int type)
//{
// std::vector<std::shared_ptr<DeviceEntity>> vecDevice;
// for (auto iter = mapDevices.begin(); iter!=mapDevices.end(); ++iter)
// {
// auto device = iter->second;
// if (device && (type<=0 || device->type == type))
// {
// vecDevice.push_back(device);
// }
// }
// return vecDevice;
//}

View File

@@ -16,13 +16,13 @@ public:
int type = -1;
std::string name;
std::string code;
std::string group;
int category;
bool isOpen = false;
std::string attrsJson = "";
int err = 0;
int online = 0;
int status = 0;
int running = 0;
//std::map<std::string, std::string> mapAttrs;
Fields attrs;
@@ -37,10 +37,16 @@ public:
int64_t tsDataDate {};
std::map<int, double> mapCacheData;
std::map<int, double> mapCacheVoltage;
std::map<int, double> mapCacheCurrent;
std::map<int, double> mapCachePower;
// 启动通讯
int startComm();
void getRuntimeParams(std::vector<std::pair<std::string, std::string>>& params);
void getCacheVoltage(std::vector<std::string>& vec);
void getCacheCurrent(std::vector<std::string>& vec);
void getCachePower(std::vector<std::string>& vec);
static std::shared_ptr<Device> create(Fields& fields);
};

View File

@@ -42,7 +42,7 @@ void Station::setFields(Fields& fields)
void Station::addDevice(int deviceId, std::shared_ptr<Device> device)
{
mapDevice[deviceId] = device;
mapDeviceGroupNum[device->group]++;
mapDeviceGroup[device->category].push_back(device);
}
std::shared_ptr<Device> Station::getDevice(int deviceId)
@@ -67,20 +67,17 @@ void Station::getDeviceByType(int typeId, std::vector<std::shared_ptr<Device>>&
}
}
int Station::getDeviceNumByGroup(std::string name)
int Station::getDeviceNumByGroup(int category)
{
return mapDeviceGroupNum[name];
return 0;
}
void Station::getDeviceByGroup(std::string name, std::vector<std::shared_ptr<Device>>& res)
void Station::getDeviceByGroup(int category, std::vector<std::shared_ptr<Device>>& res)
{
for (auto iter = mapDevice.begin(); iter!=mapDevice.end(); ++iter)
auto iter = mapDeviceGroup.find(category);
if (iter != mapDeviceGroup.end())
{
auto device = iter->second;
if (device->group == name)
{
res.push_back(device);
}
res = iter->second;
}
}

View File

@@ -98,8 +98,8 @@ public:
std::shared_ptr<Device> getDevice(int deviceId);
void getDeviceByType(int typeId, std::vector<std::shared_ptr<Device>>& res);
int getDeviceNumByGroup(std::string name);
void getDeviceByGroup(std::string name, std::vector<std::shared_ptr<Device>>& res);
int getDeviceNumByGroup(int category);
void getDeviceByGroup(int category, std::vector<std::shared_ptr<Device>>& res);
void setWorkMode(int modeId);
void setPolicy(int policyId);
@@ -169,8 +169,8 @@ public:
///////////////////////////////////////////////////////////////////////////////////////////////
/// === 设备信息 ===
std::unordered_map<int, std::shared_ptr<Device>> mapDevice;
std::map<int, std::vector<std::shared_ptr<Device>>> mapDeviceGroup;
std::map<std::string, int> mapDeviceGroupNum;
// 温湿度信息
std::map<int, TempHumUnit> mapTempHumUnit;
@@ -184,4 +184,6 @@ public:
///////////////////////////////////////////////////////////////////////////////////////////////
/// === MQTT client
std::shared_ptr<MqttClient> mqttCli {nullptr};
};