Refactor WiFi Manager

This commit is contained in:
2025-07-09 23:25:08 -06:00
parent c3bbba4a24
commit 504003e2ba
5 changed files with 747 additions and 100 deletions

View File

@ -123,16 +123,31 @@ esp_err_t state_manager_save(void) {
motor_get_last_on_state(&last_on_mode, &last_on_speed);
bool user_turned_off = motor_get_user_turned_off();
// Determine the actual state to save
motor_mode_t mode_to_save = state->mode;
int speed_to_save = state->target_speed;
// If we're in cooldown, save the pending state instead of the current OFF state
if (state->state == MOTOR_STATE_COOLDOWN && state->pending_mode != MOTOR_OFF) {
mode_to_save = state->pending_mode;
speed_to_save = state->pending_speed;
ESP_LOGI(SYSTEM_TAG, "Motor in cooldown - saving pending state instead: %s @ %d%%",
motor_mode_to_string(mode_to_save), speed_to_save);
}
ESP_LOGI(SYSTEM_TAG, "=== SAVING STATE TO NVS ===");
ESP_LOGI(SYSTEM_TAG, "Mode: %s, Speed: %d%%, Last ON: %s@%d%%, User OFF: %s",
ESP_LOGI(SYSTEM_TAG, "Current: %s @ %d%%, State: %s",
motor_mode_to_string(state->mode), state->target_speed,
motor_state_to_string(state->state));
ESP_LOGI(SYSTEM_TAG, "Saving: %s @ %d%%, Last ON: %s@%d%%, User OFF: %s",
motor_mode_to_string(mode_to_save), speed_to_save,
motor_mode_to_string(last_on_mode), last_on_speed,
user_turned_off ? "YES" : "NO");
// Save current motor state
err = nvs_set_u8(nvs_handle, NVS_KEY_MODE, (uint8_t)state->mode);
// Save the determined motor state (actual or pending)
err = nvs_set_u8(nvs_handle, NVS_KEY_MODE, (uint8_t)mode_to_save);
if (err == ESP_OK) {
err = nvs_set_u8(nvs_handle, NVS_KEY_SPEED, (uint8_t)state->target_speed);
err = nvs_set_u8(nvs_handle, NVS_KEY_SPEED, (uint8_t)speed_to_save);
}
// Save last ON state