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-09 19:26:05 +08:00
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
2025-09-16 19:38:46 +08:00
|
|
|
|
std::map<int, std::vector<DeviceParamAddr>> Device::s_mapDeviceAddrParam;
|
|
|
|
|
|
std::map<int, std::vector<std::string>> Device::s_mapDeviceAddrCurve;
|
2025-09-13 17:28:35 +08:00
|
|
|
|
|
|
|
|
|
|
static std::vector<DeviceParamAddr>& GetDeviceParamAddrs(int deviceType)
|
|
|
|
|
|
{
|
|
|
|
|
|
static std::vector<DeviceParamAddr> vecAddrs = {};
|
2025-09-16 19:38:46 +08:00
|
|
|
|
auto iter = Device::s_mapDeviceAddrParam.find(deviceType);
|
|
|
|
|
|
if (iter != Device::s_mapDeviceAddrParam.end())
|
2025-09-13 17:28:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
return iter->second;
|
|
|
|
|
|
}
|
|
|
|
|
|
return vecAddrs;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 19:26:05 +08:00
|
|
|
|
static std::unordered_set<int> g_setCacheDeviceType = {3, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110};
|
|
|
|
|
|
static bool CheckCacheType(int type)
|
|
|
|
|
|
{
|
|
|
|
|
|
return g_setCacheDeviceType.find(type) != g_setCacheDeviceType.end();
|
|
|
|
|
|
}
|
2025-09-08 19:34:12 +08:00
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Device> Device::create(Fields& fields)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto device = std::make_shared<Device>();
|
2025-09-12 18:44:34 +08:00
|
|
|
|
device->setFields(fields);
|
|
|
|
|
|
return device;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-13 17:28:35 +08:00
|
|
|
|
|
|
|
|
|
|
void Device::loadParamAddr(std::string filename)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
njson json;
|
|
|
|
|
|
if (!JSON::load(filename, json))
|
|
|
|
|
|
{
|
|
|
|
|
|
spdlog::error("[device] json load param addr error, filename={}", filename);
|
|
|
|
|
|
}
|
|
|
|
|
|
for (auto& jsonitem : json.items())
|
|
|
|
|
|
{
|
|
|
|
|
|
std::string key = jsonitem.key();
|
|
|
|
|
|
auto& jsonnodeItem = jsonitem.value();
|
|
|
|
|
|
|
|
|
|
|
|
int type = jsonnodeItem["deviceType"];
|
2025-09-16 19:38:46 +08:00
|
|
|
|
auto& vec = s_mapDeviceAddrParam[type];
|
|
|
|
|
|
for (auto& v : jsonnodeItem["addrYC"])
|
2025-09-13 17:28:35 +08:00
|
|
|
|
{
|
|
|
|
|
|
std::string name = JSON::get<std::string>(v[0]);
|
|
|
|
|
|
std::string addr = JSON::get<std::string>(v[1]);
|
|
|
|
|
|
std::string defaultVal = JSON::get<std::string>(v[2]);
|
|
|
|
|
|
std::string unit = JSON::get<std::string>(v[3]);
|
|
|
|
|
|
float ratio = Utils::toFloat(JSON::get<string>(v[4]));
|
|
|
|
|
|
vec.push_back(DeviceParamAddr(name, addr, defaultVal, unit, ratio));
|
|
|
|
|
|
}
|
2025-09-16 19:38:46 +08:00
|
|
|
|
if (jsonnodeItem.contains("addrCurve"))
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& vec = s_mapDeviceAddrCurve[type];
|
|
|
|
|
|
for (auto& v : jsonnodeItem["addrCurve"])
|
|
|
|
|
|
{
|
|
|
|
|
|
vec.push_back(v.get<std::string>());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-13 17:28:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (nlohmann::json::parse_error& e)
|
|
|
|
|
|
{
|
|
|
|
|
|
spdlog::error("[device] parse [{}] error: ", filename, e.what());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-12 18:44:34 +08:00
|
|
|
|
void Device::setFields(Fields& fields)
|
|
|
|
|
|
{
|
2025-09-16 19:38:46 +08:00
|
|
|
|
fields.get("station_id", this->stationId);
|
2025-09-12 18:44:34 +08:00
|
|
|
|
fields.get("device_id", this->deviceId);
|
|
|
|
|
|
fields.get("type", this->type);
|
|
|
|
|
|
fields.get("name", this->name);
|
|
|
|
|
|
fields.get("code", this->code);
|
|
|
|
|
|
fields.get("is_open", this->isOpen);
|
|
|
|
|
|
fields.get("attrs", this->attrsJson);
|
|
|
|
|
|
fields.get("category", this->category);
|
2025-09-16 19:38:46 +08:00
|
|
|
|
fields.get("sort_no", this->sortNo);
|
2025-09-08 19:34:12 +08:00
|
|
|
|
|
|
|
|
|
|
// 解析属性的JSON字符串,转换成键值对
|
|
|
|
|
|
njson jsonroot;
|
2025-09-12 18:44:34 +08:00
|
|
|
|
bool ret = JSON::parse(this->attrsJson, jsonroot);
|
2025-09-08 19:34:12 +08:00
|
|
|
|
if (!ret) // 解析错误
|
|
|
|
|
|
{
|
2025-09-12 18:44:34 +08:00
|
|
|
|
spdlog::error("[device] device attr json parse error, device_id={}", this->deviceId);
|
2025-09-08 19:34:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-12 18:44:34 +08:00
|
|
|
|
this->attrs.clear();
|
2025-09-08 19:34:12 +08:00
|
|
|
|
for (auto& [key, val] : jsonroot.items()) {
|
|
|
|
|
|
std::string valType = val.type_name();
|
|
|
|
|
|
if (valType == "string") {
|
2025-09-12 18:44:34 +08:00
|
|
|
|
this->attrs.set(key, val.get<std::string>());
|
2025-09-08 19:34:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (valType == "number") {
|
2025-09-12 18:44:34 +08:00
|
|
|
|
this->attrs.set(key, val.get<int>());
|
2025-09-08 19:34:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
spdlog::error("[device] device attr unknown type: key={}, valtype={}", key, valType);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-13 17:28:35 +08:00
|
|
|
|
|
|
|
|
|
|
auto& vecAddrs = GetDeviceParamAddrs(this->type);
|
|
|
|
|
|
for (auto& item: vecAddrs)
|
|
|
|
|
|
{
|
|
|
|
|
|
this->mapMyParams[item.addr] = &item;
|
|
|
|
|
|
}
|
2025-09-08 19:34:12 +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-09 19:26:05 +08:00
|
|
|
|
int64_t GetCurrentTimePos(int step)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto tp = chrono::system_clock::now();
|
|
|
|
|
|
int64_t tTime = chrono::time_point_cast<chrono::seconds>(tp).time_since_epoch().count();
|
|
|
|
|
|
std::time_t t = chrono::system_clock::to_time_t(tp);
|
|
|
|
|
|
std::tm* tmlocal = localtime(&t);
|
|
|
|
|
|
tmlocal->tm_hour = 0;
|
|
|
|
|
|
tmlocal->tm_min = 0;
|
|
|
|
|
|
tmlocal->tm_sec = 0;
|
|
|
|
|
|
int64_t tDate = chrono::time_point_cast<chrono::seconds>(chrono::system_clock::from_time_t(mktime(tmlocal))).time_since_epoch().count();
|
|
|
|
|
|
return (tTime - tDate) / step;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Device::setCache(int datatype, std::vector<double>& vec)
|
|
|
|
|
|
{
|
|
|
|
|
|
std::map<int, double>* mapptr = NULL;
|
|
|
|
|
|
if (datatype == 1) { mapptr = &mapCacheVoltage; }
|
|
|
|
|
|
else if (datatype == 2) { mapptr = &mapCacheCurrent; }
|
|
|
|
|
|
else if (datatype == 3) { mapptr = &mapCachePower; }
|
|
|
|
|
|
|
|
|
|
|
|
if (mapptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
const int step = 600;
|
|
|
|
|
|
const int N = 86400/step;
|
|
|
|
|
|
|
|
|
|
|
|
int n = GetCurrentTimePos(step);
|
|
|
|
|
|
for (int i = 0; i<N; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (i < vec.size()) { (*mapptr)[i] = vec[i]; }
|
|
|
|
|
|
else if (i <= n) { (*mapptr)[i] = 0; }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Device::cache(int npos)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!CheckCacheType(this->type))
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (npos == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
mapCacheVoltage.clear();
|
|
|
|
|
|
mapCacheCurrent.clear();
|
|
|
|
|
|
mapCachePower.clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-16 19:38:46 +08:00
|
|
|
|
std::string addrV;
|
|
|
|
|
|
std::string addrI;
|
|
|
|
|
|
std::string addrP;
|
|
|
|
|
|
auto iter = s_mapDeviceAddrCurve.find(this->type);
|
|
|
|
|
|
if (iter != s_mapDeviceAddrCurve.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
auto& vecAddr = iter->second;
|
|
|
|
|
|
auto size = addrV.size();
|
|
|
|
|
|
if (size >= 1) { addrV = vecAddr[0]; }
|
|
|
|
|
|
else if (size >= 2) { addrI = vecAddr[1]; }
|
|
|
|
|
|
else if (size >= 3) { addrP = vecAddr[2]; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 根据设备类型从参数(寄存器地址)中读取实时数据进行保存
|
|
|
|
|
|
int U = Utils::toInt(this->getParam(addrV, "0"));
|
|
|
|
|
|
int I = Utils::toInt(this->getParam(addrI, "0"));
|
|
|
|
|
|
int P = addrP.empty() ? U*I : Utils::toInt(this->getParam(addrP, "0"));
|
|
|
|
|
|
mapCacheVoltage[npos] = U;
|
|
|
|
|
|
mapCacheCurrent[npos] = I;
|
|
|
|
|
|
mapCachePower[npos] = P;
|
2025-09-09 19:26:05 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Device::storeDB(int npos)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-13 17:28:35 +08:00
|
|
|
|
void Device::setParam(std::string k, int v)
|
2025-07-18 09:08:09 +08:00
|
|
|
|
{
|
2025-09-13 17:28:35 +08:00
|
|
|
|
float ratio = 1.0;
|
|
|
|
|
|
auto iter = mapMyParams.find(k);
|
|
|
|
|
|
if (iter != mapMyParams.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
ratio = iter->second->ratio;
|
2025-09-16 19:38:46 +08:00
|
|
|
|
//spdlog::info("[device] set param: {} {}={}, ratio={}", iter->second->name, k, v, ratio);
|
2025-09-13 17:28:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-16 19:38:46 +08:00
|
|
|
|
int precision = (ratio != 1.0f) ? 1 : 0;
|
2025-09-18 20:12:46 +08:00
|
|
|
|
mapParams[k] = Utils::toStr(v*ratio, precision);
|
2025-09-10 20:10:51 +08:00
|
|
|
|
|
|
|
|
|
|
if (type == 3 ) // 电表
|
2025-09-18 20:12:46 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (k == "") this->err = v;
|
2025-09-10 20:10:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (type == 101) // EMS
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == 102) // PCS
|
|
|
|
|
|
{
|
2025-09-18 20:12:46 +08:00
|
|
|
|
if (k == "0x1003") err = v; // 故障状态 R uint16 1故障,0正常 0 0x1003
|
|
|
|
|
|
if (k == "0x1005") online = v; // 设备在线 R uint16 1在线,0无效 1 0x1005
|
|
|
|
|
|
if (k == "0x1009") running = (v==1 || v==2); //充放状态 R uint16 0:待机, 1:充电, 2:放电, 3:搁置 0 0x1009
|
2025-09-10 20:10:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (type == 103) // PCU
|
|
|
|
|
|
{
|
2025-09-18 20:12:46 +08:00
|
|
|
|
if (k == "0x1002") err = v; //故障状态 R uint16 1故障,0正常 0 0x1002
|
|
|
|
|
|
if (k == "0x1004") online = v; //设备在线 R uint16 1在线,0无效 1 0x1004
|
|
|
|
|
|
if (k == "0x1006") running = v; //启停状态 R uint16 1开机,0关机 1 0x1006
|
2025-09-10 20:10:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (type == 104) // BMS
|
|
|
|
|
|
{
|
2025-09-18 20:12:46 +08:00
|
|
|
|
if (k == "0x004A") { err = (v==1); online = 1; } //运行状态 R uint16 0 运行状态 0-正常 1-告警 2-保护 0x004A
|
|
|
|
|
|
if (k == "0x004B") running = (v==1 || v==2); //充放电状态 R uint16 0 0-待机 1-充电 2-放电 0x004B
|
2025-09-10 20:10:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (type == 105) // BCU
|
|
|
|
|
|
{
|
2025-09-18 20:12:46 +08:00
|
|
|
|
if (k == "0xA003") running = (v==0x33 || v==0x44); //蓄电池充放电状态 R uint16 "0x11开路,0x22待机,0x33充电,0x44放电" 34 0xA003
|
|
|
|
|
|
if (k == "0xA004") err = (v==0x55); online=1; //电池组运行状态 R uint16 "0x11跳机,0x22待机,0x33放空,0x44充满,0x55预警,0x66正常" 102 0xA004
|
2025-09-10 20:10:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
else if (type == 106) // 充电桩
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == 109) // 光伏板
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2025-09-08 19:34:12 +08:00
|
|
|
|
}
|
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)
|
|
|
|
|
|
{
|
2025-09-16 19:38:46 +08:00
|
|
|
|
if (k.empty())
|
|
|
|
|
|
{
|
|
|
|
|
|
return defaultVal;
|
|
|
|
|
|
}
|
2025-09-08 19:34:12 +08:00
|
|
|
|
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 光伏板
|
2025-09-16 19:38:46 +08:00
|
|
|
|
auto& vecAddr = s_mapDeviceAddrParam[this->type];
|
2025-09-13 17:28:35 +08:00
|
|
|
|
for (auto& itemAddr: vecAddr)
|
2025-09-06 15:23:07 +08:00
|
|
|
|
{
|
2025-09-16 19:38:46 +08:00
|
|
|
|
std::string v;
|
|
|
|
|
|
v = getParam(itemAddr.addr, itemAddr.defaultVal) + itemAddr.unit;
|
|
|
|
|
|
//if (this->online) { }
|
|
|
|
|
|
//else { v = "--"; }
|
|
|
|
|
|
params.push_back({itemAddr.name, v});
|
2025-09-10 20:10:51 +08:00
|
|
|
|
}
|
2025-09-13 17:28:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Device::getRuntimeParams1(std::vector<std::pair<std::string, std::string>>& params)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (type == 106)
|
2025-09-08 19:34:12 +08:00
|
|
|
|
{
|
2025-09-18 20:12:46 +08:00
|
|
|
|
params.push_back({"需求电压", getParam("31072", "0.0") + " V"});
|
|
|
|
|
|
params.push_back({"需求电流", getParam("31074", "0.0") + " A"});
|
|
|
|
|
|
params.push_back({"需求功率", getParam("31076", "0.0") + " kW"});
|
|
|
|
|
|
params.push_back({"功率限值", getParam("31078", "0.0") + " kW"});
|
|
|
|
|
|
params.push_back({"输出电压", getParam("31080", "0.0") + " V"});
|
|
|
|
|
|
params.push_back({"输出电流", getParam("31082", "0.0") + " A"});
|
|
|
|
|
|
params.push_back({"输出功率", getParam("31084", "0.0") + " kW"});
|
2025-09-08 19:34:12 +08:00
|
|
|
|
}
|
2025-07-18 09:08:09 +08:00
|
|
|
|
}
|