调试修改MQTT通讯解析

This commit is contained in:
lixiaoyuan
2025-09-16 19:38:46 +08:00
parent 9377e7f8e6
commit 393f68aec9
25 changed files with 943 additions and 1428 deletions

View File

@@ -7,13 +7,14 @@
#include <unordered_set>
std::map<int, std::vector<DeviceParamAddr>> Device::s_mapDeviceParamAddr;
std::map<int, std::vector<DeviceParamAddr>> Device::s_mapDeviceAddrParam;
std::map<int, std::vector<std::string>> Device::s_mapDeviceAddrCurve;
static std::vector<DeviceParamAddr>& GetDeviceParamAddrs(int deviceType)
{
static std::vector<DeviceParamAddr> vecAddrs = {};
auto iter = Device::s_mapDeviceParamAddr.find(deviceType);
if (iter != Device::s_mapDeviceParamAddr.end())
auto iter = Device::s_mapDeviceAddrParam.find(deviceType);
if (iter != Device::s_mapDeviceAddrParam.end())
{
return iter->second;
}
@@ -47,11 +48,10 @@ void Device::loadParamAddr(std::string filename)
{
std::string key = jsonitem.key();
auto& jsonnodeItem = jsonitem.value();
spdlog::info(jsonnodeItem.dump());
int type = jsonnodeItem["deviceType"];
auto& vec = s_mapDeviceParamAddr[type];
for (auto& v : jsonnodeItem["addr_YC"])
auto& vec = s_mapDeviceAddrParam[type];
for (auto& v : jsonnodeItem["addrYC"])
{
std::string name = JSON::get<std::string>(v[0]);
std::string addr = JSON::get<std::string>(v[1]);
@@ -60,6 +60,14 @@ void Device::loadParamAddr(std::string filename)
float ratio = Utils::toFloat(JSON::get<string>(v[4]));
vec.push_back(DeviceParamAddr(name, addr, defaultVal, unit, ratio));
}
if (jsonnodeItem.contains("addrCurve"))
{
auto& vec = s_mapDeviceAddrCurve[type];
for (auto& v : jsonnodeItem["addrCurve"])
{
vec.push_back(v.get<std::string>());
}
}
}
}
catch (nlohmann::json::parse_error& e)
@@ -70,6 +78,7 @@ void Device::loadParamAddr(std::string filename)
void Device::setFields(Fields& fields)
{
fields.get("station_id", this->stationId);
fields.get("device_id", this->deviceId);
fields.get("type", this->type);
fields.get("name", this->name);
@@ -77,6 +86,7 @@ void Device::setFields(Fields& fields)
fields.get("is_open", this->isOpen);
fields.get("attrs", this->attrsJson);
fields.get("category", this->category);
fields.get("sort_no", this->sortNo);
// 解析属性的JSON字符串转换成键值对
njson jsonroot;
@@ -216,11 +226,27 @@ bool Device::cache(int npos)
mapCacheCurrent.clear();
mapCachePower.clear();
}
// 根据设备类型从参数(寄存器地址)中读取实时数据进行保存
mapCacheVoltage[npos] = Utils::random(100, 200);
mapCacheCurrent[npos] = Utils::random(100, 200);
mapCachePower[npos] = Utils::random(100, 200);
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;
return true;
}
@@ -235,10 +261,10 @@ void Device::setParam(std::string k, int v)
if (iter != mapMyParams.end())
{
ratio = iter->second->ratio;
spdlog::info("[device] set param: {} {}={}, ratio={}", iter->second->name, k, v, ratio);
//spdlog::info("[device] set param: {} {}={}, ratio={}", iter->second->name, k, v, ratio);
}
int precision = (ratio != 1.0f) ? 2 : 0;
int precision = (ratio != 1.0f) ? 1 : 0;
mapParams[k] = Utils::toStr(v*ratio, precision);
if (type == 3 ) // 电表
@@ -280,6 +306,10 @@ void Device::setParam(std::string k, int v)
std::string Device::getParam(std::string k, std::string defaultVal)
{
if (k.empty())
{
return defaultVal;
}
auto iter = mapParams.find(k);
if (iter != mapParams.end())
{
@@ -298,10 +328,14 @@ void Device::getRuntimeParams(std::vector<std::pair<std::string, std::string>>&
// 105 BCU
// 106 充电桩
// 109 光伏板
auto& vecAddr = s_mapDeviceParamAddr[this->type];
auto& vecAddr = s_mapDeviceAddrParam[this->type];
for (auto& itemAddr: vecAddr)
{
params.push_back({itemAddr.name, getParam(itemAddr.addr, itemAddr.defaultVal) + itemAddr.unit});
std::string v;
v = getParam(itemAddr.addr, itemAddr.defaultVal) + itemAddr.unit;
//if (this->online) { }
//else { v = "--"; }
params.push_back({itemAddr.name, v});
}
}