Skip to content

Commit

Permalink
Use lock_guard to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
tbnobody committed Aug 4, 2023
1 parent 188a6cf commit 9179b67
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 82 deletions.
6 changes: 2 additions & 4 deletions include/Datastore.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
#pragma once

#include <TimeoutHelper.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <mutex>

class DatastoreClass {
public:
DatastoreClass();
void init();
void loop();

Expand Down Expand Up @@ -61,7 +59,7 @@ class DatastoreClass {

private:
TimeoutHelper _updateTimeout;
SemaphoreHandle_t _xSemaphore;
std::mutex _mutex;

float _totalAcYieldTotalEnabled = 0;
float _totalAcYieldDayEnabled = 0;
Expand Down
111 changes: 33 additions & 78 deletions src/Datastore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,8 @@
#include "Configuration.h"
#include <Hoymiles.h>

#define DAT_SEMAPHORE_TAKE() \
do { \
} while (xSemaphoreTake(_xSemaphore, portMAX_DELAY) != pdPASS)
#define DAT_SEMAPHORE_GIVE() xSemaphoreGive(_xSemaphore)

DatastoreClass Datastore;

DatastoreClass::DatastoreClass()
{
_xSemaphore = xSemaphoreCreateMutex();
DAT_SEMAPHORE_GIVE(); // release before first use
}

void DatastoreClass::init()
{
_updateTimeout.set(1000);
Expand All @@ -32,7 +21,7 @@ void DatastoreClass::loop()
uint8_t isReachable = 0;
uint8_t pollEnabledCount = 0;

DAT_SEMAPHORE_TAKE();
std::lock_guard<std::mutex> lock(_mutex);

_totalAcYieldTotalEnabled = 0;
_totalAcYieldTotalDigits = 0;
Expand Down Expand Up @@ -116,136 +105,102 @@ void DatastoreClass::loop()

_totalDcIrradiation = _totalDcIrradiationInstalled > 0 ? _totalDcPowerIrradiation / _totalDcIrradiationInstalled * 100.0f : 0;

DAT_SEMAPHORE_GIVE();

_updateTimeout.reset();
}
}

float DatastoreClass::getTotalAcYieldTotalEnabled()
{
DAT_SEMAPHORE_TAKE();
float retval = _totalAcYieldTotalEnabled;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalAcYieldTotalEnabled;
}

float DatastoreClass::getTotalAcYieldDayEnabled()
{
DAT_SEMAPHORE_TAKE();
float retval = _totalAcYieldDayEnabled;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalAcYieldDayEnabled;
}

float DatastoreClass::getTotalAcPowerEnabled()
{
DAT_SEMAPHORE_TAKE();
float retval = _totalAcPowerEnabled;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalAcPowerEnabled;
}

float DatastoreClass::getTotalDcPowerEnabled()
{
DAT_SEMAPHORE_TAKE();
float retval = _totalDcPowerEnabled;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalDcPowerEnabled;
}

float DatastoreClass::getTotalDcPowerIrradiation()
{
DAT_SEMAPHORE_TAKE();
float retval = _totalDcPowerIrradiation;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalDcPowerIrradiation;
}

float DatastoreClass::getTotalDcIrradiationInstalled()
{
DAT_SEMAPHORE_TAKE();
float retval = _totalDcIrradiationInstalled;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalDcIrradiationInstalled;
}

float DatastoreClass::getTotalDcIrradiation()
{
DAT_SEMAPHORE_TAKE();
float retval = _totalDcIrradiation;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalDcIrradiation;
}

unsigned int DatastoreClass::getTotalAcYieldTotalDigits()
{
DAT_SEMAPHORE_TAKE();
unsigned int retval = _totalAcYieldTotalDigits;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalAcYieldTotalDigits;
}

unsigned int DatastoreClass::getTotalAcYieldDayDigits()
{
DAT_SEMAPHORE_TAKE();
unsigned int retval = _totalAcYieldDayDigits;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalAcYieldDayDigits;
}

unsigned int DatastoreClass::getTotalAcPowerDigits()
{
DAT_SEMAPHORE_TAKE();
unsigned int retval = _totalAcPowerDigits;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalAcPowerDigits;
}

unsigned int DatastoreClass::getTotalDcPowerDigits()
{
DAT_SEMAPHORE_TAKE();
unsigned int retval = _totalDcPowerDigits;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _totalDcPowerDigits;
}

bool DatastoreClass::getIsAtLeastOneReachable()
{
DAT_SEMAPHORE_TAKE();
bool retval = _isAtLeastOneReachable;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _isAtLeastOneReachable;
}

bool DatastoreClass::getIsAtLeastOneProducing()
{
DAT_SEMAPHORE_TAKE();
bool retval = _isAtLeastOneProducing;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _isAtLeastOneProducing;
}

bool DatastoreClass::getIsAllEnabledProducing()
{
DAT_SEMAPHORE_TAKE();
bool retval = _isAllEnabledProducing;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _isAllEnabledProducing;
}

bool DatastoreClass::getIsAllEnabledReachable()
{
DAT_SEMAPHORE_TAKE();
bool retval = _isAllEnabledReachable;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _isAllEnabledReachable;
}

bool DatastoreClass::getIsAtLeastOnePollEnabled()
{
DAT_SEMAPHORE_TAKE();
bool retval = _isAtLeastOnePollEnabled;
DAT_SEMAPHORE_GIVE();
return retval;
std::lock_guard<std::mutex> lock(_mutex);
return _isAtLeastOnePollEnabled;
}

0 comments on commit 9179b67

Please sign in to comment.