From 4350c1bc4915a79c14a9ba198327d3189c39ea7b Mon Sep 17 00:00:00 2001 From: Citrullin Date: Wed, 18 Aug 2021 16:11:12 +0200 Subject: [PATCH 1/5] Fix Action POST --- ESPWebThingAdapter.h | 36 +++++------------------------------- Thing.h | 6 ++---- 2 files changed, 7 insertions(+), 35 deletions(-) diff --git a/ESPWebThingAdapter.h b/ESPWebThingAdapter.h index 9cf9320..2103b66 100644 --- a/ESPWebThingAdapter.h +++ b/ESPWebThingAdapter.h @@ -339,6 +339,7 @@ class WebThingAdapter { void handleThingActionPost(AsyncWebServerRequest *request, ThingDevice *device, ThingAction *action) { if (!verifyHost(request)) { + Serial.println("Invalid Host"); return; } @@ -351,6 +352,7 @@ class WebThingAdapter { new DynamicJsonDocument(SMALL_JSON_DOCUMENT_SIZE); auto error = deserializeJson(*newBuffer, (const char *)body_data); if (error) { // unable to parse json + Serial.println("Unable to parse JSON"); b_has_body_data = false; memset(body_data, 0, sizeof(body_data)); request->send(500); @@ -360,22 +362,10 @@ class WebThingAdapter { JsonObject newAction = newBuffer->as(); - if (!newAction.containsKey(action->id)) { - b_has_body_data = false; - memset(body_data, 0, sizeof(body_data)); - request->send(400); - delete newBuffer; - return; - } - - ThingActionObject *obj = device->requestAction(newBuffer); - + ThingActionObject *obj = action->create(newBuffer); if (obj == nullptr) { - b_has_body_data = false; - memset(body_data, 0, sizeof(body_data)); - request->send(500); - delete newBuffer; - return; + request->send(404); + return; } DynamicJsonDocument respBuffer(SMALL_JSON_DOCUMENT_SIZE); @@ -466,24 +456,8 @@ class WebThingAdapter { JsonObject newAction = newBuffer->as(); - if (newAction.size() != 1) { - b_has_body_data = false; - memset(body_data, 0, sizeof(body_data)); - request->send(400); - delete newBuffer; - return; - } - ThingActionObject *obj = device->requestAction(newBuffer); - if (obj == nullptr) { - b_has_body_data = false; - memset(body_data, 0, sizeof(body_data)); - request->send(500); - delete newBuffer; - return; - } - DynamicJsonDocument respBuffer(SMALL_JSON_DOCUMENT_SIZE); JsonObject item = respBuffer.to(); obj->serialize(item, device->id); diff --git a/Thing.h b/Thing.h index 1e66e77..54ce5af 100644 --- a/Thing.h +++ b/Thing.h @@ -89,8 +89,7 @@ class ThingActionObject { JsonObject data = obj.createNestedObject(name); JsonObject actionObj = actionRequest->as(); - JsonObject inner = actionObj[name]; - data["input"] = inner["input"]; + data["input"] = actionObj; data["status"] = status; data["timeRequested"] = timeRequested; @@ -111,8 +110,7 @@ class ThingActionObject { setStatus("pending"); JsonObject actionObj = actionRequest->as(); - JsonObject inner = actionObj[name]; - start_fn(inner["input"]); + start_fn(actionObj); finish(); } From 98320fee07e4f680111740ab4afafeb3201b437c Mon Sep 17 00:00:00 2001 From: Citrullin Date: Wed, 18 Aug 2021 18:58:38 +0200 Subject: [PATCH 2/5] Add 500 Error when not able to create ThinActionObject --- ESPWebThingAdapter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ESPWebThingAdapter.h b/ESPWebThingAdapter.h index 2103b66..f9ab9c3 100644 --- a/ESPWebThingAdapter.h +++ b/ESPWebThingAdapter.h @@ -364,7 +364,7 @@ class WebThingAdapter { ThingActionObject *obj = action->create(newBuffer); if (obj == nullptr) { - request->send(404); + request->send(500); return; } From 442bc97c27a3d3dd90027dbae69bfd61cb0be283 Mon Sep 17 00:00:00 2001 From: Citrullin Date: Wed, 18 Aug 2021 19:40:18 +0200 Subject: [PATCH 3/5] Add #ifdef ESP32 for WebThings, instead of ESPWebThing --- ESPWebThingAdapter.h | 3 +-- WebThingAdapter.h | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ESPWebThingAdapter.h b/ESPWebThingAdapter.h index f9ab9c3..79abf37 100644 --- a/ESPWebThingAdapter.h +++ b/ESPWebThingAdapter.h @@ -11,7 +11,7 @@ #pragma once -#if defined(ESP32) || defined(ESP8266) + #include #include @@ -538,4 +538,3 @@ class WebThingAdapter { } }; -#endif // ESP diff --git a/WebThingAdapter.h b/WebThingAdapter.h index cb76ac8..40a9ae7 100644 --- a/WebThingAdapter.h +++ b/WebThingAdapter.h @@ -10,4 +10,6 @@ #pragma once +#if defined(ESP32) || defined(ESP8266) #include "ESPWebThingAdapter.h" +#endif From 23ccab0a89599758dc28695ae727347085f0ed24 Mon Sep 17 00:00:00 2001 From: Citrullin Date: Wed, 18 Aug 2021 19:58:14 +0200 Subject: [PATCH 4/5] Remove all generic /actions/* endpoints --- ESPWebThingAdapter.h | 68 +------------------------------------------- 1 file changed, 1 insertion(+), 67 deletions(-) diff --git a/ESPWebThingAdapter.h b/ESPWebThingAdapter.h index 79abf37..16ed4ec 100644 --- a/ESPWebThingAdapter.h +++ b/ESPWebThingAdapter.h @@ -131,17 +131,7 @@ class WebThingAdapter { std::bind(&WebThingAdapter::handleThingPropertiesGet, this, std::placeholders::_1, device->firstProperty)); - this->server.on((deviceBase + "/actions").c_str(), HTTP_GET, - std::bind(&WebThingAdapter::handleThingActionsGet, this, - std::placeholders::_1, device)); - this->server.on((deviceBase + "/actions").c_str(), HTTP_POST, - std::bind(&WebThingAdapter::handleThingActionsPost, this, - std::placeholders::_1, device), - NULL, - std::bind(&WebThingAdapter::handleBody, this, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3, std::placeholders::_4, - std::placeholders::_5)); + this->server.on((deviceBase + "/events").c_str(), HTTP_GET, std::bind(&WebThingAdapter::handleThingEventsGet, this, std::placeholders::_1, device)); @@ -417,62 +407,6 @@ class WebThingAdapter { request->send(response); } - void handleThingActionsGet(AsyncWebServerRequest *request, - ThingDevice *device) { - if (!verifyHost(request)) { - return; - } - AsyncResponseStream *response = - request->beginResponseStream("application/json"); - - DynamicJsonDocument doc(LARGE_JSON_DOCUMENT_SIZE); - JsonArray queue = doc.to(); - device->serializeActionQueue(queue); - serializeJson(queue, *response); - request->send(response); - } - - void handleThingActionsPost(AsyncWebServerRequest *request, - ThingDevice *device) { - if (!verifyHost(request)) { - return; - } - - if (!b_has_body_data) { - request->send(422); // unprocessable entity (b/c no body) - return; - } - - DynamicJsonDocument *newBuffer = - new DynamicJsonDocument(SMALL_JSON_DOCUMENT_SIZE); - auto error = deserializeJson(*newBuffer, (const char *)body_data); - if (error) { // unable to parse json - b_has_body_data = false; - memset(body_data, 0, sizeof(body_data)); - request->send(500); - delete newBuffer; - return; - } - - JsonObject newAction = newBuffer->as(); - - ThingActionObject *obj = device->requestAction(newBuffer); - - DynamicJsonDocument respBuffer(SMALL_JSON_DOCUMENT_SIZE); - JsonObject item = respBuffer.to(); - obj->serialize(item, device->id); - String jsonStr; - serializeJson(item, jsonStr); - AsyncWebServerResponse *response = - request->beginResponse(201, "application/json", jsonStr); - request->send(response); - - b_has_body_data = false; - memset(body_data, 0, sizeof(body_data)); - - obj->start(); - } - void handleThingEventsGet(AsyncWebServerRequest *request, ThingDevice *device) { if (!verifyHost(request)) { From 8c2704b60df0833e23f387b187f9bae44600e0c9 Mon Sep 17 00:00:00 2001 From: Citrullin Date: Thu, 19 Aug 2021 02:45:29 +0200 Subject: [PATCH 5/5] clean memory on 500 error and remove unnecessary code --- ESPWebThingAdapter.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ESPWebThingAdapter.h b/ESPWebThingAdapter.h index 16ed4ec..6f910a5 100644 --- a/ESPWebThingAdapter.h +++ b/ESPWebThingAdapter.h @@ -350,10 +350,9 @@ class WebThingAdapter { return; } - JsonObject newAction = newBuffer->as(); - ThingActionObject *obj = action->create(newBuffer); if (obj == nullptr) { + memset(body_data, 0, sizeof(body_data)); request->send(500); return; }