change POST endpoint to accept topic and payload

This commit is contained in:
2018-09-20 21:16:44 +02:00
parent 1a3fcbbde5
commit 54b0801784

View File

@@ -97,7 +97,7 @@ class IlluCat : public MeshSprocket {
// TODO plugin
// setup web stuff
server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json");
server->on("/pixel/pattern", HTTP_POST, bind(&IlluCat::patternWebRequestHandler, this, _1));
server->on("/pixel/state", HTTP_POST, bind(&IlluCat::patternWebRequestHandler, this, _1));
ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6));
server->addHandler(ws);
@@ -107,13 +107,22 @@ class IlluCat : public MeshSprocket {
return MeshSprocket::activate(scheduler, network);
} using MeshSprocket::activate;
// TODO move to utils
String getRequestParameterOrDefault(AsyncWebServerRequest *request, String param, String defaultValue, bool isPost = true){
if(request->hasParam(param, isPost)) {
return request->getParam(param, isPost)->value();
}
return defaultValue;
}
void patternWebRequestHandler(AsyncWebServerRequest *request) {
Serial.println("POST /pixel/state");
if(request->hasParam("state", true)) {
String inStr = request->getParam("state", true)->value();
dispatch(0, inStr);
}
request->send(200, "text/plain", "OK");
currentMessage.topic = getRequestParameterOrDefault(request, "topic", "");
currentMessage.payload = getRequestParameterOrDefault(request, "payload", "");
String msg = currentMessage.toJsonString();
net->mesh.sendBroadcast(msg);
publish(currentMessage.topic, currentMessage.payload);
request->send(200, "text/plain", msg);
}
virtual void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {