|
@@ -9,33 +9,60 @@
|
|
|
#include <AboutHelper.h>
|
|
#include <AboutHelper.h>
|
|
|
#include <index.html.gz.h>
|
|
#include <index.html.gz.h>
|
|
|
|
|
|
|
|
-void MiLightHttpServer::begin() {
|
|
|
|
|
- applySettings(settings);
|
|
|
|
|
|
|
+using namespace std::placeholders;
|
|
|
|
|
|
|
|
|
|
+void MiLightHttpServer::begin() {
|
|
|
// set up HTTP end points to serve
|
|
// set up HTTP end points to serve
|
|
|
|
|
|
|
|
- _handleRootPage = handleServe_P(index_html_gz, index_html_gz_len);
|
|
|
|
|
- server.onAuthenticated("/", HTTP_GET, [this]() { _handleRootPage(); });
|
|
|
|
|
- server.onAuthenticated("/settings", HTTP_GET, [this]() { serveSettings(); });
|
|
|
|
|
- server.onAuthenticated("/settings", HTTP_PUT, [this]() { handleUpdateSettings(); });
|
|
|
|
|
- server.onAuthenticated("/settings", HTTP_POST, [this]() { handleUpdateSettingsPost(); }, handleUpdateFile(SETTINGS_FILE));
|
|
|
|
|
- server.onAuthenticated("/remote_configs", HTTP_GET, [this]() { handleGetRadioConfigs(); });
|
|
|
|
|
|
|
+ server
|
|
|
|
|
+ .buildHandler("/")
|
|
|
|
|
+ .onSimple(HTTP_GET, std::bind(&MiLightHttpServer::handleServe_P, this, index_html_gz, index_html_gz_len));
|
|
|
|
|
+
|
|
|
|
|
+ server
|
|
|
|
|
+ .buildHandler("/settings")
|
|
|
|
|
+ .on(HTTP_GET, std::bind(&MiLightHttpServer::serveSettings, this))
|
|
|
|
|
+ .on(HTTP_PUT, std::bind(&MiLightHttpServer::handleUpdateSettings, this, _1))
|
|
|
|
|
+ .on(
|
|
|
|
|
+ HTTP_POST,
|
|
|
|
|
+ std::bind(&MiLightHttpServer::handleUpdateSettingsPost, this, _1),
|
|
|
|
|
+ std::bind(&MiLightHttpServer::handleUpdateFile, this, SETTINGS_FILE)
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ server
|
|
|
|
|
+ .buildHandler("/remote_configs")
|
|
|
|
|
+ .on(HTTP_GET, std::bind(&MiLightHttpServer::handleGetRadioConfigs, this, _1));
|
|
|
|
|
+
|
|
|
|
|
+ server
|
|
|
|
|
+ .buildHandler("/gateway_traffic")
|
|
|
|
|
+ .on(HTTP_GET, std::bind(&MiLightHttpServer::handleListenGateway, this, _1));
|
|
|
|
|
+ server
|
|
|
|
|
+ .buildHandler("/gateway_traffic/:type")
|
|
|
|
|
+ .on(HTTP_GET, std::bind(&MiLightHttpServer::handleListenGateway, this, _1));
|
|
|
|
|
+
|
|
|
|
|
+ server
|
|
|
|
|
+ .buildHandler("/gateways/:device_id/:type/:group_id")
|
|
|
|
|
+ .on(HTTP_PUT, std::bind(&MiLightHttpServer::handleUpdateGroup, this, _1))
|
|
|
|
|
+ .on(HTTP_POST, std::bind(&MiLightHttpServer::handleUpdateGroup, this, _1))
|
|
|
|
|
+ .on(HTTP_DELETE, std::bind(&MiLightHttpServer::handleDeleteGroup, this, _1))
|
|
|
|
|
+ .on(HTTP_GET, std::bind(&MiLightHttpServer::handleGetGroup, this, _1));
|
|
|
|
|
|
|
|
- server.onAuthenticated("/gateway_traffic", HTTP_GET, [this]() { handleListenGateway(NULL); });
|
|
|
|
|
- server.onPatternAuthenticated("/gateway_traffic/:type", HTTP_GET, [this](const UrlTokenBindings* b) { handleListenGateway(b); });
|
|
|
|
|
|
|
+ server
|
|
|
|
|
+ .buildHandler("/raw_commands/:type")
|
|
|
|
|
+ .on(HTTP_ANY, std::bind(&MiLightHttpServer::handleSendRaw, this, _1));
|
|
|
|
|
|
|
|
- const char groupPattern[] = "/gateways/:device_id/:type/:group_id";
|
|
|
|
|
- server.onPatternAuthenticated(groupPattern, HTTP_PUT, [this](const UrlTokenBindings* b) { handleUpdateGroup(b); });
|
|
|
|
|
- server.onPatternAuthenticated(groupPattern, HTTP_POST, [this](const UrlTokenBindings* b) { handleUpdateGroup(b); });
|
|
|
|
|
- server.onPatternAuthenticated(groupPattern, HTTP_DELETE, [this](const UrlTokenBindings* b) { handleDeleteGroup(b); });
|
|
|
|
|
- server.onPatternAuthenticated(groupPattern, HTTP_GET, [this](const UrlTokenBindings* b) { handleGetGroup(b); });
|
|
|
|
|
|
|
+ server
|
|
|
|
|
+ .buildHandler("/about")
|
|
|
|
|
+ .on(HTTP_GET, std::bind(&MiLightHttpServer::handleAbout, this, _1));
|
|
|
|
|
|
|
|
- server.onPatternAuthenticated("/raw_commands/:type", HTTP_ANY, [this](const UrlTokenBindings* b) { handleSendRaw(b); });
|
|
|
|
|
- server.onAuthenticated("/web", HTTP_POST, [this]() { server.send_P(200, TEXT_PLAIN, PSTR("success")); }, handleUpdateFile(WEB_INDEX_FILENAME));
|
|
|
|
|
- server.onAuthenticated("/about", HTTP_GET, [this]() { handleAbout(); });
|
|
|
|
|
- server.onAuthenticated("/system", HTTP_POST, [this]() { handleSystemPost(); });
|
|
|
|
|
- server.onAuthenticated("/firmware", HTTP_POST, [this]() { handleFirmwarePost(); }, [this]() { handleFirmwareUpload(); });
|
|
|
|
|
|
|
+ server
|
|
|
|
|
+ .buildHandler("/system")
|
|
|
|
|
+ .on(HTTP_POST, std::bind(&MiLightHttpServer::handleSystemPost, this, _1));
|
|
|
|
|
|
|
|
|
|
+ server
|
|
|
|
|
+ .buildHandler("/firmware")
|
|
|
|
|
+ .handleOTA();
|
|
|
|
|
+
|
|
|
|
|
+ server.clearBuilders();
|
|
|
|
|
|
|
|
// set up web socket server
|
|
// set up web socket server
|
|
|
wsServer.onEvent(
|
|
wsServer.onEvent(
|
|
@@ -53,22 +80,21 @@ void MiLightHttpServer::handleClient() {
|
|
|
wsServer.loop();
|
|
wsServer.loop();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::on(const char* path, HTTPMethod method, ESP8266WebServer::THandlerFunction handler) {
|
|
|
|
|
- server.on(path, method, handler);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
WiFiClient MiLightHttpServer::client() {
|
|
WiFiClient MiLightHttpServer::client() {
|
|
|
return server.client();
|
|
return server.client();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::handleSystemPost() {
|
|
|
|
|
- DynamicJsonBuffer buffer;
|
|
|
|
|
- JsonObject& request = buffer.parse(server.arg("plain"));
|
|
|
|
|
|
|
+void MiLightHttpServer::on(const char* path, HTTPMethod method, ESP8266WebServer::THandlerFunction handler) {
|
|
|
|
|
+ server.on(path, method, handler);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void MiLightHttpServer::handleSystemPost(RequestContext& request) {
|
|
|
|
|
+ JsonObject requestBody = request.getJsonBody().as<JsonObject>();
|
|
|
|
|
|
|
|
bool handled = false;
|
|
bool handled = false;
|
|
|
|
|
|
|
|
- if (request.containsKey("command")) {
|
|
|
|
|
- if (request["command"] == "restart") {
|
|
|
|
|
|
|
+ if (requestBody.containsKey("command")) {
|
|
|
|
|
+ if (requestBody["command"] == "restart") {
|
|
|
Serial.println(F("Restarting..."));
|
|
Serial.println(F("Restarting..."));
|
|
|
server.send_P(200, TEXT_PLAIN, PSTR("true"));
|
|
server.send_P(200, TEXT_PLAIN, PSTR("true"));
|
|
|
|
|
|
|
@@ -77,7 +103,7 @@ void MiLightHttpServer::handleSystemPost() {
|
|
|
ESP.restart();
|
|
ESP.restart();
|
|
|
|
|
|
|
|
handled = true;
|
|
handled = true;
|
|
|
- } else if (request["command"] == "clear_wifi_config") {
|
|
|
|
|
|
|
+ } else if (requestBody["command"] == "clear_wifi_config") {
|
|
|
Serial.println(F("Resetting Wifi and then Restarting..."));
|
|
Serial.println(F("Resetting Wifi and then Restarting..."));
|
|
|
server.send_P(200, TEXT_PLAIN, PSTR("true"));
|
|
server.send_P(200, TEXT_PLAIN, PSTR("true"));
|
|
|
|
|
|
|
@@ -91,9 +117,11 @@ void MiLightHttpServer::handleSystemPost() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (handled) {
|
|
if (handled) {
|
|
|
- server.send_P(200, TEXT_PLAIN, PSTR("true"));
|
|
|
|
|
|
|
+ request.response.json["success"] = true;
|
|
|
} else {
|
|
} else {
|
|
|
- server.send_P(400, TEXT_PLAIN, PSTR("{\"error\":\"Unhandled command\"}"));
|
|
|
|
|
|
|
+ request.response.json["success"] = false;
|
|
|
|
|
+ request.response.json["error"] = "Unhandled command";
|
|
|
|
|
+ request.response.setCode(400);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -103,14 +131,6 @@ void MiLightHttpServer::serveSettings() {
|
|
|
serveFile(SETTINGS_FILE, APPLICATION_JSON);
|
|
serveFile(SETTINGS_FILE, APPLICATION_JSON);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::applySettings(Settings& settings) {
|
|
|
|
|
- if (settings.hasAuthSettings()) {
|
|
|
|
|
- server.requireAuthentication(settings.adminUsername, settings.adminPassword);
|
|
|
|
|
- } else {
|
|
|
|
|
- server.disableAuthentication();
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
void MiLightHttpServer::onSettingsSaved(SettingsSavedHandler handler) {
|
|
void MiLightHttpServer::onSettingsSaved(SettingsSavedHandler handler) {
|
|
|
this->settingsSavedHandler = handler;
|
|
this->settingsSavedHandler = handler;
|
|
|
}
|
|
}
|
|
@@ -119,39 +139,17 @@ void MiLightHttpServer::onGroupDeleted(GroupDeletedHandler handler) {
|
|
|
this->groupDeletedHandler = handler;
|
|
this->groupDeletedHandler = handler;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::handleAbout() {
|
|
|
|
|
- server.send(200, APPLICATION_JSON, AboutHelper::generateAboutString());
|
|
|
|
|
|
|
+void MiLightHttpServer::handleAbout(RequestContext& request) {
|
|
|
|
|
+ AboutHelper::generateAboutObject(request.response.json);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::handleGetRadioConfigs() {
|
|
|
|
|
- DynamicJsonBuffer buffer;
|
|
|
|
|
- JsonArray& arr = buffer.createArray();
|
|
|
|
|
|
|
+void MiLightHttpServer::handleGetRadioConfigs(RequestContext& request) {
|
|
|
|
|
+ JsonArray arr = request.response.json.to<JsonArray>();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < MiLightRemoteConfig::NUM_REMOTES; i++) {
|
|
for (size_t i = 0; i < MiLightRemoteConfig::NUM_REMOTES; i++) {
|
|
|
const MiLightRemoteConfig* config = MiLightRemoteConfig::ALL_REMOTES[i];
|
|
const MiLightRemoteConfig* config = MiLightRemoteConfig::ALL_REMOTES[i];
|
|
|
arr.add(config->name);
|
|
arr.add(config->name);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- String body;
|
|
|
|
|
- arr.printTo(body);
|
|
|
|
|
-
|
|
|
|
|
- server.send(200, APPLICATION_JSON, body);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-ESP8266WebServer::THandlerFunction MiLightHttpServer::handleServeFile(
|
|
|
|
|
- const char* filename,
|
|
|
|
|
- const char* contentType,
|
|
|
|
|
- const char* defaultText) {
|
|
|
|
|
-
|
|
|
|
|
- return [this, filename, contentType, defaultText]() {
|
|
|
|
|
- if (!serveFile(filename)) {
|
|
|
|
|
- if (defaultText) {
|
|
|
|
|
- server.send(200, contentType, defaultText);
|
|
|
|
|
- } else {
|
|
|
|
|
- server.send(404);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool MiLightHttpServer::serveFile(const char* file, const char* contentType) {
|
|
bool MiLightHttpServer::serveFile(const char* file, const char* contentType) {
|
|
@@ -165,54 +163,44 @@ bool MiLightHttpServer::serveFile(const char* file, const char* contentType) {
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-ESP8266WebServer::THandlerFunction MiLightHttpServer::handleUpdateFile(const char* filename) {
|
|
|
|
|
- return [this, filename]() {
|
|
|
|
|
- HTTPUpload& upload = server.upload();
|
|
|
|
|
|
|
+void MiLightHttpServer::handleUpdateFile(const char* filename) {
|
|
|
|
|
+ HTTPUpload& upload = server.upload();
|
|
|
|
|
|
|
|
- if (upload.status == UPLOAD_FILE_START) {
|
|
|
|
|
- updateFile = SPIFFS.open(filename, "w");
|
|
|
|
|
- } else if(upload.status == UPLOAD_FILE_WRITE){
|
|
|
|
|
- if (updateFile.write(upload.buf, upload.currentSize) != upload.currentSize) {
|
|
|
|
|
- Serial.println(F("Error updating web file"));
|
|
|
|
|
- }
|
|
|
|
|
- } else if (upload.status == UPLOAD_FILE_END) {
|
|
|
|
|
- updateFile.close();
|
|
|
|
|
|
|
+ if (upload.status == UPLOAD_FILE_START) {
|
|
|
|
|
+ updateFile = SPIFFS.open(filename, "w");
|
|
|
|
|
+ } else if(upload.status == UPLOAD_FILE_WRITE){
|
|
|
|
|
+ if (updateFile.write(upload.buf, upload.currentSize) != upload.currentSize) {
|
|
|
|
|
+ Serial.println(F("Error updating web file"));
|
|
|
}
|
|
}
|
|
|
- };
|
|
|
|
|
|
|
+ } else if (upload.status == UPLOAD_FILE_END) {
|
|
|
|
|
+ updateFile.close();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::handleUpdateSettings() {
|
|
|
|
|
- DynamicJsonBuffer buffer;
|
|
|
|
|
- const String& rawSettings = server.arg("plain");
|
|
|
|
|
- JsonObject& parsedSettings = buffer.parse(rawSettings);
|
|
|
|
|
|
|
+void MiLightHttpServer::handleUpdateSettings(RequestContext& request) {
|
|
|
|
|
+ JsonObject parsedSettings = request.getJsonBody().as<JsonObject>();
|
|
|
|
|
|
|
|
- if (parsedSettings.success()) {
|
|
|
|
|
|
|
+ if (! parsedSettings.isNull()) {
|
|
|
settings.patch(parsedSettings);
|
|
settings.patch(parsedSettings);
|
|
|
settings.save();
|
|
settings.save();
|
|
|
|
|
|
|
|
- this->applySettings(settings);
|
|
|
|
|
-
|
|
|
|
|
if (this->settingsSavedHandler) {
|
|
if (this->settingsSavedHandler) {
|
|
|
this->settingsSavedHandler();
|
|
this->settingsSavedHandler();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- server.send(200, APPLICATION_JSON, "true");
|
|
|
|
|
|
|
+ request.response.json["success"] = true;
|
|
|
Serial.println(F("Settings successfully updated"));
|
|
Serial.println(F("Settings successfully updated"));
|
|
|
- } else {
|
|
|
|
|
- server.send(400, APPLICATION_JSON, "\"Invalid JSON\"");
|
|
|
|
|
- Serial.println(F("Settings failed to update; invalid JSON"));
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::handleUpdateSettingsPost() {
|
|
|
|
|
|
|
+void MiLightHttpServer::handleUpdateSettingsPost(RequestContext& request) {
|
|
|
Settings::load(settings);
|
|
Settings::load(settings);
|
|
|
|
|
|
|
|
- this->applySettings(settings);
|
|
|
|
|
if (this->settingsSavedHandler) {
|
|
if (this->settingsSavedHandler) {
|
|
|
this->settingsSavedHandler();
|
|
this->settingsSavedHandler();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- server.send_P(200, TEXT_PLAIN, PSTR("success."));
|
|
|
|
|
|
|
+ request.response.json["success"] = true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MiLightHttpServer::handleFirmwarePost() {
|
|
void MiLightHttpServer::handleFirmwarePost() {
|
|
@@ -260,38 +248,46 @@ void MiLightHttpServer::handleFirmwareUpload() {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
-void MiLightHttpServer::handleListenGateway(const UrlTokenBindings* bindings) {
|
|
|
|
|
- bool listenAll = bindings == NULL;
|
|
|
|
|
|
|
+void MiLightHttpServer::handleListenGateway(RequestContext& request) {
|
|
|
|
|
+ bool listenAll = !request.pathVariables.hasBinding("type");
|
|
|
size_t configIx = 0;
|
|
size_t configIx = 0;
|
|
|
- const MiLightRadioConfig* radioConfig = NULL;
|
|
|
|
|
|
|
+ std::shared_ptr<MiLightRadio> radio = NULL;
|
|
|
const MiLightRemoteConfig* remoteConfig = NULL;
|
|
const MiLightRemoteConfig* remoteConfig = NULL;
|
|
|
|
|
+ const MiLightRemoteConfig* tmpRemoteConfig = NULL;
|
|
|
|
|
+
|
|
|
uint8_t packet[MILIGHT_MAX_PACKET_LENGTH];
|
|
uint8_t packet[MILIGHT_MAX_PACKET_LENGTH];
|
|
|
|
|
|
|
|
- if (bindings != NULL) {
|
|
|
|
|
- String strType(bindings->get("type"));
|
|
|
|
|
- const MiLightRemoteConfig* remoteConfig = MiLightRemoteConfig::fromType(strType);
|
|
|
|
|
- milightClient->prepare(remoteConfig, 0, 0);
|
|
|
|
|
- radioConfig = &remoteConfig->radioConfig;
|
|
|
|
|
|
|
+ if (!listenAll) {
|
|
|
|
|
+ String strType(request.pathVariables.get("type"));
|
|
|
|
|
+ tmpRemoteConfig = MiLightRemoteConfig::fromType(strType);
|
|
|
|
|
+ milightClient->prepare(tmpRemoteConfig, 0, 0);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (radioConfig == NULL && !listenAll) {
|
|
|
|
|
- server.send_P(400, TEXT_PLAIN, PSTR("Unknown device type supplied."));
|
|
|
|
|
|
|
+ if (tmpRemoteConfig == NULL && !listenAll) {
|
|
|
|
|
+ request.response.setCode(400);
|
|
|
|
|
+ request.response.json["error"] = "Unknown device type supplied";
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if (tmpRemoteConfig != NULL) {
|
|
|
|
|
+ radio = milightClient->switchRadio(tmpRemoteConfig);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
while (remoteConfig == NULL) {
|
|
while (remoteConfig == NULL) {
|
|
|
- if (!server.clientConnected()) {
|
|
|
|
|
|
|
+ if (!server.client().connected()) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (listenAll) {
|
|
if (listenAll) {
|
|
|
- radioConfig = &milightClient->switchRadio(configIx++ % milightClient->getNumRadios())->config();
|
|
|
|
|
|
|
+ radio = milightClient->switchRadio(configIx++ % milightClient->getNumRadios());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ radio->configure();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (milightClient->available()) {
|
|
if (milightClient->available()) {
|
|
|
size_t packetLen = milightClient->read(packet);
|
|
size_t packetLen = milightClient->read(packet);
|
|
|
remoteConfig = MiLightRemoteConfig::fromReceivedPacket(
|
|
remoteConfig = MiLightRemoteConfig::fromReceivedPacket(
|
|
|
- *radioConfig,
|
|
|
|
|
|
|
+ radio->config(),
|
|
|
packet,
|
|
packet,
|
|
|
packetLen
|
|
packetLen
|
|
|
);
|
|
);
|
|
@@ -300,8 +296,8 @@ void MiLightHttpServer::handleListenGateway(const UrlTokenBindings* bindings) {
|
|
|
yield();
|
|
yield();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- char response[200];
|
|
|
|
|
- char* responseBuffer = response;
|
|
|
|
|
|
|
+ char responseBody[200];
|
|
|
|
|
+ char* responseBuffer = responseBody;
|
|
|
|
|
|
|
|
responseBuffer += sprintf_P(
|
|
responseBuffer += sprintf_P(
|
|
|
responseBuffer,
|
|
responseBuffer,
|
|
@@ -311,77 +307,68 @@ void MiLightHttpServer::handleListenGateway(const UrlTokenBindings* bindings) {
|
|
|
);
|
|
);
|
|
|
remoteConfig->packetFormatter->format(packet, responseBuffer);
|
|
remoteConfig->packetFormatter->format(packet, responseBuffer);
|
|
|
|
|
|
|
|
- server.send(200, "text/plain", response);
|
|
|
|
|
|
|
+ request.response.json["packet_info"] = responseBody;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::sendGroupState(BulbId& bulbId, GroupState* state) {
|
|
|
|
|
- String body;
|
|
|
|
|
- StaticJsonBuffer<200> jsonBuffer;
|
|
|
|
|
- JsonObject& obj = jsonBuffer.createObject();
|
|
|
|
|
|
|
+void MiLightHttpServer::sendGroupState(BulbId& bulbId, GroupState* state, RichHttp::Response& response) {
|
|
|
|
|
+ JsonObject obj = response.json.to<JsonObject>();
|
|
|
|
|
|
|
|
if (state != NULL) {
|
|
if (state != NULL) {
|
|
|
- state->applyState(obj, bulbId, settings.groupStateFields, settings.numGroupStateFields);
|
|
|
|
|
|
|
+ state->applyState(obj, bulbId, settings.groupStateFields);
|
|
|
|
|
+ state->debugState("test");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- obj.printTo(body);
|
|
|
|
|
-
|
|
|
|
|
- server.send(200, APPLICATION_JSON, body);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::handleGetGroup(const UrlTokenBindings* urlBindings) {
|
|
|
|
|
- const String _deviceId = urlBindings->get("device_id");
|
|
|
|
|
- uint8_t _groupId = atoi(urlBindings->get("group_id"));
|
|
|
|
|
- const MiLightRemoteConfig* _remoteType = MiLightRemoteConfig::fromType(urlBindings->get("type"));
|
|
|
|
|
|
|
+void MiLightHttpServer::handleGetGroup(RequestContext& request) {
|
|
|
|
|
+ const String _deviceId = request.pathVariables.get("device_id");
|
|
|
|
|
+ uint8_t _groupId = atoi(request.pathVariables.get("group_id"));
|
|
|
|
|
+ const MiLightRemoteConfig* _remoteType = MiLightRemoteConfig::fromType(request.pathVariables.get("type"));
|
|
|
|
|
|
|
|
if (_remoteType == NULL) {
|
|
if (_remoteType == NULL) {
|
|
|
char buffer[40];
|
|
char buffer[40];
|
|
|
sprintf_P(buffer, PSTR("Unknown device type\n"));
|
|
sprintf_P(buffer, PSTR("Unknown device type\n"));
|
|
|
- server.send(400, TEXT_PLAIN, buffer);
|
|
|
|
|
|
|
+ request.response.setCode(400);
|
|
|
|
|
+ request.response.json["error"] = buffer;
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
BulbId bulbId(parseInt<uint16_t>(_deviceId), _groupId, _remoteType->type);
|
|
BulbId bulbId(parseInt<uint16_t>(_deviceId), _groupId, _remoteType->type);
|
|
|
- sendGroupState(bulbId, stateStore->get(bulbId));
|
|
|
|
|
|
|
+ sendGroupState(bulbId, stateStore->get(bulbId), request.response);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::handleDeleteGroup(const UrlTokenBindings* urlBindings) {
|
|
|
|
|
- const String _deviceId = urlBindings->get("device_id");
|
|
|
|
|
- uint8_t _groupId = atoi(urlBindings->get("group_id"));
|
|
|
|
|
- const MiLightRemoteConfig* _remoteType = MiLightRemoteConfig::fromType(urlBindings->get("type"));
|
|
|
|
|
|
|
+void MiLightHttpServer::handleDeleteGroup(RequestContext& request) {
|
|
|
|
|
+ const String _deviceId = request.pathVariables.get("device_id");
|
|
|
|
|
+ uint8_t _groupId = atoi(request.pathVariables.get("group_id"));
|
|
|
|
|
+ const MiLightRemoteConfig* _remoteType = MiLightRemoteConfig::fromType(request.pathVariables.get("type"));
|
|
|
|
|
|
|
|
if (_remoteType == NULL) {
|
|
if (_remoteType == NULL) {
|
|
|
char buffer[40];
|
|
char buffer[40];
|
|
|
sprintf_P(buffer, PSTR("Unknown device type\n"));
|
|
sprintf_P(buffer, PSTR("Unknown device type\n"));
|
|
|
- server.send(400, TEXT_PLAIN, buffer);
|
|
|
|
|
|
|
+ request.response.setCode(400);
|
|
|
|
|
+ request.response.json["error"] = buffer;
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
BulbId bulbId(parseInt<uint16_t>(_deviceId), _groupId, _remoteType->type);
|
|
BulbId bulbId(parseInt<uint16_t>(_deviceId), _groupId, _remoteType->type);
|
|
|
stateStore->clear(bulbId);
|
|
stateStore->clear(bulbId);
|
|
|
|
|
|
|
|
- server.send_P(200, APPLICATION_JSON, PSTR("true"));
|
|
|
|
|
-
|
|
|
|
|
if (groupDeletedHandler != NULL) {
|
|
if (groupDeletedHandler != NULL) {
|
|
|
this->groupDeletedHandler(bulbId);
|
|
this->groupDeletedHandler(bulbId);
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
-void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
|
|
|
|
|
- DynamicJsonBuffer buffer;
|
|
|
|
|
- JsonObject& request = buffer.parse(server.arg("plain"));
|
|
|
|
|
|
|
+ request.response.json["success"] = true;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if (!request.success()) {
|
|
|
|
|
- server.send_P(400, TEXT_PLAIN, PSTR("Invalid JSON"));
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+void MiLightHttpServer::handleUpdateGroup(RequestContext& request) {
|
|
|
|
|
+ JsonObject reqObj = request.getJsonBody().as<JsonObject>();
|
|
|
|
|
|
|
|
milightClient->setResendCount(
|
|
milightClient->setResendCount(
|
|
|
settings.httpRepeatFactor * settings.packetRepeats
|
|
settings.httpRepeatFactor * settings.packetRepeats
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
- String _deviceIds = urlBindings->get("device_id");
|
|
|
|
|
- String _groupIds = urlBindings->get("group_id");
|
|
|
|
|
- String _remoteTypes = urlBindings->get("type");
|
|
|
|
|
|
|
+ String _deviceIds = request.pathVariables.get("device_id");
|
|
|
|
|
+ String _groupIds = request.pathVariables.get("group_id");
|
|
|
|
|
+ String _remoteTypes = request.pathVariables.get("type");
|
|
|
char deviceIds[_deviceIds.length()];
|
|
char deviceIds[_deviceIds.length()];
|
|
|
char groupIds[_groupIds.length()];
|
|
char groupIds[_groupIds.length()];
|
|
|
char remoteTypes[_remoteTypes.length()];
|
|
char remoteTypes[_remoteTypes.length()];
|
|
@@ -403,7 +390,8 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
|
|
|
if (config == NULL) {
|
|
if (config == NULL) {
|
|
|
char buffer[40];
|
|
char buffer[40];
|
|
|
sprintf_P(buffer, PSTR("Unknown device type: %s"), _remoteType);
|
|
sprintf_P(buffer, PSTR("Unknown device type: %s"), _remoteType);
|
|
|
- server.send(400, "text/plain", buffer);
|
|
|
|
|
|
|
+ request.response.setCode(400);
|
|
|
|
|
+ request.response.json["error"] = buffer;
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -416,7 +404,7 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
|
|
|
const uint8_t groupId = atoi(groupIdItr.nextToken());
|
|
const uint8_t groupId = atoi(groupIdItr.nextToken());
|
|
|
|
|
|
|
|
milightClient->prepare(config, deviceId, groupId);
|
|
milightClient->prepare(config, deviceId, groupId);
|
|
|
- handleRequest(request);
|
|
|
|
|
|
|
+ handleRequest(reqObj);
|
|
|
foundBulbId = BulbId(deviceId, groupId, config->type);
|
|
foundBulbId = BulbId(deviceId, groupId, config->type);
|
|
|
groupCount++;
|
|
groupCount++;
|
|
|
}
|
|
}
|
|
@@ -424,9 +412,9 @@ void MiLightHttpServer::handleUpdateGroup(const UrlTokenBindings* urlBindings) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (groupCount == 1) {
|
|
if (groupCount == 1) {
|
|
|
- sendGroupState(foundBulbId, stateStore->get(foundBulbId));
|
|
|
|
|
|
|
+ sendGroupState(foundBulbId, stateStore->get(foundBulbId), request.response);
|
|
|
} else {
|
|
} else {
|
|
|
- server.send(200, APPLICATION_JSON, "true");
|
|
|
|
|
|
|
+ request.response.json["success"] = true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -434,25 +422,25 @@ void MiLightHttpServer::handleRequest(const JsonObject& request) {
|
|
|
milightClient->update(request);
|
|
milightClient->update(request);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-void MiLightHttpServer::handleSendRaw(const UrlTokenBindings* bindings) {
|
|
|
|
|
- DynamicJsonBuffer buffer;
|
|
|
|
|
- JsonObject& request = buffer.parse(server.arg("plain"));
|
|
|
|
|
- const MiLightRemoteConfig* config = MiLightRemoteConfig::fromType(bindings->get("type"));
|
|
|
|
|
|
|
+void MiLightHttpServer::handleSendRaw(RequestContext& request) {
|
|
|
|
|
+ JsonObject requestBody = request.getJsonBody().as<JsonObject>();
|
|
|
|
|
+ const MiLightRemoteConfig* config = MiLightRemoteConfig::fromType(request.pathVariables.get("type"));
|
|
|
|
|
|
|
|
if (config == NULL) {
|
|
if (config == NULL) {
|
|
|
char buffer[50];
|
|
char buffer[50];
|
|
|
- sprintf_P(buffer, PSTR("Unknown device type: %s"), bindings->get("type"));
|
|
|
|
|
- server.send(400, "text/plain", buffer);
|
|
|
|
|
|
|
+ sprintf_P(buffer, PSTR("Unknown device type: %s"), request.pathVariables.get("type"));
|
|
|
|
|
+ request.response.setCode(400);
|
|
|
|
|
+ request.response.json["error"] = buffer;
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
uint8_t packet[MILIGHT_MAX_PACKET_LENGTH];
|
|
uint8_t packet[MILIGHT_MAX_PACKET_LENGTH];
|
|
|
- const String& hexPacket = request["packet"];
|
|
|
|
|
|
|
+ const String& hexPacket = requestBody["packet"];
|
|
|
hexStrToBytes<uint8_t>(hexPacket.c_str(), hexPacket.length(), packet, MILIGHT_MAX_PACKET_LENGTH);
|
|
hexStrToBytes<uint8_t>(hexPacket.c_str(), hexPacket.length(), packet, MILIGHT_MAX_PACKET_LENGTH);
|
|
|
|
|
|
|
|
size_t numRepeats = MILIGHT_DEFAULT_RESEND_COUNT;
|
|
size_t numRepeats = MILIGHT_DEFAULT_RESEND_COUNT;
|
|
|
- if (request.containsKey("num_repeats")) {
|
|
|
|
|
- numRepeats = request["num_repeats"];
|
|
|
|
|
|
|
+ if (requestBody.containsKey("num_repeats")) {
|
|
|
|
|
+ numRepeats = requestBody["num_repeats"];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
milightClient->prepare(config, 0, 0);
|
|
milightClient->prepare(config, 0, 0);
|
|
@@ -461,7 +449,7 @@ void MiLightHttpServer::handleSendRaw(const UrlTokenBindings* bindings) {
|
|
|
milightClient->write(packet);
|
|
milightClient->write(packet);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- server.send_P(200, TEXT_PLAIN, PSTR("true"));
|
|
|
|
|
|
|
+ request.response.json["success"] = true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void MiLightHttpServer::handleWsEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
|
|
void MiLightHttpServer::handleWsEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
|
|
@@ -503,14 +491,12 @@ void MiLightHttpServer::handlePacketSent(uint8_t *packet, const MiLightRemoteCon
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-ESP8266WebServer::THandlerFunction MiLightHttpServer::handleServe_P(const char* data, size_t length) {
|
|
|
|
|
- return [this, data, length]() {
|
|
|
|
|
- server.sendHeader("Content-Encoding", "gzip");
|
|
|
|
|
- server.setContentLength(CONTENT_LENGTH_UNKNOWN);
|
|
|
|
|
- server.send(200, "text/html", "");
|
|
|
|
|
- server.sendContent_P(data, length);
|
|
|
|
|
- server.sendContent("");
|
|
|
|
|
- server.client().stop();
|
|
|
|
|
- };
|
|
|
|
|
|
|
+void MiLightHttpServer::handleServe_P(const char* data, size_t length) {
|
|
|
|
|
+ server.sendHeader("Content-Encoding", "gzip");
|
|
|
|
|
+ server.setContentLength(CONTENT_LENGTH_UNKNOWN);
|
|
|
|
|
+ server.send(200, "text/html", "");
|
|
|
|
|
+ server.sendContent_P(data, length);
|
|
|
|
|
+ server.sendContent("");
|
|
|
|
|
+ server.client().stop();
|
|
|
}
|
|
}
|
|
|
|
|
|