From b687d9f17d4c8f43d399ec271925563341ad5b51 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Mon, 2 May 2022 10:00:00 +0200 Subject: [PATCH] Add API mode to partition switch `u4` --- tasmota/xdrv_01_webserver.ino | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino index bd4490bf5..7609cff82 100644 --- a/tasmota/xdrv_01_webserver.ino +++ b/tasmota/xdrv_01_webserver.ino @@ -2896,6 +2896,15 @@ void HandlePreflightRequest(void) // `false`: the current partition is not the target, but a restart to factory is triggered (polling required) // `true`: the current partition is the one required // `none`: there is no factory partition + +// return a simple status page as text/plain code 200 +static void WSReturnSimpleString(const char *msg) { + if (nullptr == msg) { msg = ""; } + Webserver->client().flush(); + WSHeaderSend(); + Webserver->send(200, "text/plain", msg); +} + void HandleSwitchFactory(void) { if (!HttpCheckPriviledgedAccess()) { return; } @@ -2905,9 +2914,12 @@ void HandleSwitchFactory(void) bool switch_factory = false; // trigger a restart to factory partition? bool switch_ota = false; // switch back to OTA partition + bool single_ota = false; + bool api_mode = Webserver->hasArg("api"); // api-mode, returns `true`, `false` or `none` // switch to factory ? if (EspSingleOtaPartition()) { + single_ota = true; if (strcmp("fct", tmp1) == 0 && !EspRunningFactoryPartition()) { switch_factory = true; } @@ -2917,6 +2929,7 @@ void HandleSwitchFactory(void) } } + // apply the change in flash and return result if (switch_factory || switch_ota) { SettingsSaveAll(); if (switch_factory) { @@ -2925,11 +2938,22 @@ void HandleSwitchFactory(void) const esp_partition_t* partition = esp_ota_get_next_update_partition(nullptr); esp_ota_set_boot_partition(partition); } - // display restart page - WebRestart(0); + + if (api_mode) { + WSReturnSimpleString("false"); + AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_HTTP D_RESTART)); + EspRestart(); + } else { + WebRestart(0); + } } else { - Webserver->sendHeader("Location", "/", true); - Webserver->send(302, "text/plain", ""); + if (api_mode) { + // return `none` or `true` + WSReturnSimpleString(EspSingleOtaPartition() ? "true" : "none"); + } else { + Webserver->sendHeader("Location", "/", true); + Webserver->send(302, "text/plain", ""); + } } } #endif