From c045b5df48e50dd4167dc4a7740d8dafb85e6ef4 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Mon, 20 Nov 2023 21:06:07 +0100 Subject: [PATCH] Migrate Datastore to TaskScheduler --- include/Datastore.h | 10 ++-- src/Datastore.cpp | 142 +++++++++++++++++++++++--------------------- src/main.cpp | 4 +- 3 files changed, 80 insertions(+), 76 deletions(-) diff --git a/include/Datastore.h b/include/Datastore.h index 6e4c03964..b667f9d05 100644 --- a/include/Datastore.h +++ b/include/Datastore.h @@ -1,13 +1,12 @@ // SPDX-License-Identifier: GPL-2.0-or-later #pragma once -#include +#include #include class DatastoreClass { public: - void init(); - void loop(); + void init(Scheduler* scheduler); // Sum of yield total of all enabled inverters, a inverter which is just disabled at night is also included float getTotalAcYieldTotalEnabled(); @@ -58,7 +57,10 @@ class DatastoreClass { bool getIsAllEnabledReachable(); private: - TimeoutHelper _updateTimeout; + void loop(); + + Task _loopTask; + std::mutex _mutex; float _totalAcYieldTotalEnabled = 0; diff --git a/src/Datastore.cpp b/src/Datastore.cpp index 4ff67b803..b0be1f628 100644 --- a/src/Datastore.cpp +++ b/src/Datastore.cpp @@ -8,105 +8,109 @@ 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() { - if (Hoymiles.isAllRadioIdle() && _updateTimeout.occured()) { + if (!Hoymiles.isAllRadioIdle()) { + _loopTask.forceNextIteration(); + return; + } - uint8_t isProducing = 0; - uint8_t isReachable = 0; - uint8_t pollEnabledCount = 0; + uint8_t isProducing = 0; + uint8_t isReachable = 0; + uint8_t pollEnabledCount = 0; - std::lock_guard lock(_mutex); + std::lock_guard lock(_mutex); - _totalAcYieldTotalEnabled = 0; - _totalAcYieldTotalDigits = 0; + _totalAcYieldTotalEnabled = 0; + _totalAcYieldTotalDigits = 0; - _totalAcYieldDayEnabled = 0; - _totalAcYieldDayDigits = 0; + _totalAcYieldDayEnabled = 0; + _totalAcYieldDayDigits = 0; - _totalAcPowerEnabled = 0; - _totalAcPowerDigits = 0; + _totalAcPowerEnabled = 0; + _totalAcPowerDigits = 0; - _totalDcPowerEnabled = 0; - _totalDcPowerDigits = 0; + _totalDcPowerEnabled = 0; + _totalDcPowerDigits = 0; - _totalDcPowerIrradiation = 0; - _totalDcIrradiationInstalled = 0; + _totalDcPowerIrradiation = 0; + _totalDcIrradiationInstalled = 0; - _isAllEnabledProducing = true; - _isAllEnabledReachable = true; + _isAllEnabledProducing = true; + _isAllEnabledReachable = true; - for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) { - auto inv = Hoymiles.getInverterByPos(i); - if (inv == nullptr) { - continue; - } + for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) { + auto inv = Hoymiles.getInverterByPos(i); + if (inv == nullptr) { + continue; + } - auto cfg = Configuration.getInverterConfig(inv->serial()); - if (cfg == nullptr) { - continue; - } + auto cfg = Configuration.getInverterConfig(inv->serial()); + if (cfg == nullptr) { + continue; + } - if (inv->getEnablePolling()) { - pollEnabledCount++; - } + if (inv->getEnablePolling()) { + pollEnabledCount++; + } - if (inv->isProducing()) { - isProducing++; - } else { - if (inv->getEnablePolling()) { - _isAllEnabledProducing = false; - } + if (inv->isProducing()) { + isProducing++; + } else { + if (inv->getEnablePolling()) { + _isAllEnabledProducing = false; } + } - if (inv->isReachable()) { - isReachable++; - } else { - if (inv->getEnablePolling()) { - _isAllEnabledReachable = 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); + 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(_totalAcYieldTotalDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_YT)); - _totalAcYieldDayDigits = max(_totalAcYieldDayDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_YD)); - } - if (inv->getEnablePolling()) { - _totalAcPowerEnabled += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_PAC); - _totalAcPowerDigits = max(_totalAcPowerDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_PAC)); - } + _totalAcYieldTotalDigits = max(_totalAcYieldTotalDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_YT)); + _totalAcYieldDayDigits = max(_totalAcYieldDayDigits, inv->Statistics()->getChannelFieldDigits(TYPE_AC, c, FLD_YD)); + } + if (inv->getEnablePolling()) { + _totalAcPowerEnabled += inv->Statistics()->getChannelFieldValue(TYPE_AC, c, FLD_PAC); + _totalAcPowerDigits = max(_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(_totalDcPowerDigits, inv->Statistics()->getChannelFieldDigits(TYPE_DC, c, FLD_PDC)); + for (auto& c : inv->Statistics()->getChannelsByType(TYPE_DC)) { + if (inv->getEnablePolling()) { + _totalDcPowerEnabled += inv->Statistics()->getChannelFieldValue(TYPE_DC, c, FLD_PDC); + _totalDcPowerDigits = max(_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); - } + 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; + _isAtLeastOneProducing = isProducing > 0; + _isAtLeastOneReachable = isReachable > 0; + _isAtLeastOnePollEnabled = pollEnabledCount > 0; - _updateTimeout.reset(); - } + _totalDcIrradiation = _totalDcIrradiationInstalled > 0 ? _totalDcPowerIrradiation / _totalDcIrradiationInstalled * 100.0f : 0; } float DatastoreClass::getTotalAcYieldTotalEnabled() diff --git a/src/main.cpp b/src/main.cpp index 6385b37bc..a7b1fbf40 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -146,7 +146,7 @@ void setup() InverterSettings.init(); - Datastore.init(); + Datastore.init(&scheduler); } void loop() @@ -157,8 +157,6 @@ void loop() yield(); InverterSettings.loop(); yield(); - Datastore.loop(); - yield(); MqttHandleDtu.loop(); yield(); MqttHandleInverter.loop();