Skip to content

Commit

Permalink
Migrate Datastore to TaskScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Nov 23, 2023
1 parent 12031ed commit c045b5d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 76 deletions.
10 changes: 6 additions & 4 deletions include/Datastore.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once

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

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();
Expand Down Expand Up @@ -58,7 +57,10 @@ class DatastoreClass {
bool getIsAllEnabledReachable();

private:
TimeoutHelper _updateTimeout;
void loop();

Task _loopTask;

std::mutex _mutex;

float _totalAcYieldTotalEnabled = 0;
Expand Down
142 changes: 73 additions & 69 deletions src/Datastore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> 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<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));
}
_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));
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);
}
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()
Expand Down
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void setup()

InverterSettings.init();

Datastore.init();
Datastore.init(&scheduler);
}

void loop()
Expand All @@ -157,8 +157,6 @@ void loop()
yield();
InverterSettings.loop();
yield();
Datastore.loop();
yield();
MqttHandleDtu.loop();
yield();
MqttHandleInverter.loop();
Expand Down

0 comments on commit c045b5d

Please sign in to comment.