Skip to content

Commit f4596c7

Browse files
authored
Merge pull request #7 from Citrullin/fix-action-post
Fix action POST
2 parents 2646d73 + 8c2704b commit f4596c7

File tree

3 files changed

+12
-106
lines changed

3 files changed

+12
-106
lines changed

ESPWebThingAdapter.h

+8-102
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#pragma once
1313

14-
#if defined(ESP32) || defined(ESP8266)
14+
1515

1616
#include <ArduinoJson.h>
1717
#include <ESPAsyncWebServer.h>
@@ -131,17 +131,7 @@ class WebThingAdapter {
131131
std::bind(&WebThingAdapter::handleThingPropertiesGet,
132132
this, std::placeholders::_1,
133133
device->firstProperty));
134-
this->server.on((deviceBase + "/actions").c_str(), HTTP_GET,
135-
std::bind(&WebThingAdapter::handleThingActionsGet, this,
136-
std::placeholders::_1, device));
137-
this->server.on((deviceBase + "/actions").c_str(), HTTP_POST,
138-
std::bind(&WebThingAdapter::handleThingActionsPost, this,
139-
std::placeholders::_1, device),
140-
NULL,
141-
std::bind(&WebThingAdapter::handleBody, this,
142-
std::placeholders::_1, std::placeholders::_2,
143-
std::placeholders::_3, std::placeholders::_4,
144-
std::placeholders::_5));
134+
145135
this->server.on((deviceBase + "/events").c_str(), HTTP_GET,
146136
std::bind(&WebThingAdapter::handleThingEventsGet, this,
147137
std::placeholders::_1, device));
@@ -339,6 +329,7 @@ class WebThingAdapter {
339329
void handleThingActionPost(AsyncWebServerRequest *request,
340330
ThingDevice *device, ThingAction *action) {
341331
if (!verifyHost(request)) {
332+
Serial.println("Invalid Host");
342333
return;
343334
}
344335

@@ -351,31 +342,19 @@ class WebThingAdapter {
351342
new DynamicJsonDocument(SMALL_JSON_DOCUMENT_SIZE);
352343
auto error = deserializeJson(*newBuffer, (const char *)body_data);
353344
if (error) { // unable to parse json
345+
Serial.println("Unable to parse JSON");
354346
b_has_body_data = false;
355347
memset(body_data, 0, sizeof(body_data));
356348
request->send(500);
357349
delete newBuffer;
358350
return;
359351
}
360352

361-
JsonObject newAction = newBuffer->as<JsonObject>();
362-
363-
if (!newAction.containsKey(action->id)) {
364-
b_has_body_data = false;
365-
memset(body_data, 0, sizeof(body_data));
366-
request->send(400);
367-
delete newBuffer;
368-
return;
369-
}
370-
371-
ThingActionObject *obj = device->requestAction(newBuffer);
372-
353+
ThingActionObject *obj = action->create(newBuffer);
373354
if (obj == nullptr) {
374-
b_has_body_data = false;
375-
memset(body_data, 0, sizeof(body_data));
376-
request->send(500);
377-
delete newBuffer;
378-
return;
355+
memset(body_data, 0, sizeof(body_data));
356+
request->send(500);
357+
return;
379358
}
380359

381360
DynamicJsonDocument respBuffer(SMALL_JSON_DOCUMENT_SIZE);
@@ -427,78 +406,6 @@ class WebThingAdapter {
427406
request->send(response);
428407
}
429408

430-
void handleThingActionsGet(AsyncWebServerRequest *request,
431-
ThingDevice *device) {
432-
if (!verifyHost(request)) {
433-
return;
434-
}
435-
AsyncResponseStream *response =
436-
request->beginResponseStream("application/json");
437-
438-
DynamicJsonDocument doc(LARGE_JSON_DOCUMENT_SIZE);
439-
JsonArray queue = doc.to<JsonArray>();
440-
device->serializeActionQueue(queue);
441-
serializeJson(queue, *response);
442-
request->send(response);
443-
}
444-
445-
void handleThingActionsPost(AsyncWebServerRequest *request,
446-
ThingDevice *device) {
447-
if (!verifyHost(request)) {
448-
return;
449-
}
450-
451-
if (!b_has_body_data) {
452-
request->send(422); // unprocessable entity (b/c no body)
453-
return;
454-
}
455-
456-
DynamicJsonDocument *newBuffer =
457-
new DynamicJsonDocument(SMALL_JSON_DOCUMENT_SIZE);
458-
auto error = deserializeJson(*newBuffer, (const char *)body_data);
459-
if (error) { // unable to parse json
460-
b_has_body_data = false;
461-
memset(body_data, 0, sizeof(body_data));
462-
request->send(500);
463-
delete newBuffer;
464-
return;
465-
}
466-
467-
JsonObject newAction = newBuffer->as<JsonObject>();
468-
469-
if (newAction.size() != 1) {
470-
b_has_body_data = false;
471-
memset(body_data, 0, sizeof(body_data));
472-
request->send(400);
473-
delete newBuffer;
474-
return;
475-
}
476-
477-
ThingActionObject *obj = device->requestAction(newBuffer);
478-
479-
if (obj == nullptr) {
480-
b_has_body_data = false;
481-
memset(body_data, 0, sizeof(body_data));
482-
request->send(500);
483-
delete newBuffer;
484-
return;
485-
}
486-
487-
DynamicJsonDocument respBuffer(SMALL_JSON_DOCUMENT_SIZE);
488-
JsonObject item = respBuffer.to<JsonObject>();
489-
obj->serialize(item, device->id);
490-
String jsonStr;
491-
serializeJson(item, jsonStr);
492-
AsyncWebServerResponse *response =
493-
request->beginResponse(201, "application/json", jsonStr);
494-
request->send(response);
495-
496-
b_has_body_data = false;
497-
memset(body_data, 0, sizeof(body_data));
498-
499-
obj->start();
500-
}
501-
502409
void handleThingEventsGet(AsyncWebServerRequest *request,
503410
ThingDevice *device) {
504411
if (!verifyHost(request)) {
@@ -564,4 +471,3 @@ class WebThingAdapter {
564471
}
565472
};
566473

567-
#endif // ESP

Thing.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ class ThingActionObject {
8989
JsonObject data = obj.createNestedObject(name);
9090

9191
JsonObject actionObj = actionRequest->as<JsonObject>();
92-
JsonObject inner = actionObj[name];
93-
data["input"] = inner["input"];
92+
data["input"] = actionObj;
9493

9594
data["status"] = status;
9695
data["timeRequested"] = timeRequested;
@@ -111,8 +110,7 @@ class ThingActionObject {
111110
setStatus("pending");
112111

113112
JsonObject actionObj = actionRequest->as<JsonObject>();
114-
JsonObject inner = actionObj[name];
115-
start_fn(inner["input"]);
113+
start_fn(actionObj);
116114

117115
finish();
118116
}

WebThingAdapter.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010

1111
#pragma once
1212

13+
#if defined(ESP32) || defined(ESP8266)
1314
#include "ESPWebThingAdapter.h"
15+
#endif

0 commit comments

Comments
 (0)