mirror of
https://github.com/g4klx/MMDVMHost
synced 2025-12-22 16:25:45 +08:00
Update the MQTT and JSON handling slightly.
This commit is contained in:
2
Log.cpp
2
Log.cpp
@@ -101,7 +101,7 @@ void WriteJSON(const std::string& topLevel, nlohmann::json& json)
|
|||||||
|
|
||||||
top[topLevel] = json;
|
top[topLevel] = json;
|
||||||
|
|
||||||
m_mqtt->publish("json", top.dump().c_str());
|
m_mqtt->publish("json", top.dump());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,19 +84,14 @@ bool CMQTTConnection::publish(const char* topic, const char* text)
|
|||||||
assert(topic != NULL);
|
assert(topic != NULL);
|
||||||
assert(text != NULL);
|
assert(text != NULL);
|
||||||
|
|
||||||
if (!m_connected)
|
return publish(topic, (unsigned char*)text, ::strlen(text));
|
||||||
return false;
|
|
||||||
|
|
||||||
char topicEx[100U];
|
|
||||||
::sprintf(topicEx, "%s/%s", m_name.c_str(), topic);
|
|
||||||
|
|
||||||
int rc = ::mosquitto_publish(m_mosq, NULL, topicEx, ::strlen(text), text, static_cast<int>(m_qos), false);
|
|
||||||
if (rc != MOSQ_ERR_SUCCESS) {
|
|
||||||
::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc));
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
bool CMQTTConnection::publish(const char* topic, const std::string& text)
|
||||||
|
{
|
||||||
|
assert(topic != NULL);
|
||||||
|
|
||||||
|
return publish(topic, (unsigned char*)text.c_str(), text.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CMQTTConnection::publish(const char* topic, const unsigned char* data, unsigned int len)
|
bool CMQTTConnection::publish(const char* topic, const unsigned char* data, unsigned int len)
|
||||||
@@ -107,6 +102,7 @@ bool CMQTTConnection::publish(const char* topic, const unsigned char* data, unsi
|
|||||||
if (!m_connected)
|
if (!m_connected)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if (::strchr(topic, '/') == NULL) {
|
||||||
char topicEx[100U];
|
char topicEx[100U];
|
||||||
::sprintf(topicEx, "%s/%s", m_name.c_str(), topic);
|
::sprintf(topicEx, "%s/%s", m_name.c_str(), topic);
|
||||||
|
|
||||||
@@ -115,6 +111,13 @@ bool CMQTTConnection::publish(const char* topic, const unsigned char* data, unsi
|
|||||||
::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc));
|
::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
int rc = ::mosquitto_publish(m_mosq, NULL, topic, len, data, static_cast<int>(m_qos), false);
|
||||||
|
if (rc != MOSQ_ERR_SUCCESS) {
|
||||||
|
::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ public:
|
|||||||
bool open();
|
bool open();
|
||||||
|
|
||||||
bool publish(const char* topic, const char* text);
|
bool publish(const char* topic, const char* text);
|
||||||
|
bool publish(const char* topic, const std::string& text);
|
||||||
bool publish(const char* topic, const unsigned char* data, unsigned int len);
|
bool publish(const char* topic, const unsigned char* data, unsigned int len);
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
|||||||
Reference in New Issue
Block a user