Skip to content

Commit

Permalink
Merge pull request #7 from Citrullin/fix-action-post
Browse files Browse the repository at this point in the history
Fix action POST
  • Loading branch information
relu91 authored Aug 23, 2021
2 parents 2646d73 + 8c2704b commit f4596c7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 106 deletions.
110 changes: 8 additions & 102 deletions ESPWebThingAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#pragma once

#if defined(ESP32) || defined(ESP8266)


#include <ArduinoJson.h>
#include <ESPAsyncWebServer.h>
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -339,6 +329,7 @@ class WebThingAdapter {
void handleThingActionPost(AsyncWebServerRequest *request,
ThingDevice *device, ThingAction *action) {
if (!verifyHost(request)) {
Serial.println("Invalid Host");
return;
}

Expand All @@ -351,31 +342,19 @@ 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);
delete newBuffer;
return;
}

JsonObject newAction = newBuffer->as<JsonObject>();

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;
memset(body_data, 0, sizeof(body_data));
request->send(500);
return;
}

DynamicJsonDocument respBuffer(SMALL_JSON_DOCUMENT_SIZE);
Expand Down Expand Up @@ -427,78 +406,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<JsonArray>();
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<JsonObject>();

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<JsonObject>();
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)) {
Expand Down Expand Up @@ -564,4 +471,3 @@ class WebThingAdapter {
}
};

#endif // ESP
6 changes: 2 additions & 4 deletions Thing.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ class ThingActionObject {
JsonObject data = obj.createNestedObject(name);

JsonObject actionObj = actionRequest->as<JsonObject>();
JsonObject inner = actionObj[name];
data["input"] = inner["input"];
data["input"] = actionObj;

data["status"] = status;
data["timeRequested"] = timeRequested;
Expand All @@ -111,8 +110,7 @@ class ThingActionObject {
setStatus("pending");

JsonObject actionObj = actionRequest->as<JsonObject>();
JsonObject inner = actionObj[name];
start_fn(inner["input"]);
start_fn(actionObj);

finish();
}
Expand Down
2 changes: 2 additions & 0 deletions WebThingAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

#pragma once

#if defined(ESP32) || defined(ESP8266)
#include "ESPWebThingAdapter.h"
#endif

0 comments on commit f4596c7

Please sign in to comment.