From 6b19b877c6a590b280c3da2b68d0cd687953fad0 Mon Sep 17 00:00:00 2001 From: Bernhard Kirchen Date: Mon, 17 Jun 2024 22:00:36 +0200 Subject: [PATCH] power meter refactoring: split loop task init from init() when performing a test request using the web UI, we need to init() the respective power meter, but we do not want to start the polling task. hence we move initialization of the polling task to the poll() function. it will return if the task is setup already, otherwise setup the task. --- include/PowerMeterHttpJson.h | 2 +- include/PowerMeterHttpSml.h | 2 +- include/PowerMeterSerialSdm.h | 2 +- include/PowerMeterSerialSml.h | 2 +- src/PowerMeterHttpJson.cpp | 9 +++++++-- src/PowerMeterHttpSml.cpp | 24 ++++++++++++++---------- src/PowerMeterSerialSdm.cpp | 9 +++++++-- src/PowerMeterSerialSml.cpp | 9 +++++++-- 8 files changed, 39 insertions(+), 20 deletions(-) diff --git a/include/PowerMeterHttpJson.h b/include/PowerMeterHttpJson.h index d226bbc0..a6c97a4c 100644 --- a/include/PowerMeterHttpJson.h +++ b/include/PowerMeterHttpJson.h @@ -23,7 +23,7 @@ public: ~PowerMeterHttpJson(); bool init() final; - void loop() final { } // polling is performed asynchronously + void loop() final; float getPowerTotal() const final; bool isDataValid() const final; void doMqttPublish() const final; diff --git a/include/PowerMeterHttpSml.h b/include/PowerMeterHttpSml.h index 823a4b76..9c49b639 100644 --- a/include/PowerMeterHttpSml.h +++ b/include/PowerMeterHttpSml.h @@ -20,7 +20,7 @@ public: ~PowerMeterHttpSml(); bool init() final; - void loop() final { } // polling is performed asynchronously + void loop() final; bool isDataValid() const final; // returns an empty string on success, diff --git a/include/PowerMeterSerialSdm.h b/include/PowerMeterSerialSdm.h index 941d2132..d676fcbb 100644 --- a/include/PowerMeterSerialSdm.h +++ b/include/PowerMeterSerialSdm.h @@ -23,7 +23,7 @@ public: ~PowerMeterSerialSdm(); bool init() final; - void loop() final { } // polling is performed asynchronously + void loop() final; float getPowerTotal() const final; bool isDataValid() const final; void doMqttPublish() const final; diff --git a/include/PowerMeterSerialSml.h b/include/PowerMeterSerialSml.h index 613dace5..abe59533 100644 --- a/include/PowerMeterSerialSml.h +++ b/include/PowerMeterSerialSml.h @@ -12,7 +12,7 @@ public: ~PowerMeterSerialSml(); bool init() final; - void loop() final { } // polling is performed asynchronously + void loop() final; private: // we assume that an SML datagram is complete after no additional diff --git a/src/PowerMeterHttpJson.cpp b/src/PowerMeterHttpJson.cpp index a7164408..3d4431f3 100644 --- a/src/PowerMeterHttpJson.cpp +++ b/src/PowerMeterHttpJson.cpp @@ -48,6 +48,13 @@ bool PowerMeterHttpJson::init() return false; } + return true; +} + +void PowerMeterHttpJson::loop() +{ + if (_taskHandle != nullptr) { return; } + std::unique_lock lock(_pollingMutex); _stopPolling = false; lock.unlock(); @@ -55,8 +62,6 @@ bool PowerMeterHttpJson::init() uint32_t constexpr stackSize = 3072; xTaskCreate(PowerMeterHttpJson::pollingLoopHelper, "PM:HTTP+JSON", stackSize, this, 1/*prio*/, &_taskHandle); - - return true; } void PowerMeterHttpJson::pollingLoopHelper(void* context) diff --git a/src/PowerMeterHttpSml.cpp b/src/PowerMeterHttpSml.cpp index 7149e12a..8cfd1480 100644 --- a/src/PowerMeterHttpSml.cpp +++ b/src/PowerMeterHttpSml.cpp @@ -25,16 +25,7 @@ bool PowerMeterHttpSml::init() { _upHttpGetter = std::make_unique(_cfg.HttpRequest); - if (_upHttpGetter->init()) { - std::unique_lock lock(_pollingMutex); - _stopPolling = false; - lock.unlock(); - - uint32_t constexpr stackSize = 3072; - xTaskCreate(PowerMeterHttpSml::pollingLoopHelper, "PM:HTTP+SML", - stackSize, this, 1/*prio*/, &_taskHandle); - return true; - } + if (_upHttpGetter->init()) { return true; } MessageOutput.printf("[PowerMeterHttpSml] Initializing HTTP getter failed:\r\n"); MessageOutput.printf("[PowerMeterHttpSml] %s\r\n", _upHttpGetter->getErrorText()); @@ -44,6 +35,19 @@ bool PowerMeterHttpSml::init() return false; } +void PowerMeterHttpSml::loop() +{ + if (_taskHandle != nullptr) { return; } + + std::unique_lock lock(_pollingMutex); + _stopPolling = false; + lock.unlock(); + + uint32_t constexpr stackSize = 3072; + xTaskCreate(PowerMeterHttpSml::pollingLoopHelper, "PM:HTTP+SML", + stackSize, this, 1/*prio*/, &_taskHandle); +} + void PowerMeterHttpSml::pollingLoopHelper(void* context) { auto pInstance = static_cast(context); diff --git a/src/PowerMeterSerialSdm.cpp b/src/PowerMeterSerialSdm.cpp index fea191b5..40514427 100644 --- a/src/PowerMeterSerialSdm.cpp +++ b/src/PowerMeterSerialSdm.cpp @@ -43,6 +43,13 @@ bool PowerMeterSerialSdm::init() SWSERIAL_8N1, pin.powermeter_rx, pin.powermeter_tx); _upSdm->begin(); + return true; +} + +void PowerMeterSerialSdm::loop() +{ + if (_taskHandle != nullptr) { return; } + std::unique_lock lock(_pollingMutex); _stopPolling = false; lock.unlock(); @@ -50,8 +57,6 @@ bool PowerMeterSerialSdm::init() uint32_t constexpr stackSize = 3072; xTaskCreate(PowerMeterSerialSdm::pollingLoopHelper, "PM:SDM", stackSize, this, 1/*prio*/, &_taskHandle); - - return true; } float PowerMeterSerialSdm::getPowerTotal() const diff --git a/src/PowerMeterSerialSml.cpp b/src/PowerMeterSerialSml.cpp index 123a10dd..a6af3ccb 100644 --- a/src/PowerMeterSerialSml.cpp +++ b/src/PowerMeterSerialSml.cpp @@ -23,6 +23,13 @@ bool PowerMeterSerialSml::init() _upSmlSerial->enableTx(false); _upSmlSerial->flush(); + return true; +} + +void PowerMeterSerialSml::loop() +{ + if (_taskHandle != nullptr) { return; } + std::unique_lock lock(_pollingMutex); _stopPolling = false; lock.unlock(); @@ -30,8 +37,6 @@ bool PowerMeterSerialSml::init() uint32_t constexpr stackSize = 3072; xTaskCreate(PowerMeterSerialSml::pollingLoopHelper, "PM:SML", stackSize, this, 1/*prio*/, &_taskHandle); - - return true; } PowerMeterSerialSml::~PowerMeterSerialSml()