mirror of
https://github.com/g4klx/MMDVMHost
synced 2025-12-22 08:05:49 +08:00
Clean up the MQTT interface.
This commit is contained in:
12
Log.cpp
12
Log.cpp
@@ -34,7 +34,6 @@
|
||||
#include <cstring>
|
||||
|
||||
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();
|
||||
|
||||
4
Log.h
4
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
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -23,9 +23,10 @@
|
||||
#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_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;
|
||||
@@ -82,7 +83,10 @@ 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<int>(m_qos), false);
|
||||
if (rc != MOSQ_ERR_SUCCESS) {
|
||||
::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc));
|
||||
return false;
|
||||
|
||||
@@ -23,10 +23,15 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user