diff --git a/cpp/config.cpp b/cpp/config.cpp new file mode 100644 index 0000000..eff8b1d --- /dev/null +++ b/cpp/config.cpp @@ -0,0 +1,50 @@ +#include "config.hpp" + +#include + +#include +#include +#include + +using namespace std; +using namespace nlohmann; +using namespace iflytop; + +void MQTTConfig::initialize(string file_name) { + std::ifstream jfile(file_name); + jfile >> m_mqttConfigJson; + jfile.close(); + try { + m_address = m_mqttConfigJson["address"]; + m_username = m_mqttConfigJson["username"]; + m_password = m_mqttConfigJson["password"]; + m_clientid = m_mqttConfigJson["clientid"]; + m_qos = m_mqttConfigJson["qos"]; + m_topic = m_mqttConfigJson["topic"]; + m_timeout = m_mqttConfigJson["timeout"]; + } catch (const std::exception& e) { + std::cerr << "mqtt_config_read error: " << e.what() << '\n'; + } +} + +string MQTTConfig::dump() { + stringstream ret; + ret << "==========================mqtt_config_dump_start===========================" << endl; + ret << "address = " << getAddress() << endl; + ret << "username = " << getUsername() << endl; + ret << "password = " << getPassword() << endl; + ret << "clientid = " << getClientid() << endl; + ret << "qos = " << getQos() << endl; + ret << "topic = " << getTopic() << endl; + ret << "timeout = " << getTimeout() << endl; + ret << "===========================mqtt_config_dump_end============================" << endl; + return ret.str(); +} + +const string& MQTTConfig::getAddress() { return m_address; } +const string& MQTTConfig::getUsername() { return m_username; } +const string& MQTTConfig::getPassword() { return m_password; } +const string& MQTTConfig::getClientid() { return m_clientid; } +int MQTTConfig::getQos() { return m_qos; } +const string& MQTTConfig::getTopic() { return m_topic; } +int MQTTConfig::getTimeout() { return m_timeout; } diff --git a/cpp/config.hpp b/cpp/config.hpp new file mode 100644 index 0000000..d08eb52 --- /dev/null +++ b/cpp/config.hpp @@ -0,0 +1,36 @@ +#include +#include +#include + +#include +#include +#include + +namespace iflytop { +using namespace std; +using namespace nlohmann; +class MQTTConfig { + private: + json m_mqttConfigJson; + string m_address; + string m_username; + string m_password; + string m_clientid; + int m_qos; + string m_topic; + int m_timeout; + + public: + MQTTConfig(/* args */){}; + ~MQTTConfig(){}; + void initialize(string file_name); + string dump(); + const string& getAddress(); + const string& getUsername(); + const string& getPassword(); + const string& getClientid(); + int getQos(); + const string& getTopic(); + int getTimeout(); +}; +} // namespace iflytop diff --git a/cpp/main.cpp b/cpp/main.cpp new file mode 100644 index 0000000..56df566 --- /dev/null +++ b/cpp/main.cpp @@ -0,0 +1,94 @@ +// +// Created by zwsd +// +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "MQTTClient.h" +#include "config.hpp" + +using namespace std; +using namespace nlohmann; +using namespace iflytop; + +typedef struct { + string address; + string username; + string password; + string clientid; + int qos; + string topic; + int timeout; +} mqtt_config_t; + +shared_ptr g_mqtt_config; + +static void publish(MQTTClient client, char *topic, char *payload) { + MQTTClient_message message = MQTTClient_message_initializer; + message.payload = payload; + message.payloadlen = strlen(payload); + message.qos = g_mqtt_config->getQos(); + message.retained = 0; + MQTTClient_deliveryToken token; + MQTTClient_publishMessage(client, topic, &message, &token); + MQTTClient_waitForCompletion(client, token, g_mqtt_config->getTimeout()); + printf("Send `%s` to topic `%s` \n", payload, g_mqtt_config->getTopic().c_str()); +} +static int on_message(void *context, char *topicName, int topicLen, MQTTClient_message *message) { + char *payload = (char *)message->payload; + printf("Received `%s` from `%s` topic \n", payload, topicName); + MQTTClient_freeMessage(&message); + MQTTClient_free(topicName); + return 1; +} +int main(int argc, char *argv[]) { + int rc; + MQTTClient client; + MQTTConfig mqttConfig; + + g_mqtt_config = make_shared(); + // 从文件中读取配置 + g_mqtt_config->initialize("mqtt_config.json"); + // 打印配置 + printf("mqtt_config.json: \n%s\n", g_mqtt_config->dump().c_str()); + // 创建客户端 + MQTTClient_create(&client, g_mqtt_config->getAddress().c_str(), g_mqtt_config->getClientid().c_str(), 0, NULL); + MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; + conn_opts.username = g_mqtt_config->getUsername().c_str(); + conn_opts.password = g_mqtt_config->getPassword().c_str(); + // 设置回调函数 + MQTTClient_setCallbacks(client, NULL, NULL, on_message, NULL); + // 连接到MQTT Broker + if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) { + printf("Failed to connect, return code %d\n", rc); + exit(-1); + } else { + printf("Connected to MQTT Broker!\n"); + } + // 订阅主题 + MQTTClient_subscribe(client, g_mqtt_config->getTopic().c_str(), g_mqtt_config->getQos()); + char payload[23]; + for (int i = 0; i < 100; i += 1) { + snprintf(payload, 23, "{\"RunningState\": \"%d\"}", i); + // 发布消息 + publish(client, (char *)g_mqtt_config->getTopic().c_str(), payload); + sleep(1); + } + // 取消订阅 + MQTTClient_disconnect(client, g_mqtt_config->getTimeout()); + // 销毁客户端 + MQTTClient_destroy(&client); + + return 0; +}