From 5c501f879fa71dc68bad7abb7e8f545a61c8903c Mon Sep 17 00:00:00 2001 From: Thomas Basler <thomas@familie-basler.net> Date: Mon, 20 Nov 2023 21:35:56 +0100 Subject: [PATCH] Migrate MqttHandleDtu to TaskScheduler --- include/MqttHandleDtu.h | 8 +++++--- src/MqttHandleDtu.cpp | 28 +++++++++++++++------------- src/main.cpp | 4 +--- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/include/MqttHandleDtu.h b/include/MqttHandleDtu.h index fb5663450..e580542cc 100644 --- a/include/MqttHandleDtu.h +++ b/include/MqttHandleDtu.h @@ -1,15 +1,17 @@ // SPDX-License-Identifier: GPL-2.0-or-later #pragma once +#include <TaskSchedulerDeclarations.h> #include <cstdint> class MqttHandleDtuClass { public: - void init(); - void loop(); + void init(Scheduler* scheduler); private: - uint32_t _lastPublish = 0; + void loop(); + + Task _loopTask; }; extern MqttHandleDtuClass MqttHandleDtu; \ No newline at end of file diff --git a/src/MqttHandleDtu.cpp b/src/MqttHandleDtu.cpp index 000b6c57e..feff63187 100644 --- a/src/MqttHandleDtu.cpp +++ b/src/MqttHandleDtu.cpp @@ -10,27 +10,29 @@ MqttHandleDtuClass MqttHandleDtu; -void MqttHandleDtuClass::init() +void MqttHandleDtuClass::init(Scheduler* scheduler) { + scheduler->addTask(_loopTask); + _loopTask.setCallback(std::bind(&MqttHandleDtuClass::loop, this)); + _loopTask.setIterations(TASK_FOREVER); + _loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND); + _loopTask.enable(); } void MqttHandleDtuClass::loop() { + _loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND); + if (!MqttSettings.getConnected() || !Hoymiles.isAllRadioIdle()) { + _loopTask.forceNextIteration(); return; } - const CONFIG_T& config = Configuration.get(); - - if (millis() - _lastPublish > (config.Mqtt.PublishInterval * 1000)) { - MqttSettings.publish("dtu/uptime", String(millis() / 1000)); - MqttSettings.publish("dtu/ip", NetworkSettings.localIP().toString()); - MqttSettings.publish("dtu/hostname", NetworkSettings.getHostname()); - if (NetworkSettings.NetworkMode() == network_mode::WiFi) { - MqttSettings.publish("dtu/rssi", String(WiFi.RSSI())); - MqttSettings.publish("dtu/bssid", String(WiFi.BSSIDstr())); - } - - _lastPublish = millis(); + MqttSettings.publish("dtu/uptime", String(millis() / 1000)); + MqttSettings.publish("dtu/ip", NetworkSettings.localIP().toString()); + MqttSettings.publish("dtu/hostname", NetworkSettings.getHostname()); + if (NetworkSettings.NetworkMode() == network_mode::WiFi) { + MqttSettings.publish("dtu/rssi", String(WiFi.RSSI())); + MqttSettings.publish("dtu/bssid", String(WiFi.BSSIDstr())); } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 793a4850e..da7f74d5a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -99,7 +99,7 @@ void setup() // Initialize MqTT MessageOutput.print("Initialize MqTT... "); MqttSettings.init(); - MqttHandleDtu.init(); + MqttHandleDtu.init(&scheduler); MqttHandleInverter.init(); MqttHandleInverterTotal.init(&scheduler); MqttHandleHass.init(&scheduler); @@ -157,8 +157,6 @@ void loop() yield(); InverterSettings.loop(); yield(); - MqttHandleDtu.loop(); - yield(); MqttHandleInverter.loop(); yield(); WebApi.loop();