Implemented last will and birth message on MQTT

pull/273/head
Thomas Lohmueller 2023-02-22 21:13:40 +01:00
rodzic 5c55905e69
commit dcb90404e8
4 zmienionych plików z 41 dodań i 13 usunięć

Wyświetl plik

@ -73,7 +73,11 @@
"port": 1883,
"name": "",
"password": "",
"topic": "LoraAPRS/Data"
"topic": "LoraAPRS/Data",
"will_active": false,
"will_topic": "LoraAPRS/State",
"will_message": "offline",
"birth_message": "online"
},
"syslog": {
"active": false,

Wyświetl plik

@ -54,9 +54,19 @@ bool MQTTTask::loop(System &system) {
}
bool MQTTTask::connect(System &system) {
bool result = false;
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, getName(), "Connecting to MQTT broker: %s on port %d", system.getUserConfig()->mqtt.server.c_str(), system.getUserConfig()->mqtt.port);
if (_MQTT.connect(system.getUserConfig()->callsign.c_str(), system.getUserConfig()->mqtt.name.c_str(), system.getUserConfig()->mqtt.password.c_str())) {
if (system.getUserConfig()->mqtt.will_active) {
result = _MQTT.connect(system.getUserConfig()->callsign.c_str(), system.getUserConfig()->mqtt.name.c_str(), system.getUserConfig()->mqtt.password.c_str(), system.getUserConfig()->mqtt.will_topic.c_str(), 0, true, system.getUserConfig()->mqtt.will_message.c_str());
} else {
result = _MQTT.connect(system.getUserConfig()->callsign.c_str(), system.getUserConfig()->mqtt.name.c_str(), system.getUserConfig()->mqtt.password.c_str());
}
if (result) {
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, getName(), "Connected to MQTT broker as: %s", system.getUserConfig()->callsign.c_str());
if (system.getUserConfig()->mqtt.will_active) {
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, getName(), "Sending birth message to MQTT.");
_MQTT.publish(system.getUserConfig()->mqtt.will_topic.c_str(), system.getUserConfig()->mqtt.birth_message.c_str(), true);
}
return true;
}
system.getLogger().log(logging::LoggerLevel::LOGGER_LEVEL_INFO, getName(), "Connecting to MQTT broker failed. Try again later.");

Wyświetl plik

@ -97,6 +97,13 @@ void ProjectConfigurationManagement::readProjectConfiguration(DynamicJsonDocumen
conf.mqtt.password = data["mqtt"]["password"].as<String>();
if (data["mqtt"].containsKey("topic"))
conf.mqtt.topic = data["mqtt"]["topic"].as<String>();
conf.mqtt.will_active = data["mqtt"]["will_active"] | false;
if (data["mqtt"].containsKey("will_topic"))
conf.mqtt.will_topic = data["mqtt"]["will_topic"].as<String>();
if (data["mqtt"].containsKey("will_message"))
conf.mqtt.will_message = data["mqtt"]["will_message"].as<String>();
if (data["mqtt"].containsKey("birth_message"))
conf.mqtt.birth_message = data["mqtt"]["birth_message"].as<String>();
conf.syslog.active = data["syslog"]["active"] | true;
if (data["syslog"].containsKey("server"))
@ -161,16 +168,19 @@ void ProjectConfigurationManagement::writeProjectConfiguration(Configuration &co
v["name"] = u.name;
v["password"] = u.password;
}
data["mqtt"]["active"] = conf.mqtt.active;
data["mqtt"]["server"] = conf.mqtt.server;
data["mqtt"]["port"] = conf.mqtt.port;
data["mqtt"]["name"] = conf.mqtt.name;
data["mqtt"]["password"] = conf.mqtt.password;
data["mqtt"]["topic"] = conf.mqtt.topic;
data["syslog"]["active"] = conf.syslog.active;
data["syslog"]["server"] = conf.syslog.server;
data["syslog"]["port"] = conf.syslog.port;
data["ntp_server"] = conf.ntpServer;
data["mqtt"]["active"] = conf.mqtt.active;
data["mqtt"]["server"] = conf.mqtt.server;
data["mqtt"]["port"] = conf.mqtt.port;
data["mqtt"]["name"] = conf.mqtt.name;
data["mqtt"]["password"] = conf.mqtt.password;
data["mqtt"]["topic"] = conf.mqtt.topic;
data["mqtt"]["will_active"] = conf.mqtt.will_active;
data["mqtt"]["will_topic"] = conf.mqtt.will_topic;
data["mqtt"]["birth_message"] = conf.mqtt.birth_message;
data["syslog"]["active"] = conf.syslog.active;
data["syslog"]["server"] = conf.syslog.server;
data["syslog"]["port"] = conf.syslog.port;
data["ntp_server"] = conf.ntpServer;
data["board"] = conf.board;
}

Wyświetl plik

@ -121,7 +121,7 @@ public:
class MQTT {
public:
MQTT() : active(false), server(""), port(1883), name(""), password(""), topic("LoraAPRS/Data") {
MQTT() : active(false), server(""), port(1883), name(""), password(""), topic("LoraAPRS/Data"), will_active(false), will_topic("LoraAPRS/State"), will_message("offline"),birth_message("online") {
}
bool active;
@ -130,6 +130,10 @@ public:
String name;
String password;
String topic;
bool will_active;
String will_topic;
String will_message;
String birth_message;
};
class Syslog {