Skip to content

Commit

Permalink
Reset after configuration is restored to apply it immediately.
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueAndi committed Jan 10, 2024
1 parent eae0c5e commit 9852621
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
36 changes: 26 additions & 10 deletions data/js/dialog.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
var dialog = window.dialog || {};

dialog._show = function(title, message) {
dialog._show = function(title, message, isBlocking) {
return new Promise(function(resolve, reject) {

var waitOnClick = false;

if (("boolean" === typeof isBlocking) &&
(true == isBlocking)) {
waitOnClick = true;
}

$("#dialogTitle").text(title);
$("#dialogBody").html(message);

$("#modalDialog").on("shown.bs.modal", function() {
$("#modalDialog").off("shown.bs.modal");
resolve();

if (false === waitOnClick) {
resolve();
}
});

$("#modalDialog").modal("show");

if (true === waitOnClick) {
$("#modalDialog .btn-secondary").click(function() {
resolve();
});
}
});
}

Expand All @@ -27,7 +43,7 @@ dialog.hide = function() {
});
}

dialog.showInfo = function(message) {
dialog.showInfo = function(message, isBlocking) {
var $btnClose = $("<button>")
.attr("type", "button")
.attr("class", "btn btn-secondary")
Expand All @@ -40,10 +56,10 @@ dialog.showInfo = function(message) {
$("#dialogHeader").addClass("modal-header bg-primary text-white");
$("#dialogFooter").append($btnClose);

return dialog._show("Info", message);
return dialog._show("Info", message, isBlocking);
}

dialog.showWarning = function(message) {
dialog.showWarning = function(message, isBlocking) {
var $btnClose = $("<button>")
.attr("type", "button")
.attr("class", "btn btn-secondary")
Expand All @@ -56,10 +72,10 @@ dialog.showWarning = function(message) {
$("#dialogHeader").addClass("modal-header bg-warning text-dark");
$("#dialogFooter").append($btnClose);

return dialog._show("Warning", message);
return dialog._show("Warning", message, isBlocking);
}

dialog.showError = function(message) {
dialog.showError = function(message, isBlocking) {
var $btnClose = $("<button>")
.attr("type", "button")
.attr("class", "btn btn-secondary")
Expand All @@ -72,14 +88,14 @@ dialog.showError = function(message) {
$("#dialogHeader").addClass("modal-header bg-danger text-white");
$("#dialogFooter").append($btnClose);

return dialog._show("Error", message);
return dialog._show("Error", message, isBlocking);
}

dialog.show = function(title, message) {
dialog.show = function(title, message, isBlocking) {
$("#dialogHeader").removeClass();
$("#dialogFooter").empty();

$("#dialogHeader").addClass("modal-header bg-dark text-white");

return dialog._show(title, message);
return dialog._show(title, message, isBlocking);
}
8 changes: 8 additions & 0 deletions data/js/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,11 @@ pixelix.rest.Client.prototype.setSetting = function(key, value) {

return promise;
};

pixelix.rest.Client.prototype.reset = function() {
return utils.makeRequest({
method: "GET",
url: "/rest/api/v1/reset",
isJsonResponse: true
});
};
6 changes: 5 additions & 1 deletion data/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,11 @@ <h1 class="mt-5">Update</h1>

return promiseChain;
}).then(function() {
dialog.showInfo("<p>Restore successful.</p>");
return dialog.showInfo("<p>Restore successful.</p>", true);
}).then(function() {
return restClient.reset();
}).then(function() {
return dialog.showInfo("<p>Reset performed to apply restored configuration.</p>");
}).catch(function() {
dialog.showError("<p>Restore failed.</p>");
});
Expand Down
40 changes: 40 additions & 0 deletions src/Web/RestApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "RestUtil.h"
#include "SlotList.h"
#include "ButtonActions.h"
#include "UpdateMgr.h"

#include <Util.h>
#include <WiFi.h>
Expand Down Expand Up @@ -119,6 +120,7 @@ static void handleSlot(AsyncWebServerRequest* request);
static void handlePluginInstall(AsyncWebServerRequest* request);
static void handlePluginUninstall(AsyncWebServerRequest* request);
static void handlePlugins(AsyncWebServerRequest* request);
static void handleReset(AsyncWebServerRequest* request);
static void handleSensors(AsyncWebServerRequest* request);
static void handleSettings(AsyncWebServerRequest* request);
static void handleSetting(AsyncWebServerRequest* request);
Expand Down Expand Up @@ -179,6 +181,7 @@ void RestApi::init(AsyncWebServer& srv)
(void)srv.on("/rest/api/v1/plugin/install", handlePluginInstall);
(void)srv.on("/rest/api/v1/plugin/uninstall", handlePluginUninstall);
(void)srv.on("/rest/api/v1/plugins", handlePlugins);
(void)srv.on("/rest/api/v1/reset", handleReset);
(void)srv.on("/rest/api/v1/sensors", handleSensors);
(void)srv.on("/rest/api/v1/settings", handleSettings);
(void)srv.on("/rest/api/v1/setting", handleSetting);
Expand Down Expand Up @@ -696,6 +699,43 @@ static void handlePlugins(AsyncWebServerRequest* request)
RestUtil::sendJsonRsp(request, jsonDoc, httpStatusCode);
}

/**
* Perform a reset request.
* GET \c "/api/v1/reset"
*
* @param[in] request HTTP request
*/
static void handleReset(AsyncWebServerRequest* request)
{
const size_t JSON_DOC_SIZE = 512U;
DynamicJsonDocument jsonDoc(JSON_DOC_SIZE);
uint32_t httpStatusCode = HttpStatus::STATUS_CODE_OK;

if (nullptr == request)
{
return;
}

if (HTTP_GET != request->method())
{
RestUtil::prepareRspErrorHttpMethodNotSupported(jsonDoc);
httpStatusCode = HttpStatus::STATUS_CODE_NOT_FOUND;
}
else
{
/* To ensure the positive response will be sent. */
const uint32_t RESTART_DELAY = 100U; /* ms */

(void)RestUtil::prepareRspSuccess(jsonDoc);

UpdateMgr::getInstance().reqRestart(RESTART_DELAY);

httpStatusCode = HttpStatus::STATUS_CODE_OK;
}

RestUtil::sendJsonRsp(request, jsonDoc, httpStatusCode);
}

/**
* List all sensors.
* GET \c "/api/v1/sensors"
Expand Down

0 comments on commit 9852621

Please sign in to comment.