forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'tbnobody/OpenDTU/master'
- Loading branch information
Showing
48 changed files
with
418 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#pragma once | ||
|
||
#include <ESPAsyncWebServer.h> | ||
|
||
class WebApiMaintenanceClass { | ||
public: | ||
void init(AsyncWebServer* server); | ||
void loop(); | ||
|
||
private: | ||
void onRebootPost(AsyncWebServerRequest* request); | ||
|
||
AsyncWebServer* _server; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
/* | ||
* Copyright (C) 2022 Thomas Basler and others | ||
*/ | ||
|
||
#include "WebApi_maintenance.h" | ||
#include "AsyncJson.h" | ||
#include "WebApi.h" | ||
|
||
void WebApiMaintenanceClass::init(AsyncWebServer* server) | ||
{ | ||
using std::placeholders::_1; | ||
|
||
_server = server; | ||
|
||
_server->on("/api/maintenance/reboot", HTTP_POST, std::bind(&WebApiMaintenanceClass::onRebootPost, this, _1)); | ||
} | ||
|
||
void WebApiMaintenanceClass::loop() | ||
{ | ||
} | ||
|
||
void WebApiMaintenanceClass::onRebootPost(AsyncWebServerRequest* request) | ||
{ | ||
if (!WebApi.checkCredentials(request)) { | ||
return; | ||
} | ||
|
||
AsyncJsonResponse* response = new AsyncJsonResponse(false, MQTT_JSON_DOC_SIZE); | ||
JsonObject retMsg = response->getRoot(); | ||
retMsg[F("type")] = F("warning"); | ||
|
||
if (!request->hasParam("data", true)) { | ||
retMsg[F("message")] = F("No values found!"); | ||
response->setLength(); | ||
request->send(response); | ||
return; | ||
} | ||
|
||
String json = request->getParam("data", true)->value(); | ||
|
||
if (json.length() > MQTT_JSON_DOC_SIZE) { | ||
retMsg[F("message")] = F("Data too large!"); | ||
response->setLength(); | ||
request->send(response); | ||
return; | ||
} | ||
|
||
DynamicJsonDocument root(MQTT_JSON_DOC_SIZE); | ||
DeserializationError error = deserializeJson(root, json); | ||
|
||
if (error) { | ||
retMsg[F("message")] = F("Failed to parse data!"); | ||
response->setLength(); | ||
request->send(response); | ||
return; | ||
} | ||
|
||
if (!(root.containsKey("reboot"))) { | ||
retMsg[F("message")] = F("Values are missing!"); | ||
response->setLength(); | ||
request->send(response); | ||
return; | ||
} | ||
|
||
if (root[F("reboot")].as<bool>()) { | ||
retMsg[F("type")] = F("success"); | ||
retMsg[F("message")] = F("Reboot triggered!"); | ||
|
||
response->setLength(); | ||
request->send(response); | ||
yield(); | ||
delay(1000); | ||
yield(); | ||
ESP.restart(); | ||
} else { | ||
retMsg[F("message")] = F("Reboot cancled!"); | ||
|
||
response->setLength(); | ||
request->send(response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.