forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebApi_maintenance.cpp
53 lines (43 loc) · 1.54 KB
/
WebApi_maintenance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022-2024 Thomas Basler and others
*/
#include "WebApi_maintenance.h"
#include "RestartHelper.h"
#include "WebApi.h"
#include "WebApi_errors.h"
#include <AsyncJson.h>
void WebApiMaintenanceClass::init(AsyncWebServer& server, Scheduler& scheduler)
{
using std::placeholders::_1;
server.on("/api/maintenance/reboot", HTTP_POST, std::bind(&WebApiMaintenanceClass::onRebootPost, this, _1));
}
void WebApiMaintenanceClass::onRebootPost(AsyncWebServerRequest* request)
{
if (!WebApi.checkCredentials(request)) {
return;
}
AsyncJsonResponse* response = new AsyncJsonResponse();
JsonDocument root;
if (!WebApi.parseRequestData(request, response, root)) {
return;
}
auto& retMsg = response->getRoot();
if (!(root["reboot"].is<bool>())) {
retMsg["message"] = "Values are missing!";
retMsg["code"] = WebApiError::GenericValueMissing;
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
return;
}
if (root["reboot"].as<bool>()) {
retMsg["type"] = "success";
retMsg["message"] = "Reboot triggered!";
retMsg["code"] = WebApiError::MaintenanceRebootTriggered;
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
RestartHelper.triggerRestart();
} else {
retMsg["message"] = "Reboot cancled!";
retMsg["code"] = WebApiError::MaintenanceRebootCancled;
WebApi.sendJsonResponse(request, response, __FUNCTION__, __LINE__);
}
}