Skip to content

Commit 8638ea1

Browse files
committed
+fix: clean code
1 parent 21ddb57 commit 8638ea1

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

smartswitch.espxx/include/Logic.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "Util.h"
2828
#include "debug.h"
2929

30+
#define SECONDS_PER_HOUR 3600
31+
3032
static char tsfmt[30];
3133
char *toDate(uint32_t utc_ts, int16_t offset)
3234
{
@@ -68,7 +70,7 @@ typedef struct
6870
BatteryLevel level;
6971
} BatteryState;
7072

71-
static int seekToPvForecastData(SystemState *systemState)
73+
static int findPvForecastData(SystemState *systemState)
7274
{
7375
short i = 0;
7476

@@ -78,7 +80,7 @@ static int seekToPvForecastData(SystemState *systemState)
7880
}
7981

8082
bool foundPvData = false;
81-
uint32_t ts = systemState->ts - (systemState->ts % 3600); // start with timestamp of last full hour
83+
uint32_t ts = systemState->ts - (systemState->ts % SECONDS_PER_HOUR); // start with timestamp of last full hour
8284

8385
for (; i <= SOLAR_FORECAST_HOURS; i++)
8486
{
@@ -92,7 +94,7 @@ static int seekToPvForecastData(SystemState *systemState)
9294

9395
static BatteryState predictBatteryCapacityState(SystemConfig *systemConfig, SystemState *systemState)
9496
{
95-
short index = seekToPvForecastData(systemState);
97+
short index = findPvForecastData(systemState);
9698
if (index == -1)
9799
{
98100
return BatteryState{0, BatteryLevel::Min}; // no solar forecast data, assume battery will become empty
@@ -101,10 +103,10 @@ static BatteryState predictBatteryCapacityState(SystemConfig *systemConfig, Syst
101103
uint16_t hysteresis_Wh = systemConfig->loadPower_W * 20 / 60; // required capacity (Wh) if load is switched on for 20min
102104
uint32_t cap_bat_sim_wh = systemState->cap_bat_Wh;
103105
uint16_t cap_bat_min_Wh = systemConfig->cap_bat_min_Wh + (systemState->switchEnabled ? 0 : hysteresis_Wh);
104-
uint32_t ts = systemState->ts - (systemState->ts % 3600); // full hour
105-
uint16_t seconds = ts + 3600 - systemState->ts; // remaining seconds in this hour
106+
uint32_t ts = systemState->ts - (systemState->ts % SECONDS_PER_HOUR); // full hour
107+
uint16_t seconds = ts + SECONDS_PER_HOUR - systemState->ts; // remaining seconds in this hour
106108

107-
uint32_t wh = seconds * systemState->pv_forecast_ts_wh[index][1] / 3600; // remaining pv production in this hour
109+
uint32_t wh = seconds * systemState->pv_forecast_ts_wh[index][1] / SECONDS_PER_HOUR; // remaining pv production in this hour
108110
uint16_t cons_wh;
109111

110112
DEBUGF("%d => %u %u (s) %s %u/%u (Wh)\n", index, ts, systemState->ts, toDate(ts), wh, systemState->pv_forecast_ts_wh[index][1]);
@@ -115,8 +117,8 @@ static BatteryState predictBatteryCapacityState(SystemConfig *systemConfig, Syst
115117
// adaptive weight
116118
float weight = 1.0f - ((float)(hour) / (float)SOLAR_FORECAST_HOURS);
117119

118-
wh = wh * SOLAR_FORECAST_SAFETY_FACTOR * weight; // apply safety factor + adaptive weight to production forecast
119-
cons_wh = seconds * systemState->cons_W_norm / 3600; // remaining consumption in this hour
120+
wh = wh * SOLAR_FORECAST_SAFETY_FACTOR * weight; // apply safety factor + adaptive weight to production forecast
121+
cons_wh = seconds * systemState->cons_W_norm / SECONDS_PER_HOUR; // remaining consumption in this hour
120122

121123
uint32_t cap_bat_sim_previos_wh = cap_bat_sim_wh; // safe previous to check battery capacity trend
122124

@@ -136,7 +138,7 @@ static BatteryState predictBatteryCapacityState(SystemConfig *systemConfig, Syst
136138

137139
ts = systemState->pv_forecast_ts_wh[index][0];
138140
wh = systemState->pv_forecast_ts_wh[index][1];
139-
seconds = 3600; // full hour
141+
seconds = SECONDS_PER_HOUR; // next calculation we have a full hour
140142
}
141143
DEBUGF("capacity %u Wh (bat) %u Wh (min) %u Wh (hys) %u Wh at %s\n", cap_bat_sim_wh, cap_bat_min_Wh, hysteresis_Wh, cons_wh, toDate(ts));
142144
return BatteryState{

0 commit comments

Comments
 (0)