Files
energy_storage/src/protocol/MqttEntity.h
2025-09-08 19:34:12 +08:00

103 lines
4.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include <string>
#include <vector>
#include <functional>
#include "MQTTAsync.h"
#include "common/JsonN.h"
class Station;
struct REGInfo
{
std::string key;
std::string datatype;
int bytes {0};
std::string remark;
int ratio {1};
REGInfo() {}
REGInfo(std::string key, std::string datatype, std::string remark)
: key(key), datatype(datatype), remark(remark)
{
if (datatype == "uint16" || datatype == "int16") { bytes = 1; }
else if (datatype == "uint32" || datatype == "int32") { bytes = 2; }
}
};
using namespace std;
class MqttClient
{
public:
static void loadDataStruct(std::string filename);
int init(string addr, string clientId, string username, string password);
void destory();
void subscribe();
int publish(string topic, string text);
void onConnectionLost(char* cause);
int onMessageArrived(char* topic, int len, MQTTAsync_message* msg);
void onDeliveryComplete(MQTTAsync_token token);
void onConnectSuccess(MQTTAsync_successData* resp);
void onConnectFaiure(MQTTAsync_failureData* resp);
void parseEMS_YX(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseEMS_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parsePCU_YX(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parsePCU_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parsePCS_YX(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parsePCS_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseBMS_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseBCU_YX(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseBCU_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseMEM_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseTH_YC(std::shared_ptr<Station> station, njson& json, std::map<std::string, REGInfo>& mapRegInfo);
//void parseFire40_YX(std::string& text);
//void parseCooling_YX(std::string& text);
//void parseCooling_YC(std::string& text);
//void parseCharger_YC(std::string& text);
//void parseGateway_YX(std::string& text);
//void parseGateway_YC(std::string& text);
//void parseGateway_YT(std::string& text);
//void parseTQ(std::string& text);
public:
std::string clientId;
MQTTAsync client = nullptr;
std::vector<std::string> vecTopic;
std::string addr; // "tcp://localhost:1883"
int qos {0};
bool isConnected {false};
bool isSubscribed {false};
};
// <数据方向>/<数据格式>/<厂家ID>/<指令>/<设备标识,上行可选>
// Topic 字段说明
// 数据方向 :
//·down:下行,用于管理平台向厂家平台发送指令
// up :上行,用于厂家平台向管理平台推送设备应答或主报数据数据格式:基础格式为json其他格式均为对json字符串进行二次编码
// json : body内的数据采用json编码
// base64 : body内数据先采用ison编码然后再使用base64编码的字符串
// rsa : body内数据先采用json编码然后采用 rsa 算法加密的base64字符串(rsa 密钥由管理平台提供)
// sm2 : body内数据先采用json编码然后采用 sm2 算法加密的base64字符串(sm2 密钥由管理平台提供)
//·厂家ID : 由管理平台定义
//·命令ID : 用于指定读取/设置/控制的具体内容,如读取数据,读取时钟等等,具体参考 交互类指令协议设备标识 : 用于标识设备的唯一id具体定义见3.2中的 context.dev_id 说明,上行时可不提供。
// <数据方向>/<数据格式>/<厂家ID>/<指令>/<设备标识,上行可选>
#define TOPIC_PCS_YC "up/json/预制舱01/PCS_YC"
#define TOPIC_PCS_YC "up/json/预制舱01/PCS_YC"
class MQTT
{
public:
public:
static string pack(std::string name);
};