@@ -55,7 +55,7 @@ class WebThingAdapter {
55
55
}
56
56
57
57
MDNS.addService (" webthing" , " tcp" , port);
58
- MDNS.addServiceTxt (" webthing" , " tcp" , " path" , " /" );
58
+ MDNS.addServiceTxt (" webthing" , " tcp" , " path" , " /.well-known/wot-thing-description " );
59
59
60
60
DefaultHeaders::Instance ().addHeader (" Access-Control-Allow-Origin" , " *" );
61
61
DefaultHeaders::Instance ().addHeader (" Access-Control-Allow-Methods" ,
@@ -70,7 +70,7 @@ class WebThingAdapter {
70
70
this ->server .on (" /*" , HTTP_OPTIONS,
71
71
std::bind (&WebThingAdapter::handleOptions, this ,
72
72
std::placeholders::_1));
73
- this ->server .on (" /" , HTTP_GET,
73
+ this ->server .on (" /.well-known/wot-thing-description " , HTTP_GET,
74
74
std::bind (&WebThingAdapter::handleThings, this ,
75
75
std::placeholders::_1));
76
76
@@ -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));
@@ -362,16 +352,15 @@ class WebThingAdapter {
362
352
request->beginResponseStream (" application/json" );
363
353
364
354
DynamicJsonDocument buf (LARGE_JSON_DOCUMENT_SIZE);
365
- JsonArray things = buf.to <JsonArray >();
355
+ JsonObject thing = buf.to <JsonObject >();
366
356
ThingDevice *device = this ->firstDevice ;
367
357
while (device != nullptr ) {
368
- JsonObject descr = things.createNestedObject ();
369
- device->serialize (descr, ip, port);
370
- descr[" href" ] = " /things/" + device->id ;
358
+ device->serialize (thing, ip, port);
359
+ thing[" href" ] = " /things/" + device->id ;
371
360
device = device->next ;
372
361
}
373
362
374
- serializeJson (things , *response);
363
+ serializeJson (thing , *response);
375
364
request->send (response);
376
365
}
377
366
@@ -474,6 +463,7 @@ class WebThingAdapter {
474
463
void handleThingActionPost (AsyncWebServerRequest *request,
475
464
ThingDevice *device, ThingAction *action) {
476
465
if (!verifyHost (request)) {
466
+ Serial.println (" Invalid Host" );
477
467
return ;
478
468
}
479
469
@@ -486,31 +476,19 @@ class WebThingAdapter {
486
476
new DynamicJsonDocument (SMALL_JSON_DOCUMENT_SIZE);
487
477
auto error = deserializeJson (*newBuffer, (const char *)body_data);
488
478
if (error) { // unable to parse json
479
+ Serial.println (" Unable to parse JSON" );
489
480
b_has_body_data = false ;
490
481
memset (body_data, 0 , sizeof (body_data));
491
482
request->send (500 );
492
483
delete newBuffer;
493
484
return ;
494
485
}
495
486
496
- JsonObject newAction = newBuffer->as <JsonObject>();
497
-
498
- if (!newAction.containsKey (action->id )) {
499
- b_has_body_data = false ;
500
- memset (body_data, 0 , sizeof (body_data));
501
- request->send (400 );
502
- delete newBuffer;
503
- return ;
504
- }
505
-
506
- ThingActionObject *obj = device->requestAction (newBuffer);
507
-
487
+ ThingActionObject *obj = action->create (newBuffer);
508
488
if (obj == nullptr ) {
509
- b_has_body_data = false ;
510
- memset (body_data, 0 , sizeof (body_data));
511
- request->send (500 );
512
- delete newBuffer;
513
- return ;
489
+ memset (body_data, 0 , sizeof (body_data));
490
+ request->send (500 );
491
+ return ;
514
492
}
515
493
516
494
#ifndef WITHOUT_WS
@@ -567,84 +545,7 @@ class WebThingAdapter {
567
545
request->send (response);
568
546
}
569
547
570
- void handleThingActionsGet (AsyncWebServerRequest *request,
571
- ThingDevice *device) {
572
- if (!verifyHost (request)) {
573
- return ;
574
- }
575
- AsyncResponseStream *response =
576
- request->beginResponseStream (" application/json" );
577
-
578
- DynamicJsonDocument doc (LARGE_JSON_DOCUMENT_SIZE);
579
- JsonArray queue = doc.to <JsonArray>();
580
- device->serializeActionQueue (queue);
581
- serializeJson (queue, *response);
582
- request->send (response);
583
- }
584
-
585
- void handleThingActionsPost (AsyncWebServerRequest *request,
586
- ThingDevice *device) {
587
- if (!verifyHost (request)) {
588
- return ;
589
- }
590
-
591
- if (!b_has_body_data) {
592
- request->send (422 ); // unprocessable entity (b/c no body)
593
- return ;
594
- }
595
-
596
- DynamicJsonDocument *newBuffer =
597
- new DynamicJsonDocument (SMALL_JSON_DOCUMENT_SIZE);
598
- auto error = deserializeJson (*newBuffer, (const char *)body_data);
599
- if (error) { // unable to parse json
600
- b_has_body_data = false ;
601
- memset (body_data, 0 , sizeof (body_data));
602
- request->send (500 );
603
- delete newBuffer;
604
- return ;
605
- }
606
-
607
- JsonObject newAction = newBuffer->as <JsonObject>();
608
-
609
- if (newAction.size () != 1 ) {
610
- b_has_body_data = false ;
611
- memset (body_data, 0 , sizeof (body_data));
612
- request->send (400 );
613
- delete newBuffer;
614
- return ;
615
- }
616
-
617
- ThingActionObject *obj = device->requestAction (newBuffer);
618
-
619
- if (obj == nullptr ) {
620
- b_has_body_data = false ;
621
- memset (body_data, 0 , sizeof (body_data));
622
- request->send (500 );
623
- delete newBuffer;
624
- return ;
625
- }
626
-
627
- #ifndef WITHOUT_WS
628
- obj->setNotifyFunction (std::bind (&ThingDevice::sendActionStatus, device,
629
- std::placeholders::_1));
630
- #endif
631
-
632
- DynamicJsonDocument respBuffer (SMALL_JSON_DOCUMENT_SIZE);
633
- JsonObject item = respBuffer.to <JsonObject>();
634
- obj->serialize (item, device->id );
635
- String jsonStr;
636
- serializeJson (item, jsonStr);
637
- AsyncWebServerResponse *response =
638
- request->beginResponse (201 , " application/json" , jsonStr);
639
- request->send (response);
640
-
641
- b_has_body_data = false ;
642
- memset (body_data, 0 , sizeof (body_data));
643
-
644
- obj->start ();
645
- }
646
-
647
- void handleThingEventsGet (AsyncWebServerRequest *request,
548
+ void handleThingEventsGet (AsyncWebServerRequest *request,
648
549
ThingDevice *device) {
649
550
if (!verifyHost (request)) {
650
551
return ;
0 commit comments