Add MQTT retain to the JSON messages.

This commit is contained in:
Jonathan Naylor 2026-03-23 17:11:12 +00:00
parent 5f97588f51
commit c05dbb9998
6 changed files with 21 additions and 21 deletions

View file

@ -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
@ -94,14 +94,14 @@ void Log(unsigned int level, const char* fmt, ...)
exit(1);
}
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);
}
}

View file

@ -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
@ -35,6 +35,6 @@ extern void Log(unsigned int level, const char* fmt, ...);
extern void LogInitialise(unsigned int displayLevel, unsigned int mqttLevel);
extern void LogFinalise();
extern void WriteJSON(const std::string& topLevel, nlohmann::json& json);
extern void WriteJSON(const std::string& topLevel, nlohmann::json& json, bool retain);
#endif

View file

@ -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<int>(m_qos), false);
int rc = ::mosquitto_publish(m_mosq, nullptr, topicEx, len, data, static_cast<int>(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<int>(m_qos), false);
int rc = ::mosquitto_publish(m_mosq, nullptr, topic, len, data, static_cast<int>(m_qos), retain);
if (rc != MOSQ_ERR_SUCCESS) {
::fprintf(stderr, "MQTT Error publishing: %s\n", ::mosquitto_strerror(rc));
return false;

View file

@ -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();

View file

@ -734,7 +734,7 @@ void CNXDNGateway::writeJSONStatus(const std::string& status)
json["timestamp"] = CUtils::createTimestamp();
json["message"] = status;
WriteJSON("status", json);
WriteJSON("status", json, false);
}
void CNXDNGateway::writeJSONLinking(const std::string& reason, unsigned short tg)
@ -746,7 +746,7 @@ void CNXDNGateway::writeJSONLinking(const std::string& reason, unsigned short tg
json["reason"] = reason;
json["talkgroup"] = int(tg);
WriteJSON("link", json);
WriteJSON("link", json, true);
}
void CNXDNGateway::writeJSONUnlinked(const std::string& reason)
@ -757,7 +757,7 @@ void CNXDNGateway::writeJSONUnlinked(const std::string& reason)
json["action"] = "unlinked";
json["reason"] = reason;
WriteJSON("link", json);
WriteJSON("link", json, true);
}
void CNXDNGateway::writeJSONRelinking(unsigned short tg)
@ -768,7 +768,7 @@ void CNXDNGateway::writeJSONRelinking(unsigned short tg)
json["action"] = "relinking";
json["talkgroup"] = int(tg);
WriteJSON("link", json);
WriteJSON("link", json, true);
}
void CNXDNGateway::onCommand(const unsigned char* command, unsigned int length)

View file

@ -19,6 +19,6 @@
#if !defined(VERSION_H)
#define VERSION_H
const char* VERSION = "20260215";
const char* VERSION = "20260323";
#endif