Clean up the MQTT interface.

This commit is contained in:
Jonathan Naylor
2022-11-29 17:18:34 +00:00
parent 96364136d9
commit 53b73c6558
5 changed files with 25 additions and 21 deletions

12
Log.cpp
View File

@@ -34,7 +34,6 @@
#include <cstring> #include <cstring>
CMQTTPublisher* m_mqtt = NULL; CMQTTPublisher* m_mqtt = NULL;
static std::string m_mqttName;
static unsigned int m_fileLevel = 2U; static unsigned int m_fileLevel = 2U;
static std::string m_filePath; static std::string m_filePath;
@@ -128,7 +127,7 @@ bool LogOpen()
return logOpenNoRotate(); return logOpenNoRotate();
} }
bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, bool rotate, const std::string& mqttName) bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, bool rotate)
{ {
m_filePath = filePath; m_filePath = filePath;
m_fileRoot = fileRoot; m_fileRoot = fileRoot;
@@ -136,7 +135,6 @@ bool LogInitialise(bool daemon, const std::string& filePath, const std::string&
m_displayLevel = displayLevel; m_displayLevel = displayLevel;
m_daemon = daemon; m_daemon = daemon;
m_fileRotate = rotate; m_fileRotate = rotate;
m_mqttName = mqttName;
if (m_daemon) if (m_daemon)
m_displayLevel = 0U; m_displayLevel = 0U;
@@ -176,12 +174,8 @@ void Log(unsigned int level, const char* fmt, ...)
va_end(vl); va_end(vl);
if (m_mqtt != NULL) { if (m_mqtt != NULL)
char topic[100U]; m_mqtt->publish("log", buffer);
::sprintf(topic, "%s/log/%c", m_mqttName.c_str(), LEVELS[level]);
m_mqtt->publish(topic, buffer + 3U);
}
if (level >= m_fileLevel && m_fileLevel != 0U) { if (level >= m_fileLevel && m_fileLevel != 0U) {
bool ret = ::LogOpen(); bool ret = ::LogOpen();

4
Log.h
View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2015,2016,2020,2022 by Jonathan Naylor G4KLX * Copyright (C) 2015,2016,2020 by Jonathan Naylor G4KLX
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -30,7 +30,7 @@
extern void Log(unsigned int level, const char* fmt, ...); extern void Log(unsigned int level, const char* fmt, ...);
extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, bool rotate, const std::string& mqttName = ""); extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel, bool rotate);
extern void LogFinalise(); extern void LogFinalise();
#endif #endif

View File

@@ -271,16 +271,16 @@ int CMMDVMHost::run()
#endif #endif
#if !defined(_WIN32) && !defined(_WIN64) #if !defined(_WIN32) && !defined(_WIN64)
ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate(), m_conf.getMQTTName()); ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate());
#else #else
ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate(), m_conf.getMQTTName()); ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel(), m_conf.getLogFileRotate());
#endif #endif
if (!ret) { if (!ret) {
::fprintf(stderr, "MMDVMHost: unable to open the log file\n"); ::fprintf(stderr, "MMDVMHost: unable to open the log file\n");
return 1; return 1;
} }
m_mqtt = new CMQTTPublisher(m_conf.getMQTTHost(), m_conf.getMQTTPort(), m_conf.getMQTTKeepalive(), 2); m_mqtt = new CMQTTPublisher(m_conf.getMQTTHost(), m_conf.getMQTTPort(), m_conf.getMQTTName(), m_conf.getMQTTKeepalive());
ret = m_mqtt->open(); ret = m_mqtt->open();
if (!ret) { if (!ret) {
::fprintf(stderr, "MMDVMHost: unable to start the MQTT Publisher\n"); ::fprintf(stderr, "MMDVMHost: unable to start the MQTT Publisher\n");

View File

@@ -23,9 +23,10 @@
#include <cstring> #include <cstring>
CMQTTPublisher::CMQTTPublisher(const std::string& host, unsigned short port, unsigned int keepalive, unsigned int qos) : CMQTTPublisher::CMQTTPublisher(const std::string& host, unsigned short port, const std::string& name, unsigned int keepalive, MQTT_QOS qos) :
m_host(host), m_host(host),
m_port(port), m_port(port),
m_name(name),
m_keepalive(keepalive), m_keepalive(keepalive),
m_qos(qos), m_qos(qos),
m_mosq(NULL), m_mosq(NULL),
@@ -33,8 +34,8 @@ m_connected(false)
{ {
assert(!host.empty()); assert(!host.empty());
assert(port > 0U); assert(port > 0U);
assert(!name.empty());
assert(keepalive >= 5U); assert(keepalive >= 5U);
assert(qos >= 0U && qos <= 2U);
::mosquitto_lib_init(); ::mosquitto_lib_init();
} }
@@ -46,7 +47,7 @@ CMQTTPublisher::~CMQTTPublisher()
bool CMQTTPublisher::open() bool CMQTTPublisher::open()
{ {
m_mosq = ::mosquitto_new(NULL, true, this); m_mosq = ::mosquitto_new(m_name.c_str(), true, this);
if (m_mosq == NULL){ if (m_mosq == NULL){
::fprintf(stderr, "MQTT Error newing: Out of memory.\n"); ::fprintf(stderr, "MQTT Error newing: Out of memory.\n");
return false; return false;
@@ -82,7 +83,10 @@ bool CMQTTPublisher::publish(const char* topic, const char* text)
if (!m_connected) if (!m_connected)
return false; return false;
int rc = ::mosquitto_publish(m_mosq, NULL, topic, ::strlen(text), text, m_qos, 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) { if (rc != MOSQ_ERR_SUCCESS) {
::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc)); ::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc));
return false; return false;

View File

@@ -23,10 +23,15 @@
#include <string> #include <string>
enum MQTT_QOS {
MQTT_QOS_AT_MODE_ONCE = 0U,
MQTT_QOS_AT_LEAST_ONCE = 1U,
MQTT_QOS_EXACTLY_ONCE = 2U
};
class CMQTTPublisher { class CMQTTPublisher {
public: public:
CMQTTPublisher(const std::string& host, unsigned short port, unsigned int keepalive, unsigned int qos); CMQTTPublisher(const std::string& host, unsigned short port, const std::string& name, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS_EXACTLY_ONCE);
~CMQTTPublisher(); ~CMQTTPublisher();
bool open(); bool open();
@@ -38,8 +43,9 @@ public:
private: private:
std::string m_host; std::string m_host;
unsigned short m_port; unsigned short m_port;
std::string m_name;
unsigned int m_keepalive; unsigned int m_keepalive;
unsigned int m_qos; MQTT_QOS m_qos;
mosquitto* m_mosq; mosquitto* m_mosq;
bool m_connected; bool m_connected;