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.
This commit is contained in:
Bernhard Kirchen 2024-06-17 22:00:36 +02:00
parent 6b09ca056e
commit 6b19b877c6
8 changed files with 39 additions and 20 deletions

View File

@ -23,7 +23,7 @@ public:
~PowerMeterHttpJson(); ~PowerMeterHttpJson();
bool init() final; bool init() final;
void loop() final { } // polling is performed asynchronously void loop() final;
float getPowerTotal() const final; float getPowerTotal() const final;
bool isDataValid() const final; bool isDataValid() const final;
void doMqttPublish() const final; void doMqttPublish() const final;

View File

@ -20,7 +20,7 @@ public:
~PowerMeterHttpSml(); ~PowerMeterHttpSml();
bool init() final; bool init() final;
void loop() final { } // polling is performed asynchronously void loop() final;
bool isDataValid() const final; bool isDataValid() const final;
// returns an empty string on success, // returns an empty string on success,

View File

@ -23,7 +23,7 @@ public:
~PowerMeterSerialSdm(); ~PowerMeterSerialSdm();
bool init() final; bool init() final;
void loop() final { } // polling is performed asynchronously void loop() final;
float getPowerTotal() const final; float getPowerTotal() const final;
bool isDataValid() const final; bool isDataValid() const final;
void doMqttPublish() const final; void doMqttPublish() const final;

View File

@ -12,7 +12,7 @@ public:
~PowerMeterSerialSml(); ~PowerMeterSerialSml();
bool init() final; bool init() final;
void loop() final { } // polling is performed asynchronously void loop() final;
private: private:
// we assume that an SML datagram is complete after no additional // we assume that an SML datagram is complete after no additional

View File

@ -48,6 +48,13 @@ bool PowerMeterHttpJson::init()
return false; return false;
} }
return true;
}
void PowerMeterHttpJson::loop()
{
if (_taskHandle != nullptr) { return; }
std::unique_lock<std::mutex> lock(_pollingMutex); std::unique_lock<std::mutex> lock(_pollingMutex);
_stopPolling = false; _stopPolling = false;
lock.unlock(); lock.unlock();
@ -55,8 +62,6 @@ bool PowerMeterHttpJson::init()
uint32_t constexpr stackSize = 3072; uint32_t constexpr stackSize = 3072;
xTaskCreate(PowerMeterHttpJson::pollingLoopHelper, "PM:HTTP+JSON", xTaskCreate(PowerMeterHttpJson::pollingLoopHelper, "PM:HTTP+JSON",
stackSize, this, 1/*prio*/, &_taskHandle); stackSize, this, 1/*prio*/, &_taskHandle);
return true;
} }
void PowerMeterHttpJson::pollingLoopHelper(void* context) void PowerMeterHttpJson::pollingLoopHelper(void* context)

View File

@ -25,16 +25,7 @@ bool PowerMeterHttpSml::init()
{ {
_upHttpGetter = std::make_unique<HttpGetter>(_cfg.HttpRequest); _upHttpGetter = std::make_unique<HttpGetter>(_cfg.HttpRequest);
if (_upHttpGetter->init()) { if (_upHttpGetter->init()) { return true; }
std::unique_lock<std::mutex> lock(_pollingMutex);
_stopPolling = false;
lock.unlock();
uint32_t constexpr stackSize = 3072;
xTaskCreate(PowerMeterHttpSml::pollingLoopHelper, "PM:HTTP+SML",
stackSize, this, 1/*prio*/, &_taskHandle);
return true;
}
MessageOutput.printf("[PowerMeterHttpSml] Initializing HTTP getter failed:\r\n"); MessageOutput.printf("[PowerMeterHttpSml] Initializing HTTP getter failed:\r\n");
MessageOutput.printf("[PowerMeterHttpSml] %s\r\n", _upHttpGetter->getErrorText()); MessageOutput.printf("[PowerMeterHttpSml] %s\r\n", _upHttpGetter->getErrorText());
@ -44,6 +35,19 @@ bool PowerMeterHttpSml::init()
return false; return false;
} }
void PowerMeterHttpSml::loop()
{
if (_taskHandle != nullptr) { return; }
std::unique_lock<std::mutex> 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) void PowerMeterHttpSml::pollingLoopHelper(void* context)
{ {
auto pInstance = static_cast<PowerMeterHttpSml*>(context); auto pInstance = static_cast<PowerMeterHttpSml*>(context);

View File

@ -43,6 +43,13 @@ bool PowerMeterSerialSdm::init()
SWSERIAL_8N1, pin.powermeter_rx, pin.powermeter_tx); SWSERIAL_8N1, pin.powermeter_rx, pin.powermeter_tx);
_upSdm->begin(); _upSdm->begin();
return true;
}
void PowerMeterSerialSdm::loop()
{
if (_taskHandle != nullptr) { return; }
std::unique_lock<std::mutex> lock(_pollingMutex); std::unique_lock<std::mutex> lock(_pollingMutex);
_stopPolling = false; _stopPolling = false;
lock.unlock(); lock.unlock();
@ -50,8 +57,6 @@ bool PowerMeterSerialSdm::init()
uint32_t constexpr stackSize = 3072; uint32_t constexpr stackSize = 3072;
xTaskCreate(PowerMeterSerialSdm::pollingLoopHelper, "PM:SDM", xTaskCreate(PowerMeterSerialSdm::pollingLoopHelper, "PM:SDM",
stackSize, this, 1/*prio*/, &_taskHandle); stackSize, this, 1/*prio*/, &_taskHandle);
return true;
} }
float PowerMeterSerialSdm::getPowerTotal() const float PowerMeterSerialSdm::getPowerTotal() const

View File

@ -23,6 +23,13 @@ bool PowerMeterSerialSml::init()
_upSmlSerial->enableTx(false); _upSmlSerial->enableTx(false);
_upSmlSerial->flush(); _upSmlSerial->flush();
return true;
}
void PowerMeterSerialSml::loop()
{
if (_taskHandle != nullptr) { return; }
std::unique_lock<std::mutex> lock(_pollingMutex); std::unique_lock<std::mutex> lock(_pollingMutex);
_stopPolling = false; _stopPolling = false;
lock.unlock(); lock.unlock();
@ -30,8 +37,6 @@ bool PowerMeterSerialSml::init()
uint32_t constexpr stackSize = 3072; uint32_t constexpr stackSize = 3072;
xTaskCreate(PowerMeterSerialSml::pollingLoopHelper, "PM:SML", xTaskCreate(PowerMeterSerialSml::pollingLoopHelper, "PM:SML",
stackSize, this, 1/*prio*/, &_taskHandle); stackSize, this, 1/*prio*/, &_taskHandle);
return true;
} }
PowerMeterSerialSml::~PowerMeterSerialSml() PowerMeterSerialSml::~PowerMeterSerialSml()