From 53b73c6558734702599b44e524ccea5972b6bcd6 Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Tue, 29 Nov 2022 17:18:34 +0000 Subject: [PATCH] Clean up the MQTT interface. --- Log.cpp | 12 +++--------- Log.h | 4 ++-- MMDVMHost.cpp | 6 +++--- MQTTPublisher.cpp | 14 +++++++++----- MQTTPublisher.h | 10 ++++++++-- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Log.cpp b/Log.cpp index a851e5b..58f8840 100644 --- a/Log.cpp +++ b/Log.cpp @@ -34,7 +34,6 @@ #include CMQTTPublisher* m_mqtt = NULL; -static std::string m_mqttName; static unsigned int m_fileLevel = 2U; static std::string m_filePath; @@ -128,7 +127,7 @@ bool LogOpen() 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_fileRoot = fileRoot; @@ -136,7 +135,6 @@ bool LogInitialise(bool daemon, const std::string& filePath, const std::string& m_displayLevel = displayLevel; m_daemon = daemon; m_fileRotate = rotate; - m_mqttName = mqttName; if (m_daemon) m_displayLevel = 0U; @@ -176,12 +174,8 @@ void Log(unsigned int level, const char* fmt, ...) va_end(vl); - if (m_mqtt != NULL) { - char topic[100U]; - ::sprintf(topic, "%s/log/%c", m_mqttName.c_str(), LEVELS[level]); - - m_mqtt->publish(topic, buffer + 3U); - } + if (m_mqtt != NULL) + m_mqtt->publish("log", buffer); if (level >= m_fileLevel && m_fileLevel != 0U) { bool ret = ::LogOpen(); diff --git a/Log.h b/Log.h index fdc50c3..ae95b60 100644 --- a/Log.h +++ b/Log.h @@ -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 * 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 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(); #endif diff --git a/MMDVMHost.cpp b/MMDVMHost.cpp index e3147c1..5da6a7c 100644 --- a/MMDVMHost.cpp +++ b/MMDVMHost.cpp @@ -271,16 +271,16 @@ int CMMDVMHost::run() #endif #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 - 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 if (!ret) { ::fprintf(stderr, "MMDVMHost: unable to open the log file\n"); 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(); if (!ret) { ::fprintf(stderr, "MMDVMHost: unable to start the MQTT Publisher\n"); diff --git a/MQTTPublisher.cpp b/MQTTPublisher.cpp index 7c7a34c..bc4f37a 100644 --- a/MQTTPublisher.cpp +++ b/MQTTPublisher.cpp @@ -23,9 +23,10 @@ #include -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_port(port), +m_name(name), m_keepalive(keepalive), m_qos(qos), m_mosq(NULL), @@ -33,8 +34,8 @@ m_connected(false) { assert(!host.empty()); assert(port > 0U); + assert(!name.empty()); assert(keepalive >= 5U); - assert(qos >= 0U && qos <= 2U); ::mosquitto_lib_init(); } @@ -46,7 +47,7 @@ CMQTTPublisher::~CMQTTPublisher() bool CMQTTPublisher::open() { - m_mosq = ::mosquitto_new(NULL, true, this); + m_mosq = ::mosquitto_new(m_name.c_str(), true, this); if (m_mosq == NULL){ ::fprintf(stderr, "MQTT Error newing: Out of memory.\n"); return false; @@ -81,8 +82,11 @@ bool CMQTTPublisher::publish(const char* topic, const char* text) if (!m_connected) 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(m_qos), false); if (rc != MOSQ_ERR_SUCCESS) { ::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc)); return false; diff --git a/MQTTPublisher.h b/MQTTPublisher.h index 09b8b8b..74ad233 100644 --- a/MQTTPublisher.h +++ b/MQTTPublisher.h @@ -23,10 +23,15 @@ #include +enum MQTT_QOS { + MQTT_QOS_AT_MODE_ONCE = 0U, + MQTT_QOS_AT_LEAST_ONCE = 1U, + MQTT_QOS_EXACTLY_ONCE = 2U +}; class CMQTTPublisher { 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(); bool open(); @@ -38,8 +43,9 @@ public: private: std::string m_host; unsigned short m_port; + std::string m_name; unsigned int m_keepalive; - unsigned int m_qos; + MQTT_QOS m_qos; mosquitto* m_mosq; bool m_connected;