forked from WebThingsIO/webthing-arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESPWebThingAdapter.h
273 lines (228 loc) · 7.62 KB
/
ESPWebThingAdapter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* ESPWebThingAdapter.h
*
* Exposes the Web Thing API based on provided ThingDevices.
* Suitable for ESP32 and ESP8266 using ESPAsyncWebServer and ESPAsyncTCP
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef MOZILLA_IOT_ESPWEBTHINGADAPTER_H
#define MOZILLA_IOT_ESPWEBTHINGADAPTER_H
#if defined(ESP32) || defined(ESP8266)
#include <ArduinoJson.h>
#include <ESPAsyncWebServer.h>
#ifdef ESP8266
#include <ESP8266mDNS.h>
#else
#include <ESPmDNS.h>
#endif
#include "Thing.h"
#define ESP_MAX_PUT_BODY_SIZE 512
class WebThingAdapter {
public:
WebThingAdapter(String _name, IPAddress _ip): name(_name), server(80), ip(_ip.toString()) {
}
void begin() {
if (MDNS.begin(this->name.c_str())) {
Serial.println("MDNS responder started");
}
MDNS.addService("webthing", "tcp", 80);
MDNS.addServiceTxt("webthing", "tcp", "path", "/");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Methods", "PUT, GET, OPTIONS");
this->server.onNotFound(std::bind(&WebThingAdapter::handleUnknown, this, std::placeholders::_1));
this->server.on("/", HTTP_GET, std::bind(&WebThingAdapter::handleThings, this, std::placeholders::_1));
ThingDevice* device = this->firstDevice;
while (device != nullptr) {
String deviceBase = "/things/" + device->id;
ThingProperty* property = device->firstProperty;
while (property != nullptr) {
String propertyBase = deviceBase + "/properties/" + property->id;
this->server.on(propertyBase.c_str(), HTTP_GET, std::bind(&WebThingAdapter::handleThingPropertyGet, this, std::placeholders::_1, property));
this->server.on(propertyBase.c_str(), HTTP_PUT,
std::bind(&WebThingAdapter::handleThingPropertyPut, this, std::placeholders::_1, property),
NULL,
std::bind(&WebThingAdapter::handleBody, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)
);
property = property->next;
}
this->server.on(deviceBase.c_str(), HTTP_GET, std::bind(&WebThingAdapter::handleThing, this, std::placeholders::_1, device));
device = device->next;
}
this->server.begin();
}
void update() {
// non implemented, async web-server
}
void addDevice(ThingDevice* device) {
if (this->lastDevice == nullptr) {
this->firstDevice = device;
this->lastDevice = device;
} else {
this->lastDevice->next = device;
this->lastDevice = device;
}
}
private:
AsyncWebServer server;
String name;
String ip;
ThingDevice* firstDevice = nullptr;
ThingDevice* lastDevice = nullptr;
char body_data[ESP_MAX_PUT_BODY_SIZE];
bool b_has_body_data = false;
bool verifyHost(AsyncWebServerRequest *request) {
AsyncWebHeader* header = request->getHeader("Host");
if (header == nullptr) {
request->send(403);
return false;
}
String value = header->value();
int colonIndex = value.indexOf(':');
if (colonIndex >= 0) {
value.remove(colonIndex);
}
if (value == name + ".local" || value == ip) {
return true;
}
request->send(403);
return false;
}
void handleUnknown(AsyncWebServerRequest *request) {
if (!verifyHost(request)) {
return;
}
request->send(404);
}
void handleThings(AsyncWebServerRequest *request) {
if (!verifyHost(request)) {
return;
}
AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonBuffer buf(4096);
JsonArray& things = buf.createArray();
ThingDevice* device = this->firstDevice;
while (device != nullptr) {
JsonObject& descr = things.createNestedObject();
this->serializeDevice(descr, device);
device = device->next;
}
things.printTo(*response);
request->send(response);
}
void serializeDevice(JsonObject& descr, ThingDevice* device) {
descr["name"] = device->name;
descr["href"] = "/things/" + device->id;
descr["@context"] = "https://iot.mozilla.org/schemas";
JsonArray& typeJson = descr.createNestedArray("@type");
const char** type = device->type;
while ((*type) != nullptr) {
typeJson.add(*type);
type++;
}
JsonObject& props = descr.createNestedObject("properties");
ThingProperty* property = device->firstProperty;
while (property != nullptr) {
JsonObject& prop = props.createNestedObject(property->id);
switch (property->type) {
case BOOLEAN:
prop["type"] = "boolean";
break;
case NUMBER:
prop["type"] = "number";
break;
case STRING:
prop["type"] = "string";
break;
}
if (property->atType != nullptr) {
prop["@type"] = property->atType;
}
prop["href"] = "/things/" + device->id + "/properties/" + property->id;
property = property->next;
}
}
void handleThing(AsyncWebServerRequest *request, ThingDevice*& device) {
if (!verifyHost(request)) {
return;
}
AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonBuffer buf(1024);
JsonObject& descr = buf.createObject();
this->serializeDevice(descr, device);
descr.printTo(*response);
request->send(response);
}
void handleThingPropertyGet(AsyncWebServerRequest *request, ThingProperty* property) {
if (!verifyHost(request)) {
return;
}
AsyncResponseStream *response = request->beginResponseStream("application/json");
DynamicJsonBuffer buf(256);
JsonObject& prop = buf.createObject();
switch (property->type) {
case BOOLEAN:
prop[property->id] = property->getValue().boolean;
break;
case NUMBER:
prop[property->id] = property->getValue().number;
break;
case STRING:
prop[property->id] = *property->getValue().string;
break;
}
prop.printTo(*response);
request->send(response);
}
void handleBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
if ( total >= ESP_MAX_PUT_BODY_SIZE || index+len >= ESP_MAX_PUT_BODY_SIZE) {
return; // cannot store this size..
}
// copy to internal buffer
memcpy(&body_data[index], data, len);
b_has_body_data = true;
}
void handleThingPropertyPut(AsyncWebServerRequest *request, ThingProperty* property) {
if (!verifyHost(request)) {
return;
}
if (!b_has_body_data) {
request->send(422); // unprocessable entity (b/c no body)
return;
}
DynamicJsonBuffer newBuffer(256);
JsonObject& newProp = newBuffer.parseObject(body_data);
if (newProp.success()) {
JsonVariant newValue = newProp[property->id];
switch (property->type) {
case BOOLEAN: {
ThingPropertyValue value;
value.boolean = newValue.as<bool>();
property->setValue(value);
break;
}
case NUMBER: {
ThingPropertyValue value;
value.number = newValue.as<double>();
property->setValue(value);
break;
}
case STRING:
*property->getValue().string = newValue.as<String>();
break;
}
AsyncResponseStream *response = request->beginResponseStream("application/json");
newProp.printTo(*response);
request->send(response);
} else {
// unable to parse json
request->send(500);
}
b_has_body_data = false;
memset(body_data,0,sizeof(body_data));
}
};
#endif // ESP
#endif