2025-07-18 09:08:09 +08:00
|
|
|
|
#include "Device.h"
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
#include "common/Spdlogger.h"
|
2025-07-18 09:08:09 +08:00
|
|
|
|
#include "common/Utils.h"
|
2025-08-28 18:42:37 +08:00
|
|
|
|
#include "protocol/CommEntity.h"
|
2025-07-18 09:08:09 +08:00
|
|
|
|
#include "common/JsonN.h"
|
|
|
|
|
|
|
2025-09-08 19:34:12 +08:00
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Device> Device::create(Fields& fields)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto device = std::make_shared<Device>();
|
|
|
|
|
|
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.get<int>("is_open");
|
|
|
|
|
|
device->attrsJson = fields.value("attrs");
|
|
|
|
|
|
device->category = fields.get<int>("category");
|
|
|
|
|
|
|
|
|
|
|
|
// 解析属性的JSON字符串,转换成键值对
|
|
|
|
|
|
njson jsonroot;
|
|
|
|
|
|
bool ret = JSON::parse(device->attrsJson, jsonroot);
|
|
|
|
|
|
if (!ret) // 解析错误
|
|
|
|
|
|
{
|
|
|
|
|
|
spdlog::error("[device] device attr json parse error, device_id={}", device->deviceId);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
for (auto& [key, val] : jsonroot.items()) {
|
|
|
|
|
|
std::string valType = val.type_name();
|
|
|
|
|
|
if (valType == "string") {
|
|
|
|
|
|
device->attrs.set(key, val.get<std::string>());
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (valType == "number") {
|
|
|
|
|
|
device->attrs.set(key, val.get<int>());
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
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();
|
|
|
|
|
|
return device;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
|
2025-08-20 19:00:22 +08:00
|
|
|
|
int Device::startComm()
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (!isOpen)
|
|
|
|
|
|
{
|
2025-08-31 14:38:53 +08:00
|
|
|
|
if (commEntity && commEntity->alive)
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
|
|
|
|
|
commEntity->close();
|
|
|
|
|
|
}
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 15:23:07 +08:00
|
|
|
|
//// 从属性列表中获取通讯方式和通讯地址、端口
|
|
|
|
|
|
//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::getCacheVoltage(std::vector<std::string>& vec)
|
|
|
|
|
|
{
|
|
|
|
|
|
vec.resize(mapCacheVoltage.size());
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
for (auto iter = mapCacheVoltage.begin(); iter != mapCacheVoltage.end(); ++iter)
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
vec[i] = Utils::toStr(iter->second);
|
|
|
|
|
|
i++;
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
2025-09-06 15:23:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
void Device::getCacheCurrent(std::vector<std::string>& vec)
|
|
|
|
|
|
{
|
|
|
|
|
|
vec.resize(mapCacheCurrent.size());
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
for (auto iter = mapCacheCurrent.begin(); iter != mapCacheCurrent.end(); ++iter)
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
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++;
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-08 19:34:12 +08:00
|
|
|
|
void Device::setParam(std::string k, std::string v)
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
2025-09-08 19:34:12 +08:00
|
|
|
|
mapParams[k] = v;
|
|
|
|
|
|
}
|
2025-07-18 09:08:09 +08:00
|
|
|
|
|
2025-09-08 19:34:12 +08:00
|
|
|
|
std::string Device::getParam(std::string k, std::string defaultVal)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto iter = mapParams.find(k);
|
|
|
|
|
|
if (iter != mapParams.end())
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
2025-09-08 19:34:12 +08:00
|
|
|
|
return iter->second;
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
2025-09-08 19:34:12 +08:00
|
|
|
|
return defaultVal;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Device::getRuntimeParams(std::vector<std::pair<std::string, std::string>>& params)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 3 电表
|
|
|
|
|
|
// 101 EMS
|
|
|
|
|
|
// 102 PCS
|
|
|
|
|
|
// 103 PCU
|
|
|
|
|
|
// 104 BMS
|
|
|
|
|
|
// 105 BCU
|
|
|
|
|
|
// 106 充电桩
|
|
|
|
|
|
// 109 光伏板
|
|
|
|
|
|
|
|
|
|
|
|
if (this->type == 3)
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
2025-09-08 19:34:12 +08:00
|
|
|
|
params.push_back({"A相电压", getParam("0x000B", "0.0") + "V"});
|
|
|
|
|
|
params.push_back({"B相电压", getParam("0x000D", "0.0") + "V"});
|
|
|
|
|
|
params.push_back({"C相电压", getParam("0x000F", "0.0") + "V"});
|
|
|
|
|
|
params.push_back({"A相电流", getParam("0x0011", "0.0") + "A"});
|
|
|
|
|
|
params.push_back({"B相电流", getParam("0x0013", "0.0") + "A"});
|
|
|
|
|
|
params.push_back({"C相电流", getParam("0x0015", "0.0") + "A"});
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|
2025-09-08 19:34:12 +08:00
|
|
|
|
else if (this->type == 101)
|
2025-09-06 15:23:07 +08:00
|
|
|
|
{
|
2025-09-08 19:34:12 +08:00
|
|
|
|
params.push_back({"额定电压", getParam("0x0001", "0.0") + "V"});
|
|
|
|
|
|
params.push_back({"实时电压", getParam("0x0001", "0.0") + "V"});
|
|
|
|
|
|
params.push_back({"额定电流", getParam("0x0001", "0.0") + "A"});
|
|
|
|
|
|
params.push_back({"实时电流", getParam("0x0001", "0.0") + "A"});
|
|
|
|
|
|
params.push_back({"额定功率", getParam("0x0001", "0.0") + "kW"});
|
|
|
|
|
|
params.push_back({"实时功率", getParam("0x0001", "0.0") + "A"});
|
2025-09-06 15:23:07 +08:00
|
|
|
|
}
|
2025-09-08 19:34:12 +08:00
|
|
|
|
//else if (this->type == 101)
|
|
|
|
|
|
//{
|
2025-09-06 15:23:07 +08:00
|
|
|
|
|
2025-09-08 19:34:12 +08:00
|
|
|
|
//}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
params.push_back({"额定电压", getParam("0x0001", "0.0") + "V"});
|
|
|
|
|
|
params.push_back({"实时电压", getParam("0x0001", "0.0") + "V"});
|
|
|
|
|
|
params.push_back({"额定电流", getParam("0x0001", "0.0") + "A"});
|
|
|
|
|
|
params.push_back({"实时电流", getParam("0x0001", "0.0") + "A"});
|
|
|
|
|
|
params.push_back({"额定功率", getParam("0x0001", "0.0") + "kW"});
|
|
|
|
|
|
params.push_back({"实时功率", getParam("0x0001", "0.0") + "A"});
|
|
|
|
|
|
}
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|