From 689a326c8981ea438709213c04016c76bb8fef51 Mon Sep 17 00:00:00 2001 From: proddy Date: Sun, 1 Sep 2024 11:07:52 +0200 Subject: [PATCH] fixes to restart --- interface/src/app/main/Customizations.tsx | 2 +- .../src/app/settings/ApplicationSettings.tsx | 2 +- interface/src/app/settings/DownloadUpload.tsx | 2 +- .../app/settings/network/NetworkSettings.tsx | 2 +- interface/src/app/status/Status.tsx | 2 +- lib/framework/UploadFileService.cpp | 4 ++-- scripts/upload.py | 21 ++++++++++++++++--- src/emsesp.cpp | 4 ++-- src/system.cpp | 7 ++++--- 9 files changed, 31 insertions(+), 15 deletions(-) diff --git a/interface/src/app/main/Customizations.tsx b/interface/src/app/main/Customizations.tsx index efebe049a..4fcbe15c6 100644 --- a/interface/src/app/main/Customizations.tsx +++ b/interface/src/app/main/Customizations.tsx @@ -137,7 +137,7 @@ const Customizations = () => { const doRestart = async () => { setRestarting(true); - await sendAPI({ device: 'system', cmd: 'restart', id: -1 }).catch( + await sendAPI({ device: 'system', cmd: 'restart', id: 0 }).catch( (error: Error) => { toast.error(error.message); } diff --git a/interface/src/app/settings/ApplicationSettings.tsx b/interface/src/app/settings/ApplicationSettings.tsx index 9ddc4dd50..b6ef0cc23 100644 --- a/interface/src/app/settings/ApplicationSettings.tsx +++ b/interface/src/app/settings/ApplicationSettings.tsx @@ -108,7 +108,7 @@ const ApplicationSettings = () => { const doRestart = async () => { setRestarting(true); - await sendAPI({ device: 'system', cmd: 'restart', id: -1 }).catch( + await sendAPI({ device: 'system', cmd: 'restart', id: 0 }).catch( (error: Error) => { toast.error(error.message); } diff --git a/interface/src/app/settings/DownloadUpload.tsx b/interface/src/app/settings/DownloadUpload.tsx index f86f345a6..2d781e64c 100644 --- a/interface/src/app/settings/DownloadUpload.tsx +++ b/interface/src/app/settings/DownloadUpload.tsx @@ -103,7 +103,7 @@ const DownloadUpload = () => { const doRestart = async () => { setRestarting(true); - await sendAPI({ device: 'system', cmd: 'restart', id: -1 }).catch( + await sendAPI({ device: 'system', cmd: 'restart', id: 0 }).catch( (error: Error) => { toast.error(error.message); } diff --git a/interface/src/app/settings/network/NetworkSettings.tsx b/interface/src/app/settings/network/NetworkSettings.tsx index 312bc4b37..c521b116e 100644 --- a/interface/src/app/settings/network/NetworkSettings.tsx +++ b/interface/src/app/settings/network/NetworkSettings.tsx @@ -134,7 +134,7 @@ const NetworkSettings = () => { const doRestart = async () => { setRestarting(true); - await sendAPI({ device: 'system', cmd: 'restart', id: -1 }).catch( + await sendAPI({ device: 'system', cmd: 'restart', id: 0 }).catch( (error: Error) => { toast.error(error.message); } diff --git a/interface/src/app/status/Status.tsx b/interface/src/app/status/Status.tsx index e14ee3948..6f274dd96 100644 --- a/interface/src/app/status/Status.tsx +++ b/interface/src/app/status/Status.tsx @@ -204,7 +204,7 @@ const SystemStatus = () => { const doRestart = async () => { setConfirmRestart(false); setRestarting(true); - await sendAPI({ device: 'system', cmd: 'restart', id: -1 }).catch( + await sendAPI({ device: 'system', cmd: 'restart', id: 0 }).catch( (error: Error) => { toast.error(error.message); } diff --git a/lib/framework/UploadFileService.cpp b/lib/framework/UploadFileService.cpp index e554fbc91..a1ce63bb7 100644 --- a/lib/framework/UploadFileService.cpp +++ b/lib/framework/UploadFileService.cpp @@ -124,7 +124,7 @@ void UploadFileService::uploadComplete(AsyncWebServerRequest * request) { request->_tempFile.close(); // close the file handle as the upload is now done AsyncWebServerResponse * response = request->beginResponse(200); request->send(response); - emsesp::EMSESP::system_.restart_pending(true); // will be handled by the main loop + emsesp::EMSESP::system_.restart_pending(true); // will be handled by the main loop. We use pending for the Web's RestartMonitor return; } @@ -133,7 +133,7 @@ void UploadFileService::uploadComplete(AsyncWebServerRequest * request) { if (_is_firmware && !request->_tempObject) { AsyncWebServerResponse * response = request->beginResponse(200); request->send(response); - emsesp::EMSESP::system_.restart_pending(true); // will be handled by the main loop + emsesp::EMSESP::system_.restart_pending(true); // will be handled by the main loop. We use pending for the Web's RestartMonitor return; } diff --git a/scripts/upload.py b/scripts/upload.py index bdcd9e2a9..3becb20f4 100644 --- a/scripts/upload.py +++ b/scripts/upload.py @@ -68,7 +68,7 @@ def on_upload(source, target, env): "password": password } - response = requests.post(signon_url, json=username_password, headers=signon_headers, auth=None) + response = requests.post(signon_url, json=username_password, headers=signon_headers) if response.status_code != 200: print_fail("Authentication failed (code " + str(response.status_code) + ")") @@ -116,7 +116,7 @@ def on_upload(source, target, env): upload_url = f"{emsesp_url}/rest/uploadFile" - response = requests.post(upload_url, data=monitor, headers=post_headers, auth=None) + response = requests.post(upload_url, data=monitor, headers=post_headers) bar.close() time.sleep(0.1) @@ -126,7 +126,22 @@ def on_upload(source, target, env): if response.status_code != 200: print_fail("Upload failed (code " + response.status.code + ").") else: - print_success("Upload successful.") + print_success("Upload successful. Rebooting device.") + restart_headers = { + 'Host': host_ip, + 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0', + 'Accept': '*/*', + 'Accept-Language': 'de,en-US;q=0.7,en;q=0.3', + 'Accept-Encoding': 'gzip, deflate', + 'Referer': f'{emsesp_url}', + 'Content-Type': 'application/json', + 'Connection': 'keep-alive', + 'Authorization': 'Bearer ' + f'{access_token}' + } + restart_url = f"{emsesp_url}/api/system/restart" + response = requests.get(restart_url, headers=restart_headers) + if response.status_code != 200: + print_fail("Restart failed (code " + str(response.status_code) + ")") print() diff --git a/src/emsesp.cpp b/src/emsesp.cpp index c8c091238..5dea438d9 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -1673,8 +1673,8 @@ void EMSESP::loop() { // force a query on the EMS devices to fetch latest data at a set interval (1 min) scheduled_fetch_values(); - } else { - emsesp::EMSESP::system_.uploadFirmwareURL(); // start an upload from a URL. This is blocking. + } else if (!system_.uploadFirmwareURL()) { // start an upload from a URL. This is blocking. + system_.upload_status(false); // abort the upload } uuid::loop(); diff --git a/src/system.cpp b/src/system.cpp index 1d5778fdb..2a06d74dc 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -1754,15 +1754,16 @@ bool System::command_format(const char * value, const int8_t id) { return true; } -// restart command - perform a hard reset +// restart command - perform a hard reset (system reboot) bool System::command_restart(const char * value, const int8_t id) { - if (id != 0) { + if (id == 0) { // if it has an id then it's a web call and we need to queue the restart + // default id is -1 when calling /api/system/restart directly for example LOG_INFO("Preparing to restart system"); EMSESP::system_.restart_pending(true); return true; } - LOG_INFO("Restarting system"); + LOG_INFO("Restarting system immediately"); EMSESP::system_.restart_requested(true); // will be handled by the main loop return true; }