|
@@ -7,25 +7,23 @@
|
|
|
#include <GithubClient.h>
|
|
#include <GithubClient.h>
|
|
|
#include <string.h>
|
|
#include <string.h>
|
|
|
#include <TokenIterator.h>
|
|
#include <TokenIterator.h>
|
|
|
-#include <ESP8266SSDP.h>
|
|
|
|
|
|
|
|
|
|
void MiLightHttpServer::begin() {
|
|
void MiLightHttpServer::begin() {
|
|
|
applySettings(settings);
|
|
applySettings(settings);
|
|
|
|
|
|
|
|
server.on("/", HTTP_GET, handleServeFile(WEB_INDEX_FILENAME, "text/html", DEFAULT_INDEX_PAGE));
|
|
server.on("/", HTTP_GET, handleServeFile(WEB_INDEX_FILENAME, "text/html", DEFAULT_INDEX_PAGE));
|
|
|
- server.on("/settings", HTTP_GET, handleServeFile(SETTINGS_FILE, "application/json"));
|
|
|
|
|
|
|
+ server.on("/settings", HTTP_GET, handleServeFile(SETTINGS_FILE, APPLICATION_JSON));
|
|
|
server.on("/settings", HTTP_PUT, [this]() { handleUpdateSettings(); });
|
|
server.on("/settings", HTTP_PUT, [this]() { handleUpdateSettings(); });
|
|
|
- server.on("/settings", HTTP_POST, [this]() { server.send(200, "text/plain", "success"); }, handleUpdateFile(SETTINGS_FILE));
|
|
|
|
|
|
|
+ server.on("/settings", HTTP_POST, [this]() { server.send(200, TEXT_PLAIN, "success"); }, handleUpdateFile(SETTINGS_FILE));
|
|
|
server.on("/radio_configs", HTTP_GET, [this]() { handleGetRadioConfigs(); });
|
|
server.on("/radio_configs", HTTP_GET, [this]() { handleGetRadioConfigs(); });
|
|
|
server.onPattern("/gateway_traffic/:type", HTTP_GET, [this](const UrlTokenBindings* b) { handleListenGateway(b); });
|
|
server.onPattern("/gateway_traffic/:type", HTTP_GET, [this](const UrlTokenBindings* b) { handleListenGateway(b); });
|
|
|
server.onPattern("/gateways/:device_id/:type/:group_id", HTTP_ANY, [this](const UrlTokenBindings* b) { handleUpdateGroup(b); });
|
|
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("/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.onPattern("/download_update/:component", HTTP_GET, [this](const UrlTokenBindings* b) { handleDownloadUpdate(b); });
|
|
|
- server.on("/web", HTTP_POST, [this]() { server.send(200, "text/plain", "success"); }, handleUpdateFile(WEB_INDEX_FILENAME));
|
|
|
|
|
|
|
+ server.on("/web", HTTP_POST, [this]() { server.send(200, TEXT_PLAIN, "success"); }, handleUpdateFile(WEB_INDEX_FILENAME));
|
|
|
server.on("/about", HTTP_GET, [this]() { handleAbout(); });
|
|
server.on("/about", HTTP_GET, [this]() { handleAbout(); });
|
|
|
server.on("/latest_release", HTTP_GET, [this]() { handleGetLatestRelease(); });
|
|
server.on("/latest_release", HTTP_GET, [this]() { handleGetLatestRelease(); });
|
|
|
server.on("/system", HTTP_POST, [this]() { handleSystemPost(); });
|
|
server.on("/system", HTTP_POST, [this]() { handleSystemPost(); });
|
|
|
- server.on("/description.xml", HTTP_GET, [this]() { SSDP.schema(server.client()); });
|
|
|
|
|
server.on("/firmware", HTTP_POST,
|
|
server.on("/firmware", HTTP_POST,
|
|
|
[this](){
|
|
[this](){
|
|
|
server.sendHeader("Connection", "close");
|
|
server.sendHeader("Connection", "close");
|
|
@@ -34,13 +32,13 @@ void MiLightHttpServer::begin() {
|
|
|
if (Update.hasError()) {
|
|
if (Update.hasError()) {
|
|
|
server.send_P(
|
|
server.send_P(
|
|
|
500,
|
|
500,
|
|
|
- "text/plain",
|
|
|
|
|
|
|
+ TEXT_PLAIN,
|
|
|
PSTR("Failed updating firmware. Check serial logs for more information. You may need to re-flash the device.")
|
|
PSTR("Failed updating firmware. Check serial logs for more information. You may need to re-flash the device.")
|
|
|
);
|
|
);
|
|
|
} else {
|
|
} else {
|
|
|
server.send_P(
|
|
server.send_P(
|
|
|
200,
|
|
200,
|
|
|
- "text/plain",
|
|
|
|
|
|
|
+ TEXT_PLAIN,
|
|
|
PSTR("Success. Device will now reboot.")
|
|
PSTR("Success. Device will now reboot.")
|
|
|
);
|
|
);
|
|
|
}
|
|
}
|
|
@@ -95,12 +93,12 @@ void MiLightHttpServer::handleGetLatestRelease() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (!SPIFFS.exists(fsPath)) {
|
|
if (!SPIFFS.exists(fsPath)) {
|
|
|
- server.send_P(500, "text/plain", PSTR("Failed to stream API request from GitHub. Check Serial logs for more information."));
|
|
|
|
|
|
|
+ server.send_P(500, TEXT_PLAIN, PSTR("Failed to stream API request from GitHub. Check Serial logs for more information."));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
File file = SPIFFS.open(fsPath, "r");
|
|
File file = SPIFFS.open(fsPath, "r");
|
|
|
- server.streamFile(file, "application/json");
|
|
|
|
|
|
|
+ server.streamFile(file, APPLICATION_JSON);
|
|
|
SPIFFS.remove(fsPath);
|
|
SPIFFS.remove(fsPath);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -125,7 +123,7 @@ void MiLightHttpServer::handleSystemPost() {
|
|
|
if (request.containsKey("command")) {
|
|
if (request.containsKey("command")) {
|
|
|
if (request["command"] == "restart") {
|
|
if (request["command"] == "restart") {
|
|
|
Serial.println(F("Restarting..."));
|
|
Serial.println(F("Restarting..."));
|
|
|
- server.send(200, "text/plain", "true");
|
|
|
|
|
|
|
+ server.send(200, TEXT_PLAIN, "true");
|
|
|
|
|
|
|
|
delay(100);
|
|
delay(100);
|
|
|
|
|
|
|
@@ -134,9 +132,9 @@ void MiLightHttpServer::handleSystemPost() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (handled) {
|
|
if (handled) {
|
|
|
- server.send(200, "text/plain", "true");
|
|
|
|
|
|
|
+ server.send(200, TEXT_PLAIN, "true");
|
|
|
} else {
|
|
} else {
|
|
|
- server.send(400, "text/plain", F("{\"error\":\"Unhandled command\"}"));
|
|
|
|
|
|
|
+ server.send(400, TEXT_PLAIN, F("{\"error\":\"Unhandled command\"}"));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -151,14 +149,14 @@ void MiLightHttpServer::handleDownloadUpdate(const UrlTokenBindings* bindings) {
|
|
|
size_t tries = 0;
|
|
size_t tries = 0;
|
|
|
|
|
|
|
|
while (!result && tries++ <= MAX_DOWNLOAD_ATTEMPTS) {
|
|
while (!result && tries++ <= MAX_DOWNLOAD_ATTEMPTS) {
|
|
|
- printf("building url\n");
|
|
|
|
|
|
|
+ Serial.println(F("building url\n"));
|
|
|
String urlPath = GithubClient::buildRepoPath(
|
|
String urlPath = GithubClient::buildRepoPath(
|
|
|
MILIGHT_GITHUB_USER,
|
|
MILIGHT_GITHUB_USER,
|
|
|
MILIGHT_GITHUB_REPO,
|
|
MILIGHT_GITHUB_REPO,
|
|
|
MILIGHT_REPO_WEB_PATH
|
|
MILIGHT_REPO_WEB_PATH
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
- printf("URL: %s\n", urlPath.c_str());
|
|
|
|
|
|
|
+ printf_P(PSTR("URL: %s\n"), urlPath.c_str());
|
|
|
|
|
|
|
|
result = downloader.download(urlPath, WEB_INDEX_FILENAME);
|
|
result = downloader.download(urlPath, WEB_INDEX_FILENAME);
|
|
|
}
|
|
}
|
|
@@ -169,11 +167,11 @@ void MiLightHttpServer::handleDownloadUpdate(const UrlTokenBindings* bindings) {
|
|
|
server.sendHeader("Location", "/");
|
|
server.sendHeader("Location", "/");
|
|
|
server.send(302);
|
|
server.send(302);
|
|
|
} else {
|
|
} else {
|
|
|
- server.send(500, "text/plain", F("Failed to download update from Github. Check serial logs for more information."));
|
|
|
|
|
|
|
+ server.send(500, TEXT_PLAIN, F("Failed to download update from Github. Check serial logs for more information."));
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
String body = String("Unknown component: ") + component;
|
|
String body = String("Unknown component: ") + component;
|
|
|
- server.send(400, "text/plain", body);
|
|
|
|
|
|
|
+ server.send(400, TEXT_PLAIN, body);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -197,6 +195,7 @@ void MiLightHttpServer::handleAbout() {
|
|
|
|
|
|
|
|
response["version"] = QUOTE(MILIGHT_HUB_VERSION);
|
|
response["version"] = QUOTE(MILIGHT_HUB_VERSION);
|
|
|
response["variant"] = QUOTE(FIRMWARE_VARIANT);
|
|
response["variant"] = QUOTE(FIRMWARE_VARIANT);
|
|
|
|
|
+ response["free_heap"] = ESP.getFreeHeap();
|
|
|
|
|
|
|
|
String body;
|
|
String body;
|
|
|
response.printTo(body);
|
|
response.printTo(body);
|
|
@@ -216,7 +215,7 @@ void MiLightHttpServer::handleGetRadioConfigs() {
|
|
|
String body;
|
|
String body;
|
|
|
arr.printTo(body);
|
|
arr.printTo(body);
|
|
|
|
|
|
|
|
- server.send(200, "application/json", body);
|
|
|
|
|
|
|
+ server.send(200, APPLICATION_JSON, body);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
ESP8266WebServer::THandlerFunction MiLightHttpServer::handleServeFile(
|
|
ESP8266WebServer::THandlerFunction MiLightHttpServer::handleServeFile(
|
|
@@ -274,9 +273,9 @@ void MiLightHttpServer::handleUpdateSettings() {
|
|
|
this->applySettings(settings);
|
|
this->applySettings(settings);
|
|
|
this->settingsSavedHandler();
|
|
this->settingsSavedHandler();
|
|
|
|
|
|
|
|
- server.send(200, "application/json", "true");
|
|
|
|
|
|
|
+ server.send(200, APPLICATION_JSON, "true");
|
|
|
} else {
|
|
} else {
|
|
|
- server.send(400, "application/json", "\"Invalid JSON\"");
|
|
|
|
|
|
|
+ server.send(400, APPLICATION_JSON, "\"Invalid JSON\"");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -288,7 +287,7 @@ void MiLightHttpServer::handleListenGateway(const UrlTokenBindings* bindings) {
|
|
|
String body = "Unknown device type: ";
|
|
String body = "Unknown device type: ";
|
|
|
body += bindings->get("type");
|
|
body += bindings->get("type");
|
|
|
|
|
|
|
|
- server.send(400, "text/plain", body);
|
|
|
|
|
|
|
+ server.send(400, TEXT_PLAIN, body);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -312,10 +311,10 @@ void MiLightHttpServer::handleListenGateway(const UrlTokenBindings* bindings) {
|
|
|
char response[200];
|
|
char response[200];
|
|
|
char* responseBuffer = response;
|
|
char* responseBuffer = response;
|
|
|
|
|
|
|
|
- responseBuffer += sprintf(responseBuffer, "\nPacket received (%d bytes):\n", sizeof(packet));
|
|
|
|
|
|
|
+ responseBuffer += sprintf_P(responseBuffer, PSTR("\nPacket received (%d bytes):\n"), sizeof(packet));
|
|
|
milightClient->formatPacket(packet, responseBuffer);
|
|
milightClient->formatPacket(packet, responseBuffer);
|
|
|
|
|
|
|
|
- server.send(200, "text/plain", response);
|
|
|
|
|
|
|
+ server.send(200, TEXT_PLAIN, response);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
|
|
void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
|
|
@@ -323,7 +322,7 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
|
|
|
JsonObject& request = buffer.parse(server.arg("plain"));
|
|
JsonObject& request = buffer.parse(server.arg("plain"));
|
|
|
|
|
|
|
|
if (!request.success()) {
|
|
if (!request.success()) {
|
|
|
- server.send(400, "text/plain", F("Invalid JSON"));
|
|
|
|
|
|
|
+ server.send(400, TEXT_PLAIN, F("Invalid JSON"));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -352,7 +351,7 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
|
|
|
if (config == NULL) {
|
|
if (config == NULL) {
|
|
|
String body = "Unknown device type: ";
|
|
String body = "Unknown device type: ";
|
|
|
body += String(_radioType);
|
|
body += String(_radioType);
|
|
|
- server.send(400, "text/plain", body);
|
|
|
|
|
|
|
+ server.send(400, TEXT_PLAIN, body);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -370,7 +369,7 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- server.send(200, "application/json", "true");
|
|
|
|
|
|
|
+ server.send(200, APPLICATION_JSON, "true");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MiLightHttpServer::handleRequest(const JsonObject& request) {
|
|
void MiLightHttpServer::handleRequest(const JsonObject& request) {
|
|
@@ -462,7 +461,7 @@ void MiLightHttpServer::handleSendRaw(const UrlTokenBindings* bindings) {
|
|
|
String body = "Unknown device type: ";
|
|
String body = "Unknown device type: ";
|
|
|
body += bindings->get("type");
|
|
body += bindings->get("type");
|
|
|
|
|
|
|
|
- server.send(400, "text/plain", body);
|
|
|
|
|
|
|
+ server.send(400, TEXT_PLAIN, body);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -481,5 +480,5 @@ void MiLightHttpServer::handleSendRaw(const UrlTokenBindings* bindings) {
|
|
|
milightClient->write(packet);
|
|
milightClient->write(packet);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- server.send(200, "text/plain", "true");
|
|
|
|
|
|
|
+ server.send(200, TEXT_PLAIN, "true");
|
|
|
}
|
|
}
|