131 lines
2.4 KiB
C++
131 lines
2.4 KiB
C++
//
|
|
// Created by Thinkpad on 2022/4/22.
|
|
//
|
|
|
|
#ifndef COM_INTERFACE_MQTTCLIENT_H
|
|
#define COM_INTERFACE_MQTTCLIENT_H
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
|
|
#include "common/inc/queue.h"
|
|
#include "mqtt/MQTTClient.h"
|
|
|
|
#define DEV_TYPE_ID "SHB-T"
|
|
|
|
class CMqttClient {
|
|
public:
|
|
CMqttClient(MessageQueue *recvMsgQueue);
|
|
|
|
virtual ~CMqttClient();
|
|
|
|
public:
|
|
|
|
/**
|
|
* 从配置文件中获取网关参数
|
|
*/
|
|
void loadMqttCfgFromFile();
|
|
|
|
/**
|
|
* @brief 从文件中加载MQTT主题
|
|
*/
|
|
bool loadMqttTopicFromFile();
|
|
|
|
/**
|
|
* 发布数据
|
|
* @param strTopic
|
|
* @param strMessage
|
|
*/
|
|
bool publish(const std::string &strTopic, const std::string &strMessage);
|
|
|
|
|
|
/**
|
|
* 连接到服务器
|
|
* @return
|
|
*/
|
|
bool connectServer();
|
|
|
|
/**
|
|
* 接收消息
|
|
* @param strTopic
|
|
* @param strMessage
|
|
*/
|
|
void recvMsg(const std::string &strTopic, const std::string &strMessage);
|
|
|
|
/**
|
|
* 获取连接状态
|
|
* @return
|
|
*/
|
|
bool getConnectStatus();
|
|
|
|
/**
|
|
* 获取网关id
|
|
* @return
|
|
*/
|
|
std::string& getGateWayId();
|
|
|
|
private:
|
|
/**
|
|
* 初始化 从配置文件中读取参数
|
|
*/
|
|
void init();
|
|
|
|
/**
|
|
* 增加订阅主题
|
|
* @param strTopic
|
|
*/
|
|
void addSubTopicToList(const std::string &strTopic);
|
|
|
|
/**
|
|
* 取消订阅主题
|
|
* @param strTopic
|
|
*/
|
|
void rmSubTopic(const std::string &strTopic);
|
|
|
|
/**
|
|
* 订阅
|
|
*/
|
|
void subScribing();
|
|
|
|
/**
|
|
* 清除订阅
|
|
*/
|
|
void cleanSubTopics();
|
|
|
|
/**
|
|
* 断开连接处理
|
|
* @param pContext
|
|
* @param pCause
|
|
*/
|
|
friend void connLostFromMQTT(void *pContext, char *pCause);
|
|
|
|
void printfTopicList();
|
|
void printfMqttCfgInfo();
|
|
private:
|
|
CMqttClient(const CMqttClient &) = delete;
|
|
|
|
CMqttClient &operator=(const CMqttClient &);
|
|
|
|
private:
|
|
|
|
// MQTTClient相关
|
|
void *pClient_;
|
|
std::string strClientID_;
|
|
std::string strUser_;
|
|
std::string strPassword_;
|
|
std::string strServerAddress_;
|
|
std::string strGatewayID_;
|
|
std::vector<std::string> subTopicsList_;
|
|
|
|
// 连接状态
|
|
bool bConnected_;
|
|
bool isTerm_;
|
|
|
|
//接收消息队列
|
|
MessageQueue *recvMsgQueue_;
|
|
};
|
|
|
|
#endif //COM_INTERFACE_MQTTCLIENT_H
|