Use lock_guard to simplify code

This commit is contained in:
Thomas Basler 2023-08-05 01:25:53 +02:00
parent 188a6cf39d
commit 9179b672f5
2 changed files with 35 additions and 82 deletions

View File

@ -2,12 +2,10 @@
#pragma once #pragma once
#include <TimeoutHelper.h> #include <TimeoutHelper.h>
#include <freertos/FreeRTOS.h> #include <mutex>
#include <freertos/semphr.h>
class DatastoreClass { class DatastoreClass {
public: public:
DatastoreClass();
void init(); void init();
void loop(); void loop();
@ -61,7 +59,7 @@ public:
private: private:
TimeoutHelper _updateTimeout; TimeoutHelper _updateTimeout;
SemaphoreHandle_t _xSemaphore; std::mutex _mutex;
float _totalAcYieldTotalEnabled = 0; float _totalAcYieldTotalEnabled = 0;
float _totalAcYieldDayEnabled = 0; float _totalAcYieldDayEnabled = 0;

View File

@ -6,19 +6,8 @@
#include "Configuration.h" #include "Configuration.h"
#include <Hoymiles.h> #include <Hoymiles.h>
#define DAT_SEMAPHORE_TAKE() \
do { \
} while (xSemaphoreTake(_xSemaphore, portMAX_DELAY) != pdPASS)
#define DAT_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)
DatastoreClass Datastore; DatastoreClass Datastore;
DatastoreClass::DatastoreClass()
{
_xSemaphore = xSemaphoreCreateMutex();
DAT_SEMAPHORE_GIVE(); // release before first use
}
void DatastoreClass::init() void DatastoreClass::init()
{ {
_updateTimeout.set(1000); _updateTimeout.set(1000);
@ -32,7 +21,7 @@ void DatastoreClass::loop()
uint8_t isReachable = 0; uint8_t isReachable = 0;
uint8_t pollEnabledCount = 0; uint8_t pollEnabledCount = 0;
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
_totalAcYieldTotalEnabled = 0; _totalAcYieldTotalEnabled = 0;
_totalAcYieldTotalDigits = 0; _totalAcYieldTotalDigits = 0;
@ -116,136 +105,102 @@ void DatastoreClass::loop()
_totalDcIrradiation = _totalDcIrradiationInstalled > 0 ? _totalDcPowerIrradiation / _totalDcIrradiationInstalled * 100.0f : 0; _totalDcIrradiation = _totalDcIrradiationInstalled > 0 ? _totalDcPowerIrradiation / _totalDcIrradiationInstalled * 100.0f : 0;
DAT_SEMAPHORE_GIVE();
_updateTimeout.reset(); _updateTimeout.reset();
} }
} }
float DatastoreClass::getTotalAcYieldTotalEnabled() float DatastoreClass::getTotalAcYieldTotalEnabled()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
float retval = _totalAcYieldTotalEnabled; return _totalAcYieldTotalEnabled;
DAT_SEMAPHORE_GIVE();
return retval;
} }
float DatastoreClass::getTotalAcYieldDayEnabled() float DatastoreClass::getTotalAcYieldDayEnabled()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
float retval = _totalAcYieldDayEnabled; return _totalAcYieldDayEnabled;
DAT_SEMAPHORE_GIVE();
return retval;
} }
float DatastoreClass::getTotalAcPowerEnabled() float DatastoreClass::getTotalAcPowerEnabled()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
float retval = _totalAcPowerEnabled; return _totalAcPowerEnabled;
DAT_SEMAPHORE_GIVE();
return retval;
} }
float DatastoreClass::getTotalDcPowerEnabled() float DatastoreClass::getTotalDcPowerEnabled()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
float retval = _totalDcPowerEnabled; return _totalDcPowerEnabled;
DAT_SEMAPHORE_GIVE();
return retval;
} }
float DatastoreClass::getTotalDcPowerIrradiation() float DatastoreClass::getTotalDcPowerIrradiation()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
float retval = _totalDcPowerIrradiation; return _totalDcPowerIrradiation;
DAT_SEMAPHORE_GIVE();
return retval;
} }
float DatastoreClass::getTotalDcIrradiationInstalled() float DatastoreClass::getTotalDcIrradiationInstalled()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
float retval = _totalDcIrradiationInstalled; return _totalDcIrradiationInstalled;
DAT_SEMAPHORE_GIVE();
return retval;
} }
float DatastoreClass::getTotalDcIrradiation() float DatastoreClass::getTotalDcIrradiation()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
float retval = _totalDcIrradiation; return _totalDcIrradiation;
DAT_SEMAPHORE_GIVE();
return retval;
} }
unsigned int DatastoreClass::getTotalAcYieldTotalDigits() unsigned int DatastoreClass::getTotalAcYieldTotalDigits()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
unsigned int retval = _totalAcYieldTotalDigits; return _totalAcYieldTotalDigits;
DAT_SEMAPHORE_GIVE();
return retval;
} }
unsigned int DatastoreClass::getTotalAcYieldDayDigits() unsigned int DatastoreClass::getTotalAcYieldDayDigits()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
unsigned int retval = _totalAcYieldDayDigits; return _totalAcYieldDayDigits;
DAT_SEMAPHORE_GIVE();
return retval;
} }
unsigned int DatastoreClass::getTotalAcPowerDigits() unsigned int DatastoreClass::getTotalAcPowerDigits()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
unsigned int retval = _totalAcPowerDigits; return _totalAcPowerDigits;
DAT_SEMAPHORE_GIVE();
return retval;
} }
unsigned int DatastoreClass::getTotalDcPowerDigits() unsigned int DatastoreClass::getTotalDcPowerDigits()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
unsigned int retval = _totalDcPowerDigits; return _totalDcPowerDigits;
DAT_SEMAPHORE_GIVE();
return retval;
} }
bool DatastoreClass::getIsAtLeastOneReachable() bool DatastoreClass::getIsAtLeastOneReachable()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
bool retval = _isAtLeastOneReachable; return _isAtLeastOneReachable;
DAT_SEMAPHORE_GIVE();
return retval;
} }
bool DatastoreClass::getIsAtLeastOneProducing() bool DatastoreClass::getIsAtLeastOneProducing()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
bool retval = _isAtLeastOneProducing; return _isAtLeastOneProducing;
DAT_SEMAPHORE_GIVE();
return retval;
} }
bool DatastoreClass::getIsAllEnabledProducing() bool DatastoreClass::getIsAllEnabledProducing()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
bool retval = _isAllEnabledProducing; return _isAllEnabledProducing;
DAT_SEMAPHORE_GIVE();
return retval;
} }
bool DatastoreClass::getIsAllEnabledReachable() bool DatastoreClass::getIsAllEnabledReachable()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
bool retval = _isAllEnabledReachable; return _isAllEnabledReachable;
DAT_SEMAPHORE_GIVE();
return retval;
} }
bool DatastoreClass::getIsAtLeastOnePollEnabled() bool DatastoreClass::getIsAtLeastOnePollEnabled()
{ {
DAT_SEMAPHORE_TAKE(); std::lock_guard<std::mutex> lock(_mutex);
bool retval = _isAtLeastOnePollEnabled; return _isAtLeastOnePollEnabled;
DAT_SEMAPHORE_GIVE();
return retval;
} }