Skip to content

Commit

Permalink
Migrate MqttHandleInverterTotal to TaskSchedule
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Nov 23, 2023
1 parent c045b5d commit 98c30d1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
9 changes: 5 additions & 4 deletions include/MqttHandleInverterTotal.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

#include <TimeoutHelper.h>
#include <TaskSchedulerDeclarations.h>

class MqttHandleInverterTotalClass {
public:
void init();
void loop();
void init(Scheduler* scheduler);

private:
TimeoutHelper _lastPublish;
void loop();

Task _loopTask;
};

extern MqttHandleInverterTotalClass MqttHandleInverterTotal;
30 changes: 17 additions & 13 deletions src/MqttHandleInverterTotal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@

MqttHandleInverterTotalClass MqttHandleInverterTotal;

void MqttHandleInverterTotalClass::init()
void MqttHandleInverterTotalClass::init(Scheduler* scheduler)
{
_lastPublish.set(Configuration.get().Mqtt.PublishInterval * 1000);
scheduler->addTask(_loopTask);
_loopTask.setCallback(std::bind(&MqttHandleInverterTotalClass::loop, this));
_loopTask.setIterations(TASK_FOREVER);
_loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND);
_loopTask.enable();
}

void MqttHandleInverterTotalClass::loop()
{
// Update interval from config
_loopTask.setInterval(Configuration.get().Mqtt.PublishInterval * TASK_SECOND);

if (!MqttSettings.getConnected() || !Hoymiles.isAllRadioIdle()) {
_loopTask.forceNextIteration();
return;
}

if (_lastPublish.occured()) {
MqttSettings.publish("ac/power", String(Datastore.getTotalAcPowerEnabled(), Datastore.getTotalAcPowerDigits()));
MqttSettings.publish("ac/yieldtotal", String(Datastore.getTotalAcYieldTotalEnabled(), Datastore.getTotalAcYieldTotalDigits()));
MqttSettings.publish("ac/yieldday", String(Datastore.getTotalAcYieldDayEnabled(), Datastore.getTotalAcYieldDayDigits()));
MqttSettings.publish("ac/is_valid", String(Datastore.getIsAllEnabledReachable()));
MqttSettings.publish("dc/power", String(Datastore.getTotalDcPowerEnabled(), Datastore.getTotalDcPowerDigits()));
MqttSettings.publish("dc/irradiation", String(Datastore.getTotalDcIrradiation(), 3));
MqttSettings.publish("dc/is_valid", String(Datastore.getIsAllEnabledReachable()));

_lastPublish.set(Configuration.get().Mqtt.PublishInterval * 1000);
}
MqttSettings.publish("ac/power", String(Datastore.getTotalAcPowerEnabled(), Datastore.getTotalAcPowerDigits()));
MqttSettings.publish("ac/yieldtotal", String(Datastore.getTotalAcYieldTotalEnabled(), Datastore.getTotalAcYieldTotalDigits()));
MqttSettings.publish("ac/yieldday", String(Datastore.getTotalAcYieldDayEnabled(), Datastore.getTotalAcYieldDayDigits()));
MqttSettings.publish("ac/is_valid", String(Datastore.getIsAllEnabledReachable()));
MqttSettings.publish("dc/power", String(Datastore.getTotalDcPowerEnabled(), Datastore.getTotalDcPowerDigits()));
MqttSettings.publish("dc/irradiation", String(Datastore.getTotalDcIrradiation(), 3));
MqttSettings.publish("dc/is_valid", String(Datastore.getIsAllEnabledReachable()));
}
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void setup()
MqttSettings.init();
MqttHandleDtu.init();
MqttHandleInverter.init();
MqttHandleInverterTotal.init();
MqttHandleInverterTotal.init(&scheduler);
MqttHandleHass.init();
MessageOutput.println("done");

Expand Down Expand Up @@ -161,8 +161,6 @@ void loop()
yield();
MqttHandleInverter.loop();
yield();
MqttHandleInverterTotal.loop();
yield();
MqttHandleHass.loop();
yield();
WebApi.loop();
Expand Down

0 comments on commit 98c30d1

Please sign in to comment.