mirror of
https://github.com/g4klx/DMRGateway.git
synced 2026-03-24 13:54:41 +01:00
Add MQTT retain to some JSON messages.
This commit is contained in:
parent
f320e82be4
commit
0e04ebae0f
9
Log.cpp
9
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
4
Log.h
4
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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue