mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
实现MQTT协议消息订阅和消息解析流程
This commit is contained in:
@@ -276,6 +276,16 @@ std::shared_ptr<Device> AppData::getDevice(int stationId, int deviceId)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::shared_ptr<Device> AppData::getDeviceByType(int stationId, int deviceType, std::string code)
|
||||
{
|
||||
auto station = getStation(stationId);
|
||||
if (station)
|
||||
{
|
||||
return station->getDeviceByType(deviceType, code);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string AppData::getDeviceNameById(int typeId)
|
||||
{
|
||||
auto iter = mapDeviceType.find(typeId);
|
||||
|
||||
@@ -73,6 +73,7 @@ public:
|
||||
std::shared_ptr<Station> getStationByCode(std::string code);
|
||||
|
||||
std::shared_ptr<Device> getDevice(int stationId, int deviceId);
|
||||
std::shared_ptr<Device> getDeviceByType(int stationId, int deviceType, std::string code);
|
||||
|
||||
std::string getDeviceNameById(int typeId);
|
||||
|
||||
|
||||
@@ -59,5 +59,11 @@ bool Config::init(std::string filename)
|
||||
{
|
||||
spdlog::info("[config] parse mqtt failed: not found.");
|
||||
}
|
||||
|
||||
if (jsonroot.contains("weburl"))
|
||||
{
|
||||
JSON::read(jsonroot, "weburl", option.webSrvUrl);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -26,6 +26,8 @@ struct AppOption
|
||||
std::string password;
|
||||
} mqtt;
|
||||
|
||||
std::string webSrvUrl;
|
||||
|
||||
};
|
||||
|
||||
class Config
|
||||
|
||||
@@ -5,104 +5,6 @@
|
||||
#include "protocol/CommEntity.h"
|
||||
#include "common/JsonN.h"
|
||||
|
||||
//int DeviceEntity::getAttrInt(std::string key)
|
||||
//{
|
||||
// auto iter = mapAttrs.find(key);
|
||||
// if (iter == mapAttrs.end()) { return 0; }
|
||||
// return Utils::toInt(iter->second);
|
||||
//}
|
||||
//
|
||||
//float DeviceEntity::getAttrFloat(std::string key)
|
||||
//{
|
||||
// auto iter = mapAttrs.find(key);
|
||||
// if (iter == mapAttrs.end()) { return 0.0f; }
|
||||
// return Utils::toFloat(iter->second);
|
||||
//}
|
||||
//
|
||||
//double DeviceEntity::getAttrDouble(std::string key)
|
||||
//{
|
||||
// auto iter = mapAttrs.find(key);
|
||||
// if (iter == mapAttrs.end()) { return 0.0; }
|
||||
// return Utils::toDouble(iter->second);
|
||||
//}
|
||||
//
|
||||
//std::string DeviceEntity::getAttrStr(std::string key)
|
||||
//{
|
||||
// auto iter = mapAttrs.find(key);
|
||||
// if (iter == mapAttrs.end()) { return ""; }
|
||||
// return iter->second;
|
||||
//}
|
||||
|
||||
int Device::startComm()
|
||||
{
|
||||
if (!isOpen)
|
||||
{
|
||||
if (commEntity && commEntity->alive)
|
||||
{
|
||||
commEntity->close();
|
||||
}
|
||||
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();
|
||||
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)
|
||||
{
|
||||
@@ -153,3 +55,123 @@ std::shared_ptr<Device> Device::create(Fields& fields)
|
||||
return device;
|
||||
}
|
||||
|
||||
|
||||
int Device::startComm()
|
||||
{
|
||||
if (!isOpen)
|
||||
{
|
||||
if (commEntity && commEntity->alive)
|
||||
{
|
||||
commEntity->close();
|
||||
}
|
||||
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();
|
||||
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)
|
||||
{
|
||||
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++;
|
||||
}
|
||||
}
|
||||
|
||||
void Device::setParam(std::string k, std::string v)
|
||||
{
|
||||
mapParams[k] = v;
|
||||
}
|
||||
|
||||
std::string Device::getParam(std::string k, std::string defaultVal)
|
||||
{
|
||||
auto iter = mapParams.find(k);
|
||||
if (iter != mapParams.end())
|
||||
{
|
||||
return iter->second;
|
||||
}
|
||||
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)
|
||||
{
|
||||
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"});
|
||||
}
|
||||
else if (this->type == 101)
|
||||
{
|
||||
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"});
|
||||
}
|
||||
//else if (this->type == 101)
|
||||
//{
|
||||
|
||||
//}
|
||||
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"});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,24 @@
|
||||
|
||||
class CommEntity;
|
||||
|
||||
|
||||
class Device
|
||||
{
|
||||
public:
|
||||
static std::shared_ptr<Device> create(Fields& fields);
|
||||
|
||||
|
||||
int startComm();
|
||||
|
||||
void getCacheVoltage(std::vector<std::string>& vec);
|
||||
void getCacheCurrent(std::vector<std::string>& vec);
|
||||
void getCachePower(std::vector<std::string>& vec);
|
||||
|
||||
void setParam(std::string k, std::string v);
|
||||
std::string getParam(std::string k, std::string defaultVal = "");
|
||||
|
||||
void getRuntimeParams(std::vector<std::pair<std::string, std::string>>& params);
|
||||
|
||||
public:
|
||||
int deviceId = -1;
|
||||
int type = -1;
|
||||
@@ -30,23 +46,9 @@ public:
|
||||
// 通讯entity
|
||||
std::shared_ptr<CommEntity> commEntity;
|
||||
|
||||
//int getAttrInt(std::string key);
|
||||
//float getAttrFloat(std::string key);
|
||||
//double getAttrDouble(std::string key);
|
||||
//std::string getAttrStr(std::string key);
|
||||
|
||||
|
||||
int64_t tsDataDate {};
|
||||
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);
|
||||
std::map<std::string, std::string> mapParams;
|
||||
};
|
||||
|
||||
@@ -55,12 +55,25 @@ std::shared_ptr<Device> Station::getDevice(int deviceId)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Station::getDeviceByType(int typeId, std::vector<std::shared_ptr<Device>>& res)
|
||||
std::shared_ptr<Device> Station::getDeviceByType(int deviceType, std::string code)
|
||||
{
|
||||
for (auto iter = mapDevice.begin(); iter!=mapDevice.end(); ++iter)
|
||||
{
|
||||
auto device = iter->second;
|
||||
if (device->type == typeId)
|
||||
if (device->type == deviceType && device->code == code)
|
||||
{
|
||||
return device;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void Station::getDeviceByType(int deviceType, std::vector<std::shared_ptr<Device>>& res)
|
||||
{
|
||||
for (auto iter = mapDevice.begin(); iter!=mapDevice.end(); ++iter)
|
||||
{
|
||||
auto device = iter->second;
|
||||
if (device->type == deviceType)
|
||||
{
|
||||
res.push_back(device);
|
||||
}
|
||||
|
||||
@@ -96,7 +96,8 @@ public:
|
||||
|
||||
void addDevice(int deviceId, std::shared_ptr<Device> device);
|
||||
std::shared_ptr<Device> getDevice(int deviceId);
|
||||
|
||||
|
||||
std::shared_ptr<Device> getDeviceByType(int deviceType, std::string code);
|
||||
void getDeviceByType(int typeId, std::vector<std::shared_ptr<Device>>& res);
|
||||
int getDeviceNumByGroup(int category);
|
||||
void getDeviceByGroup(int category, std::vector<std::shared_ptr<Device>>& res);
|
||||
|
||||
Reference in New Issue
Block a user