11
11
12
12
#pragma once
13
13
14
- # if defined(ESP32) || defined(ESP8266)
14
+
15
15
16
16
#include < ArduinoJson.h>
17
17
#include < ESPAsyncWebServer.h>
@@ -131,17 +131,7 @@ class WebThingAdapter {
131
131
std::bind (&WebThingAdapter::handleThingPropertiesGet,
132
132
this , std::placeholders::_1,
133
133
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
+
145
135
this ->server .on ((deviceBase + " /events" ).c_str (), HTTP_GET,
146
136
std::bind (&WebThingAdapter::handleThingEventsGet, this ,
147
137
std::placeholders::_1, device));
@@ -339,6 +329,7 @@ class WebThingAdapter {
339
329
void handleThingActionPost (AsyncWebServerRequest *request,
340
330
ThingDevice *device, ThingAction *action) {
341
331
if (!verifyHost (request)) {
332
+ Serial.println (" Invalid Host" );
342
333
return ;
343
334
}
344
335
@@ -351,31 +342,19 @@ class WebThingAdapter {
351
342
new DynamicJsonDocument (SMALL_JSON_DOCUMENT_SIZE);
352
343
auto error = deserializeJson (*newBuffer, (const char *)body_data);
353
344
if (error) { // unable to parse json
345
+ Serial.println (" Unable to parse JSON" );
354
346
b_has_body_data = false ;
355
347
memset (body_data, 0 , sizeof (body_data));
356
348
request->send (500 );
357
349
delete newBuffer;
358
350
return ;
359
351
}
360
352
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);
373
354
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 ;
379
358
}
380
359
381
360
DynamicJsonDocument respBuffer (SMALL_JSON_DOCUMENT_SIZE);
@@ -427,78 +406,6 @@ class WebThingAdapter {
427
406
request->send (response);
428
407
}
429
408
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
-
502
409
void handleThingEventsGet (AsyncWebServerRequest *request,
503
410
ThingDevice *device) {
504
411
if (!verifyHost (request)) {
@@ -564,4 +471,3 @@ class WebThingAdapter {
564
471
}
565
472
};
566
473
567
- #endif // ESP
0 commit comments