mirror of
https://github.com/g4klx/MMDVMHost
synced 2025-12-24 01:25:41 +08:00
Handle changes to the MQTT class API.
This commit is contained in:
@@ -355,7 +355,7 @@ int CMMDVMHost::run()
|
|||||||
|
|
||||||
::LogInitialise(m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel());
|
::LogInitialise(m_conf.getLogDisplayLevel(), m_conf.getLogMQTTLevel());
|
||||||
|
|
||||||
std::vector<std::pair<std::string, void (*)(const std::string&)>> subscriptions;
|
std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>> subscriptions;
|
||||||
subscriptions.push_back(std::make_pair("display", CMMDVMHost::onDisplay));
|
subscriptions.push_back(std::make_pair("display", CMMDVMHost::onDisplay));
|
||||||
subscriptions.push_back(std::make_pair("command", CMMDVMHost::onCommand));
|
subscriptions.push_back(std::make_pair("command", CMMDVMHost::onCommand));
|
||||||
|
|
||||||
@@ -3621,24 +3621,27 @@ void CMMDVMHost::writeJSONMessage(const std::string& message)
|
|||||||
WriteJSON("MMDVM", json);
|
WriteJSON("MMDVM", json);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CMMDVMHost::writeSerial(const std::string& message)
|
void CMMDVMHost::writeSerial(const unsigned char* message, unsigned int length)
|
||||||
{
|
{
|
||||||
assert(m_modem != NULL);
|
assert(m_modem != NULL);
|
||||||
|
assert(message != NULL);
|
||||||
|
|
||||||
m_modem->writeSerialData((unsigned char*)message.c_str(), message.length());
|
m_modem->writeSerialData(message, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CMMDVMHost::onCommand(const std::string& command)
|
void CMMDVMHost::onCommand(const unsigned char* command, unsigned int length)
|
||||||
{
|
{
|
||||||
assert(host != NULL);
|
assert(host != NULL);
|
||||||
|
assert(command != NULL);
|
||||||
|
|
||||||
host->remoteControl(command);
|
host->remoteControl(std::string((char*)command, length));
|
||||||
}
|
}
|
||||||
|
|
||||||
void CMMDVMHost::onDisplay(const std::string& message)
|
void CMMDVMHost::onDisplay(const unsigned char* message, unsigned int length)
|
||||||
{
|
{
|
||||||
assert(host != NULL);
|
assert(host != NULL);
|
||||||
|
assert(message != NULL);
|
||||||
|
|
||||||
host->writeSerial(message);
|
host->writeSerial(message, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -221,7 +221,7 @@ private:
|
|||||||
#if defined(USE_AX25)
|
#if defined(USE_AX25)
|
||||||
bool createAX25Network();
|
bool createAX25Network();
|
||||||
#endif
|
#endif
|
||||||
void writeSerial(const std::string& message);
|
void writeSerial(const unsigned char* message, unsigned int length);
|
||||||
|
|
||||||
void remoteControl(const std::string& commandString);
|
void remoteControl(const std::string& commandString);
|
||||||
void processModeCommand(unsigned char mode, unsigned int timeout);
|
void processModeCommand(unsigned char mode, unsigned int timeout);
|
||||||
@@ -235,8 +235,8 @@ private:
|
|||||||
void writeJSONMode(const std::string& mode);
|
void writeJSONMode(const std::string& mode);
|
||||||
void writeJSONMessage(const std::string& message);
|
void writeJSONMessage(const std::string& message);
|
||||||
|
|
||||||
static void onDisplay(const std::string& message);
|
static void onDisplay(const unsigned char* message, unsigned int length);
|
||||||
static void onCommand(const std::string& command);
|
static void onCommand(const unsigned char* command, unsigned int length);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const std::string&)>>& subs, unsigned int keepalive, MQTT_QOS qos) :
|
CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos) :
|
||||||
m_host(host),
|
m_host(host),
|
||||||
m_port(port),
|
m_port(port),
|
||||||
m_name(name),
|
m_name(name),
|
||||||
@@ -145,17 +145,25 @@ void CMQTTConnection::onConnect(mosquitto* mosq, void* obj, int rc)
|
|||||||
CMQTTConnection* p = static_cast<CMQTTConnection*>(obj);
|
CMQTTConnection* p = static_cast<CMQTTConnection*>(obj);
|
||||||
p->m_connected = true;
|
p->m_connected = true;
|
||||||
|
|
||||||
for (std::vector<std::pair<std::string, void (*)(const std::string&)>>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) {
|
for (std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) {
|
||||||
std::string topic = (*it).first;
|
std::string topic = (*it).first;
|
||||||
|
|
||||||
|
if (topic.find_first_of('/') == std::string::npos) {
|
||||||
char topicEx[100U];
|
char topicEx[100U];
|
||||||
::sprintf(topicEx, "%s/%s", p->m_name.c_str(), topic.c_str());
|
::sprintf(topicEx, "%s/%s", p->m_name.c_str(), topic.c_str());
|
||||||
|
|
||||||
rc = ::mosquitto_subscribe(mosq, NULL, topicEx, MQTT_QOS_AT_LEAST_ONCE);
|
rc = ::mosquitto_subscribe(mosq, NULL, topicEx, static_cast<int>(p->m_qos));
|
||||||
if (rc != MOSQ_ERR_SUCCESS) {
|
if (rc != MOSQ_ERR_SUCCESS) {
|
||||||
::fprintf(stderr, "MQTT: error subscribing to %s - %s\n", topicEx, ::mosquitto_strerror(rc));
|
::fprintf(stderr, "MQTT: error subscribing to %s - %s\n", topicEx, ::mosquitto_strerror(rc));
|
||||||
::mosquitto_disconnect(mosq);
|
::mosquitto_disconnect(mosq);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
rc = ::mosquitto_subscribe(mosq, NULL, topic.c_str(), static_cast<int>(p->m_qos));
|
||||||
|
if (rc != MOSQ_ERR_SUCCESS) {
|
||||||
|
::fprintf(stderr, "MQTT: error subscribing to %s - %s\n", topic.c_str(), ::mosquitto_strerror(rc));
|
||||||
|
::mosquitto_disconnect(mosq);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,14 +185,14 @@ void CMQTTConnection::onMessage(mosquitto* mosq, void* obj, const mosquitto_mess
|
|||||||
|
|
||||||
CMQTTConnection* p = static_cast<CMQTTConnection*>(obj);
|
CMQTTConnection* p = static_cast<CMQTTConnection*>(obj);
|
||||||
|
|
||||||
for (std::vector<std::pair<std::string, void (*)(const std::string&)>>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) {
|
for (std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>::const_iterator it = p->m_subs.cbegin(); it != p->m_subs.cend(); ++it) {
|
||||||
std::string topic = (*it).first;
|
std::string topic = (*it).first;
|
||||||
|
|
||||||
char topicEx[100U];
|
char topicEx[100U];
|
||||||
::sprintf(topicEx, "%s/%s", p->m_name.c_str(), topic.c_str());
|
::sprintf(topicEx, "%s/%s", p->m_name.c_str(), topic.c_str());
|
||||||
|
|
||||||
if (::strcmp(topicEx, message->topic) == 0) {
|
if (::strcmp(topicEx, message->topic) == 0) {
|
||||||
(*it).second(std::string((char*)message->payload, message->payloadlen));
|
(*it).second((unsigned char*)message->payload, message->payloadlen);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ enum MQTT_QOS {
|
|||||||
|
|
||||||
class CMQTTConnection {
|
class CMQTTConnection {
|
||||||
public:
|
public:
|
||||||
CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const std::string&)>>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS_EXACTLY_ONCE);
|
CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS_EXACTLY_ONCE);
|
||||||
~CMQTTConnection();
|
~CMQTTConnection();
|
||||||
|
|
||||||
bool open();
|
bool open();
|
||||||
@@ -47,7 +47,7 @@ private:
|
|||||||
std::string m_host;
|
std::string m_host;
|
||||||
unsigned short m_port;
|
unsigned short m_port;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
std::vector<std::pair<std::string, void (*)(const std::string&)>> m_subs;
|
std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>> m_subs;
|
||||||
unsigned int m_keepalive;
|
unsigned int m_keepalive;
|
||||||
MQTT_QOS m_qos;
|
MQTT_QOS m_qos;
|
||||||
mosquitto* m_mosq;
|
mosquitto* m_mosq;
|
||||||
|
|||||||
Reference in New Issue
Block a user