From 86ee7e1a641800a678cab96d8edb571d3b5c833c Mon Sep 17 00:00:00 2001 From: helgeerbe Date: Wed, 14 Jun 2023 11:25:54 +0200 Subject: [PATCH] perf(PowerMeter): query PowerMeter on demand In addition to the cyclic query of the power meters, they will be queried on demand when total power is requested and last update is older than 1 second. --- include/PowerMeter.h | 2 ++ src/PowerMeter.cpp | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/PowerMeter.h b/include/PowerMeter.h index c7a106989..bb40b54fa 100644 --- a/include/PowerMeter.h +++ b/include/PowerMeter.h @@ -60,6 +60,8 @@ class PowerMeterClass { bool mqttInitDone = false; + void readPowerMeter(); + bool smlReadLoop(); const std::list smlHandlerList{ {{0x01, 0x00, 0x10, 0x07, 0x00, 0xff}, &smlOBISW, &_powerMeter1Power}, diff --git a/src/PowerMeter.cpp b/src/PowerMeter.cpp index 0c5e55b87..46a5f3c31 100644 --- a/src/PowerMeter.cpp +++ b/src/PowerMeter.cpp @@ -96,6 +96,11 @@ void PowerMeterClass::onMqttMessage(const espMqttClientTypes::MessageProperties& float PowerMeterClass::getPowerTotal() { + CONFIG_T& config = Configuration.get(); + if (!config.PowerMeter_Enabled + || (millis() - _lastPowerMeterUpdate) < (1000)) { + readPowerMeter(); + } return _powerMeter1Power + _powerMeter2Power + _powerMeter3Power; } @@ -137,6 +142,19 @@ void PowerMeterClass::loop() return; } + readPowerMeter(); + + MessageOutput.printf("PowerMeterClass: TotalPower: %5.2f\r\n", getPowerTotal()); + + mqtt(); + + _lastPowerMeterCheck = millis(); +} + +void PowerMeterClass::readPowerMeter() +{ + CONFIG_T& config = Configuration.get(); + uint8_t _address = config.PowerMeter_SdmAddress; if (config.PowerMeter_Source == SOURCE_SDM1PH) { @@ -169,12 +187,6 @@ void PowerMeterClass::loop() _lastPowerMeterUpdate = millis(); } } - - MessageOutput.printf("PowerMeterClass: TotalPower: %5.2f\r\n", getPowerTotal()); - - mqtt(); - - _lastPowerMeterCheck = millis(); } bool PowerMeterClass::smlReadLoop()