Migrate Datastore to TaskScheduler

This commit is contained in:
Thomas Basler 2023-11-20 21:06:07 +01:00
parent 12031ed09e
commit c045b5df48
3 changed files with 87 additions and 83 deletions

View File

@ -1,13 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
#pragma once #pragma once
#include <TimeoutHelper.h> #include <TaskSchedulerDeclarations.h>
#include <mutex> #include <mutex>
class DatastoreClass { class DatastoreClass {
public: public:
void init(); void init(Scheduler* scheduler);
void loop();
// Sum of yield total of all enabled inverters, a inverter which is just disabled at night is also included // Sum of yield total of all enabled inverters, a inverter which is just disabled at night is also included
float getTotalAcYieldTotalEnabled(); float getTotalAcYieldTotalEnabled();
@ -58,7 +57,10 @@ public:
bool getIsAllEnabledReachable(); bool getIsAllEnabledReachable();
private: private:
TimeoutHelper _updateTimeout; void loop();
Task _loopTask;
std::mutex _mutex; std::mutex _mutex;
float _totalAcYieldTotalEnabled = 0; float _totalAcYieldTotalEnabled = 0;

View File

@ -8,105 +8,109 @@
DatastoreClass Datastore; DatastoreClass Datastore;
void DatastoreClass::init() void DatastoreClass::init(Scheduler* scheduler)
{ {
_updateTimeout.set(1000); scheduler->addTask(_loopTask);
_loopTask.setCallback(std::bind(&DatastoreClass::loop, this));
_loopTask.setIterations(TASK_FOREVER);
_loopTask.setInterval(1 * TASK_SECOND);
_loopTask.enable();
} }
void DatastoreClass::loop() void DatastoreClass::loop()
{ {
if (Hoymiles.isAllRadioIdle() && _updateTimeout.occured()) { if (!Hoymiles.isAllRadioIdle()) {
_loopTask.forceNextIteration();
return;
}
uint8_t isProducing = 0; uint8_t isProducing = 0;
uint8_t isReachable = 0; uint8_t isReachable = 0;
uint8_t pollEnabledCount = 0; uint8_t pollEnabledCount = 0;
std::lock_guard<std::mutex> lock(_mutex); std::lock_guard<std::mutex> lock(_mutex);
_totalAcYieldTotalEnabled = 0; _totalAcYieldTotalEnabled = 0;
_totalAcYieldTotalDigits = 0; _totalAcYieldTotalDigits = 0;
_totalAcYieldDayEnabled = 0; _totalAcYieldDayEnabled = 0;
_totalAcYieldDayDigits = 0; _totalAcYieldDayDigits = 0;
_totalAcPowerEnabled = 0; _totalAcPowerEnabled = 0;
_totalAcPowerDigits = 0; _totalAcPowerDigits = 0;
_totalDcPowerEnabled = 0; _totalDcPowerEnabled = 0;
_totalDcPowerDigits = 0; _totalDcPowerDigits = 0;
_totalDcPowerIrradiation = 0; _totalDcPowerIrradiation = 0;
_totalDcIrradiationInstalled = 0; _totalDcIrradiationInstalled = 0;
_isAllEnabledProducing = true; _isAllEnabledProducing = true;
_isAllEnabledReachable = true; _isAllEnabledReachable = true;
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) { for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i); auto inv = Hoymiles.getInverterByPos(i);
if (inv == nullptr) { if (inv == nullptr) {
continue; continue;
} }
auto cfg = Configuration.getInverterConfig(inv->serial()); auto cfg = Configuration.getInverterConfig(inv->serial());
if (cfg == nullptr) { if (cfg == nullptr) {
continue; continue;
} }
if (inv->getEnablePolling()) {
pollEnabledCount++;
}
if (inv->isProducing()) {
isProducing++;
} else {
if (inv->getEnablePolling()) { if (inv->getEnablePolling()) {
pollEnabledCount++; _isAllEnabledProducing = false;
}
if (inv->isProducing()) {
isProducing++;
} else {
if (inv->getEnablePolling()) {
_isAllEnabledProducing = false;
}
}
if (inv->isReachable()) {
isReachable++;
} else {
if (inv->getEnablePolling()) {
_isAllEnabledReachable = false;
}
}
for (auto& c : inv->Statistics()->getChannelsByType(TYPE_AC)) {
if (cfg->Poll_Enable) {
_totalAcYieldTotalEnabled += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_YT);
_totalAcYieldDayEnabled += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_YD);
_totalAcYieldTotalDigits = max<unsigned int>(_totalAcYieldTotalDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_YT));
_totalAcYieldDayDigits = max<unsigned int>(_totalAcYieldDayDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_YD));
}
if (inv->getEnablePolling()) {
_totalAcPowerEnabled += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_PAC);
_totalAcPowerDigits = max<unsigned int>(_totalAcPowerDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_PAC));
}
}
for (auto& c : inv->Statistics()->getChannelsByType(TYPE_DC)) {
if (inv->getEnablePolling()) {
_totalDcPowerEnabled += inv->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC);
_totalDcPowerDigits = max<unsigned int>(_totalDcPowerDigits, inv->Statistics()->getChannelFieldDigits(TYPE_DC, c, FLD_PDC));
if (inv->Statistics()->getStringMaxPower(c) > 0) {
_totalDcPowerIrradiation += inv->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC);
_totalDcIrradiationInstalled += inv->Statistics()->getStringMaxPower(c);
}
}
} }
} }
_isAtLeastOneProducing = isProducing > 0; if (inv->isReachable()) {
_isAtLeastOneReachable = isReachable > 0; isReachable++;
_isAtLeastOnePollEnabled = pollEnabledCount > 0; } else {
if (inv->getEnablePolling()) {
_isAllEnabledReachable = false;
}
}
_totalDcIrradiation = _totalDcIrradiationInstalled > 0 ? _totalDcPowerIrradiation / _totalDcIrradiationInstalled * 100.0f : 0; for (auto& c : inv->Statistics()->getChannelsByType(TYPE_AC)) {
if (cfg->Poll_Enable) {
_totalAcYieldTotalEnabled += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_YT);
_totalAcYieldDayEnabled += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_YD);
_updateTimeout.reset(); _totalAcYieldTotalDigits = max<unsigned int>(_totalAcYieldTotalDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_YT));
_totalAcYieldDayDigits = max<unsigned int>(_totalAcYieldDayDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_YD));
}
if (inv->getEnablePolling()) {
_totalAcPowerEnabled += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_PAC);
_totalAcPowerDigits = max<unsigned int>(_totalAcPowerDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_PAC));
}
}
for (auto& c : inv->Statistics()->getChannelsByType(TYPE_DC)) {
if (inv->getEnablePolling()) {
_totalDcPowerEnabled += inv->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC);
_totalDcPowerDigits = max<unsigned int>(_totalDcPowerDigits, inv->Statistics()->getChannelFieldDigits(TYPE_DC, c, FLD_PDC));
if (inv->Statistics()->getStringMaxPower(c) > 0) {
_totalDcPowerIrradiation += inv->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC);
_totalDcIrradiationInstalled += inv->Statistics()->getStringMaxPower(c);
}
}
}
} }
_isAtLeastOneProducing = isProducing > 0;
_isAtLeastOneReachable = isReachable > 0;
_isAtLeastOnePollEnabled = pollEnabledCount > 0;
_totalDcIrradiation = _totalDcIrradiationInstalled > 0 ? _totalDcPowerIrradiation / _totalDcIrradiationInstalled * 100.0f : 0;
} }
float DatastoreClass::getTotalAcYieldTotalEnabled() float DatastoreClass::getTotalAcYieldTotalEnabled()

View File

@ -146,7 +146,7 @@ void setup()
InverterSettings.init(); InverterSettings.init();
Datastore.init(); Datastore.init(&scheduler);
} }
void loop() void loop()
@ -157,8 +157,6 @@ void loop()
yield(); yield();
InverterSettings.loop(); InverterSettings.loop();
yield(); yield();
Datastore.loop();
yield();
MqttHandleDtu.loop(); MqttHandleDtu.loop();
yield(); yield();
MqttHandleInverter.loop(); MqttHandleInverter.loop();