code_app/app/scanner2/mqtt.cpp

140 lines
4.2 KiB
C++

#include "mqtt.h"
#include "base/HGDef.h"
#if defined(HG_CMP_MSC)
#include "mosquitto.h"
#endif
#include <string.h>
#include <string>
#include "app_cfg.h"
static void on_connect_wrapper(struct mosquitto *mosq, void *userdata, int rc)
{
}
static void on_connect_with_flags_wrapper(struct mosquitto *mosq, void *userdata, int rc, int flags)
{
}
static void on_disconnect_wrapper(struct mosquitto *mosq, void *userdata, int rc)
{
}
static void on_publish_wrapper(struct mosquitto *mosq, void *userdata, int mid)
{
}
static void on_message_wrapper(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
{
}
static void on_subscribe_wrapper(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
{
}
static void on_unsubscribe_wrapper(struct mosquitto *mosq, void *userdata, int mid)
{
}
static void on_log_wrapper(struct mosquitto *mosq, void *userdata, int level, const char *str)
{
}
void mqtt_init()
{
#if defined(HG_CMP_MSC)
mosquitto_lib_init();
#endif
}
void mqtt_deinit()
{
#if defined(HG_CMP_MSC)
mosquitto_lib_cleanup();
#endif
}
bool mqtt_publish(const char *title, const char *info, bool will_set)
{
int mqttEnable = getCfgValue("mqtt", "enable", 0);
if (!mqttEnable)
{
return false;
}
bool ret = false;
#if defined(HG_CMP_MSC)
struct mosquitto *mosq = mosquitto_new(NULL, true, NULL);
if (NULL != mosq)
{
mosquitto_connect_callback_set(mosq, on_connect_wrapper);
mosquitto_connect_with_flags_callback_set(mosq, on_connect_with_flags_wrapper);
mosquitto_disconnect_callback_set(mosq, on_disconnect_wrapper);
mosquitto_publish_callback_set(mosq, on_publish_wrapper);
mosquitto_message_callback_set(mosq, on_message_wrapper);
mosquitto_subscribe_callback_set(mosq, on_subscribe_wrapper);
mosquitto_unsubscribe_callback_set(mosq, on_unsubscribe_wrapper);
mosquitto_log_callback_set(mosq, on_log_wrapper);
int rc = mosquitto_username_pw_set(mosq, "ynhxy", "KbRtHt#9Lk");
if (will_set)
rc = mosquitto_will_set(mosq, title, strlen(info), info, 0, false);
rc = mosquitto_connect(mosq, "iot-hardware.139zhxy.cn", 31883, 60);
if (MOSQ_ERR_SUCCESS == rc)
{
rc = mosquitto_loop(mosq, -1, 1);
if (MOSQ_ERR_SUCCESS == rc)
{
rc = mosquitto_publish(mosq, NULL, title, strlen(info), info, 0, false);
if (MOSQ_ERR_SUCCESS == rc)
ret = true;
mosquitto_loop(mosq, -1, 1);
}
}
mosquitto_disconnect(mosq);
mosquitto_destroy(mosq);
}
#endif
return ret;
}
bool mqtt_pulish_online()
{
std::string productId = getCfgValue("mqtt", "productId", QString("GSYJ-BZ")).toStdString();
std::string deviceId = getCfgValue("mqtt", "deviceId", QString("testID")).toStdString();
std::string title = "/" + productId + "/" + deviceId + "/online";
std::string info = "{\"deviceId\": \"" + deviceId + "\"}";
return mqtt_publish(title.c_str(), info.c_str(), false);
}
bool mqtt_pulish_offline()
{
std::string productId = getCfgValue("mqtt", "productId", QString("GSYJ-BZ")).toStdString();
std::string deviceId = getCfgValue("mqtt", "deviceId", QString("testID")).toStdString();
std::string title = "/" + productId + "/" + deviceId + "/offline";
std::string info = "{\"deviceId\": \"" + deviceId + "\"}";
return mqtt_publish(title.c_str(), info.c_str(), true);
}
bool mqtt_publish_device_properties(const char *ip, const char *sysVerion,
const char *deviceModel, const char *companyCode)
{
std::string productId = getCfgValue("mqtt", "productId", QString("GSYJ-BZ")).toStdString();
std::string deviceId = getCfgValue("mqtt", "deviceId", QString("testID")).toStdString();
std::string title = "/" + productId + "/" + deviceId + "/properties/report";
std::string info = "{\"deviceId\": \"" + deviceId + "\", \"properties\": {\"ip\": \""
+ ip + "\", \"sysVersion\": \"" + sysVerion + "\", \"deviceModel\": \"" + deviceModel + "\", \"companyCode\": \"" + companyCode + "\"}}";
return mqtt_publish(title.c_str(), info.c_str(), false);
}