Skip to content

Commit

Permalink
Migrate MqttHandleDtu to TaskScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Nov 23, 2023
1 parent 48a27fb commit 5c501f8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
8 changes: 5 additions & 3 deletions include/MqttHandleDtu.h
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 15 additions & 13 deletions src/MqttHandleDtu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -157,8 +157,6 @@ void loop()
yield();
InverterSettings.loop();
yield();
MqttHandleDtu.loop();
yield();
MqttHandleInverter.loop();
yield();
WebApi.loop();
Expand Down

0 comments on commit 5c501f8

Please sign in to comment.