mirror of
https://github.com/lora-aprs/LoRa_APRS_iGate.git
synced 2025-12-06 07:42:00 +01:00
refactor MQTT task
This commit is contained in:
parent
e9158a44fd
commit
c66d7d7d8f
|
|
@ -9,11 +9,7 @@
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
WiFiClient wiFiClient;
|
MQTTTask::MQTTTask(TaskQueue<std::shared_ptr<APRSMessage>> &toMQTT) : Task(TASK_MQTT, TaskMQTT), _toMQTT(toMQTT), _MQTT(_client) {
|
||||||
PubSubClient _MQTT(wiFiClient);
|
|
||||||
|
|
||||||
|
|
||||||
MQTTTask::MQTTTask(TaskQueue<std::shared_ptr<APRSMessage>> &toMQTT) : Task(TASK_MQTT, TaskMQTT), _beginCalled(false), _toMQTT(toMQTT) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MQTTTask::~MQTTTask() {
|
MQTTTask::~MQTTTask() {
|
||||||
|
|
@ -21,15 +17,10 @@ MQTTTask::~MQTTTask() {
|
||||||
|
|
||||||
bool MQTTTask::setup(System &system) {
|
bool MQTTTask::setup(System &system) {
|
||||||
_MQTT.setServer(system.getUserConfig()->mqtt.server.c_str(), system.getUserConfig()->mqtt.port);
|
_MQTT.setServer(system.getUserConfig()->mqtt.server.c_str(), system.getUserConfig()->mqtt.port);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MQTTTask::loop(System &system) {
|
bool MQTTTask::loop(System &system) {
|
||||||
if (!_beginCalled) {
|
|
||||||
_beginCalled = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!system.isWifiEthConnected()) {
|
if (!system.isWifiEthConnected()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -41,30 +32,30 @@ bool MQTTTask::loop(System &system) {
|
||||||
if (!_toMQTT.empty()) {
|
if (!_toMQTT.empty()) {
|
||||||
std::shared_ptr<APRSMessage> msg = _toMQTT.getElement();
|
std::shared_ptr<APRSMessage> msg = _toMQTT.getElement();
|
||||||
|
|
||||||
DynamicJsonDocument _Data(1024);
|
DynamicJsonDocument data(1024);
|
||||||
String _r;
|
data["Source"] = msg->getSource();
|
||||||
|
data["Destination"] = msg->getDestination();
|
||||||
|
data["Path"] = msg->getPath();
|
||||||
|
data["Type"] = msg->getType().toString();
|
||||||
|
String body = msg->getBody()->encode();
|
||||||
|
body.replace("\n", "");
|
||||||
|
data["Data"] = body;
|
||||||
|
|
||||||
_Data["Source"] = msg->getSource();
|
String r;
|
||||||
_Data["Destination"] = msg->getDestination();
|
serializeJson(data, r);
|
||||||
_Data["Path"] = msg->getPath();
|
|
||||||
_Data["Type"] = msg->getType().toString();
|
|
||||||
String _body = msg->getBody()->encode();
|
|
||||||
_body.replace("\n", "");
|
|
||||||
_Data["Data"] = _body;
|
|
||||||
|
|
||||||
serializeJson(_Data, _r);
|
String topic = String(system.getUserConfig()->mqtt.topic);
|
||||||
|
if (!topic.endsWith("/")) {
|
||||||
logPrintD("Send MQTT: ");
|
topic = topic + "/";
|
||||||
logPrintlnD(_r);
|
|
||||||
|
|
||||||
String _topic = String(system.getUserConfig()->mqtt.topic);
|
|
||||||
|
|
||||||
if (!_topic.endsWith("/")) {
|
|
||||||
_topic = _topic + "/";
|
|
||||||
}
|
}
|
||||||
_topic = _topic + system.getUserConfig()->callsign;
|
topic = topic + system.getUserConfig()->callsign;
|
||||||
|
|
||||||
_MQTT.publish(_topic.c_str(), _r.c_str());
|
logPrintD("Send MQTT with topic: \"");
|
||||||
|
logPrintD(topic);
|
||||||
|
logPrintD("\", data: ");
|
||||||
|
logPrintlnD(r);
|
||||||
|
|
||||||
|
_MQTT.publish(topic.c_str(), r.c_str());
|
||||||
}
|
}
|
||||||
_MQTT.loop();
|
_MQTT.loop();
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -79,10 +70,8 @@ bool MQTTTask::connect(const System &system) {
|
||||||
if (_MQTT.connect(system.getUserConfig()->callsign.c_str(), system.getUserConfig()->mqtt.name.c_str(), system.getUserConfig()->mqtt.password.c_str())) {
|
if (_MQTT.connect(system.getUserConfig()->callsign.c_str(), system.getUserConfig()->mqtt.name.c_str(), system.getUserConfig()->mqtt.password.c_str())) {
|
||||||
logPrintI("Connected to MQTT broker as: ");
|
logPrintI("Connected to MQTT broker as: ");
|
||||||
logPrintlnI(system.getUserConfig()->callsign);
|
logPrintlnI(system.getUserConfig()->callsign);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
|
||||||
logPrintlnI("Connecting to MQTT broker faild. Try again later.");
|
|
||||||
}
|
}
|
||||||
|
logPrintlnI("Connecting to MQTT broker faild. Try again later.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@
|
||||||
#define TASK_MQTT_H_
|
#define TASK_MQTT_H_
|
||||||
|
|
||||||
#include <APRSMessage.h>
|
#include <APRSMessage.h>
|
||||||
#include <TaskManager.h>
|
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
#include <TaskManager.h>
|
||||||
|
|
||||||
class MQTTTask : public Task {
|
class MQTTTask : public Task {
|
||||||
public:
|
public:
|
||||||
|
|
@ -14,8 +14,11 @@ public:
|
||||||
virtual bool loop(System &system) override;
|
virtual bool loop(System &system) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _beginCalled;
|
|
||||||
TaskQueue<std::shared_ptr<APRSMessage>> &_toMQTT;
|
TaskQueue<std::shared_ptr<APRSMessage>> &_toMQTT;
|
||||||
|
|
||||||
|
WiFiClient _client;
|
||||||
|
PubSubClient _MQTT;
|
||||||
|
|
||||||
bool connect(const System &system);
|
bool connect(const System &system);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue