mirror of
https://gitlab.com/zwirbel/illucat.git
synced 2025-12-16 10:04:30 +01:00
change POST endpoint to accept topic and payload
This commit is contained in:
@@ -97,7 +97,7 @@ class IlluCat : public MeshSprocket {
|
|||||||
// TODO plugin
|
// TODO plugin
|
||||||
// setup web stuff
|
// setup web stuff
|
||||||
server->serveStatic("/pixelConfig.json", SPIFFS, "pixelConfig.json");
|
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));
|
ws->onEvent(bind(&IlluCat::onWsEvent, this, _1, _2, _3, _4, _5, _6));
|
||||||
server->addHandler(ws);
|
server->addHandler(ws);
|
||||||
|
|
||||||
@@ -107,13 +107,22 @@ class IlluCat : public MeshSprocket {
|
|||||||
return MeshSprocket::activate(scheduler, network);
|
return MeshSprocket::activate(scheduler, network);
|
||||||
} using MeshSprocket::activate;
|
} 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) {
|
void patternWebRequestHandler(AsyncWebServerRequest *request) {
|
||||||
Serial.println("POST /pixel/state");
|
Serial.println("POST /pixel/state");
|
||||||
if(request->hasParam("state", true)) {
|
currentMessage.topic = getRequestParameterOrDefault(request, "topic", "");
|
||||||
String inStr = request->getParam("state", true)->value();
|
currentMessage.payload = getRequestParameterOrDefault(request, "payload", "");
|
||||||
dispatch(0, inStr);
|
String msg = currentMessage.toJsonString();
|
||||||
}
|
net->mesh.sendBroadcast(msg);
|
||||||
request->send(200, "text/plain", "OK");
|
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) {
|
virtual void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
|
||||||
|
|||||||
Reference in New Issue
Block a user