diff --git a/Log.cpp b/Log.cpp index ee493e3..d75d0be 100644 --- a/Log.cpp +++ b/Log.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016,2020,2022,2023,2025 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2020,2022,2023,2025,2026 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 @@ -102,17 +102,16 @@ void WriteJSONStatus(const std::string& status) json["timestamp"] = CUtils::createTimestamp(); json["message"] = status; - WriteJSON("status", json); + WriteJSON("status", json, false); } -void WriteJSON(const std::string& topLevel, nlohmann::json& json) +void WriteJSON(const std::string& topLevel, nlohmann::json& json, bool retain) { if (m_mqtt != nullptr) { nlohmann::json top; top[topLevel] = json; - m_mqtt->publish("json", top.dump()); + m_mqtt->publish("json", top.dump(), retain); } } - diff --git a/Log.h b/Log.h index 736dfdb..ea68a1f 100644 --- a/Log.h +++ b/Log.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016,2020,2022,2023 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2020,2022,2023,2026 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 @@ -36,6 +36,6 @@ extern void LogInitialise(unsigned int displayLevel, unsigned int mqttLevel); extern void LogFinalise(); extern void WriteJSONStatus(const std::string& status); -extern void WriteJSON(const std::string& topLevel, nlohmann::json& json); +extern void WriteJSON(const std::string& topLevel, nlohmann::json& json, bool retain); #endif diff --git a/MQTTConnection.cpp b/MQTTConnection.cpp index b8189c3..f72f708 100644 --- a/MQTTConnection.cpp +++ b/MQTTConnection.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022,2023,2025 by Jonathan Naylor G4KLX + * Copyright (C) 2022,2023,2025,2026 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 @@ -99,22 +99,22 @@ bool CMQTTConnection::open() return true; } -bool CMQTTConnection::publish(const char* topic, const char* text) +bool CMQTTConnection::publish(const char* topic, const char* text, bool retain) { assert(topic != nullptr); assert(text != nullptr); - return publish(topic, (unsigned char*)text, (unsigned int)::strlen(text)); + return publish(topic, (unsigned char*)text, (unsigned int)::strlen(text), retain); } -bool CMQTTConnection::publish(const char* topic, const std::string& text) +bool CMQTTConnection::publish(const char* topic, const std::string& text, bool retain) { assert(topic != nullptr); - return publish(topic, (unsigned char*)text.c_str(), (unsigned int)text.size()); + return publish(topic, (unsigned char*)text.c_str(), (unsigned int)text.size(), retain); } -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, bool retain) { assert(topic != nullptr); assert(data != nullptr); @@ -126,13 +126,13 @@ bool CMQTTConnection::publish(const char* topic, const unsigned char* data, unsi char topicEx[100U]; ::sprintf(topicEx, "%s/%s", m_name.c_str(), topic); - int rc = ::mosquitto_publish(m_mosq, nullptr, topicEx, len, data, static_cast(m_qos), false); + int rc = ::mosquitto_publish(m_mosq, nullptr, topicEx, len, data, static_cast(m_qos), retain); if (rc != MOSQ_ERR_SUCCESS) { ::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc)); return false; } } else { - int rc = ::mosquitto_publish(m_mosq, nullptr, topic, len, data, static_cast(m_qos), false); + int rc = ::mosquitto_publish(m_mosq, nullptr, topic, len, data, static_cast(m_qos), retain); if (rc != MOSQ_ERR_SUCCESS) { ::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc)); return false; diff --git a/MQTTConnection.h b/MQTTConnection.h index 9f718da..8d66bd4 100644 --- a/MQTTConnection.h +++ b/MQTTConnection.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022,2023,2025 by Jonathan Naylor G4KLX + * Copyright (C) 2022,2023,2025,2026 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 @@ -37,9 +37,9 @@ public: bool open(); - 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 char* text, bool retain = false); + bool publish(const char* topic, const std::string& text, bool retain = false); + bool publish(const char* topic, const unsigned char* data, unsigned int len, bool retain = false); void close(); @@ -63,4 +63,3 @@ private: }; #endif - diff --git a/Version.h b/Version.h index 90309c4..fb57f8f 100644 --- a/Version.h +++ b/Version.h @@ -19,6 +19,6 @@ #if !defined(VERSION_H) #define VERSION_H -const char* VERSION = "20260214"; +const char* VERSION = "20260323"; #endif