1515#include " LogCapture.hpp"
1616#include " OperationModeNode.hpp"
1717#include " RelayModuleNode.hpp"
18+ #include " StateManager.hpp"
1819#include " TimeClientHelper.hpp"
1920
2021#ifdef NORVI_AE01_R
@@ -57,6 +58,16 @@ std::uint16_t *buttonThreshold(Settings &settings, ControllerCommandType type) {
5758 }
5859}
5960
61+ /* * @brief Writes the four timer keys; the timer lives outside the settings record. */
62+ bool persistTimer (const TimerSetting &timer) {
63+ bool ok = true ;
64+ ok = StateManager::saveInt (" timerStartH" , timer.timerStartHour ) && ok;
65+ ok = StateManager::saveInt (" timerStartM" , timer.timerStartMinutes ) && ok;
66+ ok = StateManager::saveInt (" timerEndH" , timer.timerEndHour ) && ok;
67+ ok = StateManager::saveInt (" timerEndM" , timer.timerEndMinutes ) && ok;
68+ return ok;
69+ }
70+
6071bool isValidTime (const ControllerCommand &command) {
6172 return command.hour <= 23 && command.minute <= 59 ;
6273}
@@ -77,6 +88,8 @@ struct ControllerCommandHandler::Plan final {
7788 bool timeLossChanged{false };
7889 bool mappingLoaded{false };
7990 bool mappingChanged{false };
91+ bool timerChanged{false };
92+ TimerSetting timer{};
8093 std::uint8_t solarAddress[8 ]{};
8194 std::uint8_t poolAddress[8 ]{};
8295};
@@ -136,6 +149,14 @@ CommandStatus ControllerCommandHandler::execute(
136149 return CommandStatus::PERSIST_FAILED ;
137150 }
138151 }
152+ const TimerSetting previousTimer = dependencies_.operationMode .getTimerSetting ();
153+ if (plan.timerChanged && !persistTimer (plan.timer )) {
154+ LOG_ERROR (" Controller command: failed to persist timer\n " );
155+ if (!persistTimer (previousTimer)) {
156+ LOG_ERROR (" Controller command: could not restore the previous timer in NVS\n " );
157+ }
158+ return CommandStatus::PERSIST_FAILED ;
159+ }
139160 if (plan.settingsChanged ) {
140161 const Settings previousSettings = liveSettings;
141162 const NtpSettings previousNtp = liveNtp;
@@ -150,6 +171,9 @@ CommandStatus ControllerCommandHandler::execute(
150171 if (!ConfigManager::save ()) {
151172 LOG_ERROR (" Controller command: could not restore previous settings in NVS\n " );
152173 }
174+ if (plan.timerChanged && !persistTimer (previousTimer)) {
175+ LOG_ERROR (" Controller command: could not restore the previous timer in NVS\n " );
176+ }
153177 return CommandStatus::PERSIST_FAILED ;
154178 }
155179 }
@@ -167,6 +191,15 @@ CommandStatus ControllerCommandHandler::execute(
167191 for (std::size_t i = 0 ; i < count; ++i) {
168192 applySensorMapping (commands[i], plan);
169193 }
194+ for (std::size_t i = 0 ; i < count; ++i) {
195+ if (commands[i].type == ControllerCommandType::SET_TIME_LOSS_GREEN_HOURS ||
196+ commands[i].type == ControllerCommandType::SET_TIME_LOSS_RED_HOURS ) {
197+ // The validated pair from the plan, green first so red clamps against it.
198+ setTimeDegradationGreenHours (static_cast <std::uint8_t >(plan.settings .timeLossGreenHours ));
199+ setTimeDegradationRedHours (static_cast <std::uint8_t >(plan.settings .timeLossRedHours ));
200+ break ;
201+ }
202+ }
170203 return CommandStatus::APPLIED ;
171204}
172205
@@ -240,7 +273,21 @@ CommandStatus ControllerCommandHandler::stage(const ControllerCommand &command,
240273
241274 case ControllerCommandType::SET_TIMER_START :
242275 case ControllerCommandType::SET_TIMER_END :
243- return isValidTime (command) ? CommandStatus::APPLIED : CommandStatus::INVALID ;
276+ if (!isValidTime (command)) {
277+ return CommandStatus::INVALID ;
278+ }
279+ if (!plan.timerChanged ) {
280+ plan.timer = dependencies_.operationMode .getTimerSetting ();
281+ plan.timerChanged = true ;
282+ }
283+ if (command.type == ControllerCommandType::SET_TIMER_START ) {
284+ plan.timer .timerStartHour = command.hour ;
285+ plan.timer .timerStartMinutes = command.minute ;
286+ } else {
287+ plan.timer .timerEndHour = command.hour ;
288+ plan.timer .timerEndMinutes = command.minute ;
289+ }
290+ return CommandStatus::APPLIED ;
244291
245292 case ControllerCommandType::SET_SENSOR_MAPPING :
246293 case ControllerCommandType::CLEAR_SENSOR_MAPPING :
@@ -318,10 +365,9 @@ void ControllerCommandHandler::applyRuntime(const ControllerCommand &command, Co
318365 setTimezoneIndex (command.integerValue );
319366 break ;
320367 case ControllerCommandType::SET_TIME_LOSS_GREEN_HOURS :
321- setTimeDegradationGreenHours (static_cast <std::uint8_t >(command.integerValue ));
322- break ;
323368 case ControllerCommandType::SET_TIME_LOSS_RED_HOURS :
324- setTimeDegradationRedHours (static_cast <std::uint8_t >(command.integerValue ));
369+ // Applied as a pair once the whole batch is replayed (see execute()): the
370+ // runtime setters clamp each bound against the other one.
325371 break ;
326372 case ControllerCommandType::SET_BUTTON_1_MIN :
327373 case ControllerCommandType::SET_BUTTON_1_MAX :
0 commit comments