|
|
@@ -4,7 +4,6 @@
|
|
|
#include <Settings.h>
|
|
|
#include <MiLightHttpServer.h>
|
|
|
#include <MiLightRadioConfig.h>
|
|
|
-#include <GithubClient.h>
|
|
|
#include <string.h>
|
|
|
#include <TokenIterator.h>
|
|
|
#include <index.html.gz.h>
|
|
|
@@ -23,10 +22,8 @@ void MiLightHttpServer::begin() {
|
|
|
|
|
|
server.onPattern("/gateways/:device_id/:type/:group_id", HTTP_ANY, [this](const UrlTokenBindings* b) { handleUpdateGroup(b); });
|
|
|
server.onPattern("/raw_commands/:type", HTTP_ANY, [this](const UrlTokenBindings* b) { handleSendRaw(b); });
|
|
|
- server.onPattern("/download_update/:component", HTTP_GET, [this](const UrlTokenBindings* b) { handleDownloadUpdate(b); });
|
|
|
server.on("/web", HTTP_POST, [this]() { server.send_P(200, TEXT_PLAIN, PSTR("success")); }, handleUpdateFile(WEB_INDEX_FILENAME));
|
|
|
server.on("/about", HTTP_GET, [this]() { handleAbout(); });
|
|
|
- server.on("/latest_release", HTTP_GET, [this]() { handleGetLatestRelease(); });
|
|
|
server.on("/system", HTTP_POST, [this]() { handleSystemPost(); });
|
|
|
server.on("/firmware", HTTP_POST,
|
|
|
[this](){
|
|
|
@@ -74,36 +71,6 @@ void MiLightHttpServer::begin() {
|
|
|
server.begin();
|
|
|
}
|
|
|
|
|
|
-void MiLightHttpServer::handleGetLatestRelease() {
|
|
|
- GithubClient client = GithubClient::apiClient();
|
|
|
- String path = GithubClient::buildApiRequest(
|
|
|
- MILIGHT_GITHUB_USER,
|
|
|
- MILIGHT_GITHUB_REPO,
|
|
|
- "/releases/latest"
|
|
|
- );
|
|
|
-
|
|
|
- // This is an ugly hack, but probably not worth optimizing. The nice way
|
|
|
- // to do this would be to extract the content len from GitHub's response
|
|
|
- // and stream the body to the server directly. But this would require parsing
|
|
|
- // headers in the response from GitHub, which seems like more trouble than
|
|
|
- // it's worth.
|
|
|
- const String& fsPath = "/_cv.json";
|
|
|
- size_t tries = 0;
|
|
|
-
|
|
|
- while (tries++ < MAX_DOWNLOAD_ATTEMPTS && !client.download(path, fsPath)) {
|
|
|
- Serial.println(F("Failed download attempt."));
|
|
|
- }
|
|
|
-
|
|
|
- if (!SPIFFS.exists(fsPath)) {
|
|
|
- server.send_P(500, TEXT_PLAIN, PSTR("Failed to stream API request from GitHub. Check Serial logs for more information."));
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- File file = SPIFFS.open(fsPath, "r");
|
|
|
- server.streamFile(file, APPLICATION_JSON);
|
|
|
- SPIFFS.remove(fsPath);
|
|
|
-}
|
|
|
-
|
|
|
void MiLightHttpServer::handleClient() {
|
|
|
server.handleClient();
|
|
|
}
|
|
|
@@ -152,43 +119,6 @@ void MiLightHttpServer::handleSystemPost() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-void MiLightHttpServer::handleDownloadUpdate(const UrlTokenBindings* bindings) {
|
|
|
- GithubClient downloader = GithubClient::rawDownloader();
|
|
|
- const String& component = bindings->get("component");
|
|
|
-
|
|
|
- if (component.equalsIgnoreCase("web")) {
|
|
|
- Serial.println(F("Attempting to update web UI..."));
|
|
|
-
|
|
|
- bool result = false;
|
|
|
- size_t tries = 0;
|
|
|
-
|
|
|
- while (!result && tries++ <= MAX_DOWNLOAD_ATTEMPTS) {
|
|
|
- Serial.println(F("building url\n"));
|
|
|
- String urlPath = GithubClient::buildRepoPath(
|
|
|
- MILIGHT_GITHUB_USER,
|
|
|
- MILIGHT_GITHUB_REPO,
|
|
|
- MILIGHT_REPO_WEB_PATH
|
|
|
- );
|
|
|
-
|
|
|
- printf_P(PSTR("URL: %s\n"), urlPath.c_str());
|
|
|
-
|
|
|
- result = downloader.download(urlPath, WEB_INDEX_FILENAME);
|
|
|
- }
|
|
|
-
|
|
|
- Serial.println(F("Download complete!"));
|
|
|
-
|
|
|
- if (result) {
|
|
|
- server.sendHeader("Location", "/");
|
|
|
- server.send(302);
|
|
|
- } else {
|
|
|
- server.send_P(500, TEXT_PLAIN, PSTR("Failed to download update from Github. Check serial logs for more information."));
|
|
|
- }
|
|
|
- } else {
|
|
|
- String body = String("Unknown component: ") + component;
|
|
|
- server.send(400, "text/plain", body);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
void MiLightHttpServer::applySettings(Settings& settings) {
|
|
|
if (settings.hasAuthSettings()) {
|
|
|
server.requireAuthentication(settings.adminUsername, settings.adminPassword);
|
|
|
@@ -380,9 +310,9 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
|
|
|
MiLightRadioConfig* config = MiLightRadioConfig::fromString(_radioType);
|
|
|
|
|
|
if (config == NULL) {
|
|
|
- String body = "Unknown device type: ";
|
|
|
- body += String(_radioType);
|
|
|
- server.send(400, "text/plain", body);
|
|
|
+ char buffer[40];
|
|
|
+ sprintf_P(buffer, PSTR("Unknown device type: %s"), _radioType);
|
|
|
+ server.send(400, "text/plain", buffer);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
@@ -413,10 +343,9 @@ void MiLightHttpServer::handleSendRaw(const UrlTokenBindings* bindings) {
|
|
|
MiLightRadioConfig* config = MiLightRadioConfig::fromString(bindings->get("type"));
|
|
|
|
|
|
if (config == NULL) {
|
|
|
- String body = "Unknown device type: ";
|
|
|
- body += bindings->get("type");
|
|
|
-
|
|
|
- server.send(400, "text/plain", body);
|
|
|
+ char buffer[50];
|
|
|
+ sprintf_P(buffer, PSTR("Unknown device type: %s"), bindings->get("type"));
|
|
|
+ server.send(400, "text/plain", buffer);
|
|
|
return;
|
|
|
}
|
|
|
|