Refactor energy drivers

This commit is contained in:
Theo Arends 2024-06-30 16:58:37 +02:00
parent 171d641092
commit 33062dc839
4 changed files with 74 additions and 98 deletions

View File

@ -562,9 +562,9 @@ typedef struct {
uint16_t energy_max_power_limit; // 386 MaxPowerLimit
uint16_t energy_max_power_limit_hold; // 388 MaxPowerLimitHold
uint16_t energy_max_power_limit_window; // 38A MaxPowerLimitWindow
uint16_t energy_max_power_safe_limit; // 38C MaxSafePowerLimit
uint16_t energy_max_power_safe_limit_hold; // 38E MaxSafePowerLimitHold
uint16_t energy_max_power_safe_limit_window; // 390 MaxSafePowerLimitWindow
uint16_t ex_energy_max_power_safe_limit; // 38C MaxSafePowerLimit - Free since 14.1.0.3
uint16_t ex_energy_max_power_safe_limit_hold; // 38E MaxSafePowerLimitHold - Free since 14.1.0.3
uint16_t ex_energy_max_power_safe_limit_window; // 390 MaxSafePowerLimitWindow - Free since 14.1.0.3
uint16_t energy_max_energy; // 392 MaxEnergy
uint16_t energy_max_energy_start; // 394 MaxEnergyStart
uint16_t mqtt_retry; // 396

View File

@ -1208,9 +1208,9 @@ void SettingsDefaultSet2(void) {
// Settings->energy_max_power_limit = 0; // MaxPowerLimit
Settings->energy_max_power_limit_hold = MAX_POWER_HOLD;
Settings->energy_max_power_limit_window = MAX_POWER_WINDOW;
// Settings->energy_max_power_safe_limit = 0; // MaxSafePowerLimit
Settings->energy_max_power_safe_limit_hold = SAFE_POWER_HOLD;
Settings->energy_max_power_safe_limit_window = SAFE_POWER_WINDOW;
// Settings->ex_energy_max_power_safe_limit = 0; // MaxSafePowerLimit
// Settings->ex_energy_max_power_safe_limit_hold = SAFE_POWER_HOLD;
// Settings->ex_energy_max_power_safe_limit_window = SAFE_POWER_WINDOW;
// Settings->energy_max_energy = 0; // MaxEnergy
// Settings->energy_max_energy_start = 0; // MaxEnergyStart
// Settings->energy_kWhtotal_ph[0] = 0;

View File

@ -59,7 +59,6 @@ const char kEnergyCommands[] PROGMEM = "|" // No prefix
#ifdef USE_ENERGY_POWER_LIMIT
D_CMND_MAXENERGY "|" D_CMND_MAXENERGYSTART "|"
D_CMND_MAXPOWER "|" D_CMND_MAXPOWERHOLD "|" D_CMND_MAXPOWERWINDOW "|"
D_CMND_SAFEPOWER "|" D_CMND_SAFEPOWERHOLD "|" D_CMND_SAFEPOWERWINDOW "|"
#endif // USE_ENERGY_POWER_LIMIT
#endif // USE_ENERGY_MARGIN_DETECTION
D_CMND_ENERGYTODAY "|" D_CMND_ENERGYYESTERDAY "|" D_CMND_ENERGYTOTAL "|" D_CMND_ENERGYEXPORTACTIVE "|" D_CMND_ENERGYUSAGE "|" D_CMND_ENERGYEXPORT "|" D_CMND_TARIFF;
@ -72,7 +71,6 @@ void (* const EnergyCommand[])(void) PROGMEM = {
#ifdef USE_ENERGY_POWER_LIMIT
&CmndMaxEnergy, &CmndMaxEnergyStart,
&CmndMaxPower, &CmndMaxPowerHold, &CmndMaxPowerWindow,
&CmndSafePower, &CmndSafePowerHold, &CmndSafePowerWindow,
#endif // USE_ENERGY_POWER_LIMIT
#endif // USE_ENERGY_MARGIN_DETECTION
&CmndEnergyToday, &CmndEnergyYesterday, &CmndEnergyTotal, &CmndEnergyExportActive, &CmndEnergyUsage, &CmndEnergyExport, &CmndTariff};
@ -1018,10 +1016,30 @@ void CmndEnergyConfig(void) {
\*********************************************************************************************/
void EnergyMarginStatus(void) {
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS9_MARGIN "\":{\"" D_CMND_POWERDELTA "\":[%d,%d,%d],\"" D_CMND_POWERLOW "\":%d,\"" D_CMND_POWERHIGH "\":%d,\""
D_CMND_VOLTAGELOW "\":%d,\"" D_CMND_VOLTAGEHIGH "\":%d,\"" D_CMND_CURRENTLOW "\":%d,\"" D_CMND_CURRENTHIGH "\":%d}}"),
Settings->energy_power_delta[0], Settings->energy_power_delta[1], Settings->energy_power_delta[2], Settings->energy_min_power, Settings->energy_max_power,
Settings->energy_min_voltage, Settings->energy_max_voltage, Settings->energy_min_current, Settings->energy_max_current);
Response_P(PSTR("{\"" D_CMND_STATUS D_STATUS9_MARGIN "\":{\"" D_CMND_POWERDELTA "\":"));
if (Energy->phase_count > 1) {
for (uint32_t i = 0; i < Energy->phase_count; i++) {
ResponseAppend_P(PSTR("%c%d"), (i)?',':'[', Settings->energy_power_delta[i]);
}
ResponseAppend_P(PSTR("]"));
} else {
ResponseAppend_P(PSTR("%d"), Settings->energy_power_delta[0]);
}
ResponseAppend_P(PSTR(",\"" D_CMND_POWERLOW "\":%d,\"" D_CMND_POWERHIGH "\":%d,\""
D_CMND_VOLTAGELOW "\":%d,\"" D_CMND_VOLTAGEHIGH "\":%d,\""
D_CMND_CURRENTLOW "\":%d,\"" D_CMND_CURRENTHIGH "\":%d"),
Settings->energy_min_power, Settings->energy_max_power,
Settings->energy_min_voltage, Settings->energy_max_voltage,
Settings->energy_min_current, Settings->energy_max_current);
#ifdef USE_ENERGY_POWER_LIMIT
ResponseAppend_P(PSTR(",\"" D_CMND_MAXPOWER "\":%d,\""
D_CMND_MAXPOWERHOLD "\":%d,\"" D_CMND_MAXPOWERWINDOW "\":%d,\""
D_CMND_MAXENERGY "\":%d,\"" D_CMND_MAXENERGYSTART "\":%d"),
Settings->energy_max_power_limit,
Settings->energy_max_power_limit_hold, Settings->energy_max_power_limit_window,
Settings->energy_max_energy, Settings->energy_max_energy_start);
#endif // USE_ENERGY_POWER_LIMIT
ResponseJsonEndEnd();
}
void CmndPowerDelta(void) {
@ -1033,104 +1051,62 @@ void CmndPowerDelta(void) {
}
}
void CmndPowerLow(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 6000)) {
Settings->energy_min_power = XdrvMailbox.payload;
bool ResponseCmndEnergyMargin(uint16_t* value, uint32_t max_value, uint32_t default_value = 1);
bool ResponseCmndEnergyMargin(uint16_t* value, uint32_t max_value, uint32_t default_value) {
bool value_changed = false;
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= max_value)) {
*value = (1 == XdrvMailbox.payload) ? default_value : XdrvMailbox.payload;
value_changed = true;
}
ResponseCmndNumber(Settings->energy_min_power);
ResponseCmndNumber(*value);
return value_changed;
}
void CmndPowerLow(void) {
ResponseCmndEnergyMargin(&Settings->energy_min_power, 6000);
}
void CmndPowerHigh(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 6000)) {
Settings->energy_max_power = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_max_power);
ResponseCmndEnergyMargin(&Settings->energy_max_power, 6000);
}
void CmndVoltageLow(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 500)) {
Settings->energy_min_voltage = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_min_voltage);
ResponseCmndEnergyMargin(&Settings->energy_min_voltage, 500);
}
void CmndVoltageHigh(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 500)) {
Settings->energy_max_voltage = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_max_voltage);
ResponseCmndEnergyMargin(&Settings->energy_max_voltage, 500);
}
void CmndCurrentLow(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 25000)) {
Settings->energy_min_current = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_min_current);
ResponseCmndEnergyMargin(&Settings->energy_min_current, 25000);
}
void CmndCurrentHigh(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 25000)) {
Settings->energy_max_current = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_max_current);
ResponseCmndEnergyMargin(&Settings->energy_max_current, 25000);
}
#ifdef USE_ENERGY_POWER_LIMIT
void CmndMaxPower(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 6000)) {
Settings->energy_max_power_limit = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_max_power_limit);
ResponseCmndEnergyMargin(&Settings->energy_max_power_limit, 6000);
}
void CmndMaxPowerHold(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 6000)) {
Settings->energy_max_power_limit_hold = (1 == XdrvMailbox.payload) ? MAX_POWER_HOLD : XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_max_power_limit_hold);
ResponseCmndEnergyMargin(&Settings->energy_max_power_limit_hold, 6000, MAX_POWER_HOLD);
}
void CmndMaxPowerWindow(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 6000)) {
Settings->energy_max_power_limit_window = (1 == XdrvMailbox.payload) ? MAX_POWER_WINDOW : XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_max_power_limit_window);
}
void CmndSafePower(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 6000)) {
Settings->energy_max_power_safe_limit = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_max_power_safe_limit);
}
void CmndSafePowerHold(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 6000)) {
Settings->energy_max_power_safe_limit_hold = (1 == XdrvMailbox.payload) ? SAFE_POWER_HOLD : XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_max_power_safe_limit_hold);
}
void CmndSafePowerWindow(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 1440)) {
Settings->energy_max_power_safe_limit_window = (1 == XdrvMailbox.payload) ? SAFE_POWER_WINDOW : XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_max_power_safe_limit_window);
ResponseCmndEnergyMargin(&Settings->energy_max_power_limit_window, 6000, MAX_POWER_WINDOW);
}
void CmndMaxEnergy(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 6000)) {
Settings->energy_max_energy = XdrvMailbox.payload;
if (ResponseCmndEnergyMargin(&Settings->energy_max_energy, 6000)) {
Energy->max_energy_state = 3;
}
ResponseCmndNumber(Settings->energy_max_energy);
}
void CmndMaxEnergyStart(void) {
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload < 24)) {
Settings->energy_max_energy_start = XdrvMailbox.payload;
}
ResponseCmndNumber(Settings->energy_max_energy_start);
ResponseCmndEnergyMargin(&Settings->energy_max_energy_start, 23);
}
#endif // USE_ENERGY_POWER_LIMIT
#endif // USE_ENERGY_MARGIN_DETECTION

View File

@ -1329,18 +1329,18 @@ void CmndEnergyConfig(void) {
* USE_ENERGY_MARGIN_DETECTION and USE_ENERGY_POWER_LIMIT
\*********************************************************************************************/
void ResponseAppendMargin(uint16_t* start, uint32_t step_size = 0);
void ResponseAppendMargin(uint16_t* start, uint32_t step_size) {
void ResponseAppendMargin(uint16_t* value, uint32_t step_size = 0);
void ResponseAppendMargin(uint16_t* value, uint32_t step_size) {
if (Energy->phase_count > 1) {
if (0 == step_size) {
step_size = sizeof(tPhase) / sizeof(uint16_t);
}
for (uint32_t i = 0; i < Energy->phase_count; i++) {
ResponseAppend_P(PSTR("%c%d"), (i)?',':'[', start[i * step_size]);
ResponseAppend_P(PSTR("%c%d"), (i)?',':'[', value[i * step_size]);
}
ResponseAppend_P(PSTR("]"));
} else {
ResponseAppend_P(PSTR("%d"), start[0]);
ResponseAppend_P(PSTR("%d"), value[0]);
}
}
@ -1372,8 +1372,8 @@ void EnergyMarginStatus(void) {
ResponseJsonEndEnd();
}
bool ResponseCmndEnergyMargin(uint16_t* start, uint32_t max_value, uint32_t default_value = 1, uint32_t step_size = 0);
bool ResponseCmndEnergyMargin(uint16_t* start, uint32_t max_value, uint32_t default_value, uint32_t step_size) {
bool ResponseCmndIdxEnergyMargin(uint16_t* value, uint32_t max_value, uint32_t default_value = 1, uint32_t step_size = 0);
bool ResponseCmndIdxEnergyMargin(uint16_t* value, uint32_t max_value, uint32_t default_value, uint32_t step_size) {
bool value_changed = false;
if ((XdrvMailbox.index >= 0) && (XdrvMailbox.index <= ENERGY_MAX_PHASES)) {
if (0 == step_size) {
@ -1383,75 +1383,75 @@ bool ResponseCmndEnergyMargin(uint16_t* start, uint32_t max_value, uint32_t defa
if (0 == XdrvMailbox.index) {
// PowerLow0 10
for (uint32_t i = 0; i < Energy->phase_count; i++) {
start[i * step_size] = (1 == XdrvMailbox.payload) ? default_value : XdrvMailbox.payload;
value[i * step_size] = (1 == XdrvMailbox.payload) ? default_value : XdrvMailbox.payload;
}
} else {
// PowerLow 10 = PowerLow1 10 .. PowerLow8 10
start[(XdrvMailbox.index -1) * step_size] = (1 == XdrvMailbox.payload) ? default_value : XdrvMailbox.payload;
value[(XdrvMailbox.index -1) * step_size] = (1 == XdrvMailbox.payload) ? default_value : XdrvMailbox.payload;
}
value_changed = true;
}
if (0 == XdrvMailbox.index) {
// PowerLow0, PowerLow0 10
ResponseCmnd();
ResponseAppendMargin(start, step_size);
ResponseAppendMargin(value, step_size);
ResponseJsonEnd();
} else {
// PowerLow = PowerLow1 .. PowerLow8
ResponseCmndIdxNumber(start[(XdrvMailbox.index -1) * step_size]);
ResponseCmndIdxNumber(value[(XdrvMailbox.index -1) * step_size]);
}
}
return value_changed;
}
void CmndPowerDelta(void) {
ResponseCmndEnergyMargin(&Energy->Settings.power_delta[0], 32000, 1, 1);
ResponseCmndIdxEnergyMargin(&Energy->Settings.power_delta[0], 32000, 1, 1);
}
void CmndPowerLow(void) {
ResponseCmndEnergyMargin(&Energy->Settings.phase[0].min_power, 6000);
ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].min_power, 6000);
}
void CmndPowerHigh(void) {
ResponseCmndEnergyMargin(&Energy->Settings.phase[0].max_power, 6000);
ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].max_power, 6000);
}
void CmndVoltageLow(void) {
ResponseCmndEnergyMargin(&Energy->Settings.phase[0].min_voltage, 500);
ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].min_voltage, 500);
}
void CmndVoltageHigh(void) {
ResponseCmndEnergyMargin(&Energy->Settings.phase[0].max_voltage, 500);
ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].max_voltage, 500);
}
void CmndCurrentLow(void) {
ResponseCmndEnergyMargin(&Energy->Settings.phase[0].min_current, 25000);
ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].min_current, 25000);
}
void CmndCurrentHigh(void) {
ResponseCmndEnergyMargin(&Energy->Settings.phase[0].max_current, 25000);
ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].max_current, 25000);
}
void CmndMaxPower(void) {
ResponseCmndEnergyMargin(&Energy->Settings.phase[0].max_power_limit, 6000);
ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].max_power_limit, 6000);
}
void CmndMaxPowerHold(void) {
ResponseCmndEnergyMargin(&Energy->Settings.phase[0].max_power_limit_hold, 6000, MAX_POWER_HOLD);
ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].max_power_limit_hold, 6000, MAX_POWER_HOLD);
}
void CmndMaxPowerWindow(void) {
ResponseCmndEnergyMargin(&Energy->Settings.phase[0].max_power_limit_window, 6000, MAX_POWER_WINDOW);
ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].max_power_limit_window, 6000, MAX_POWER_WINDOW);
}
void CmndMaxEnergy(void) {
if (ResponseCmndEnergyMargin(&Energy->Settings.phase[0].max_energy, 6000)) {
if (ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].max_energy, 6000)) {
Energy->max_energy_state[XdrvMailbox.index -1] = 3;
}
}
void CmndMaxEnergyStart(void) {
ResponseCmndEnergyMargin(&Energy->Settings.phase[0].max_energy_start, 23);
ResponseCmndIdxEnergyMargin(&Energy->Settings.phase[0].max_energy_start, 23);
}
/********************************************************************************************/