-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembeddedWoT_WebSocket.cpp
More file actions
466 lines (426 loc) · 17.7 KB
/
embeddedWoT_WebSocket.cpp
File metadata and controls
466 lines (426 loc) · 17.7 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include "Arduino.h"
#include <WebSocketsServer.h>
#include <ArduinoJson.h>
#include "embeddedWoT_WebSocket.h"
embeddedWoT_WebSocket::embeddedWoT_WebSocket(int portSocket): ac_doc(2000), ia_doc(1000), ipia_doc(2000), e_doc(1000), es_doc(20), ipe_doc(2000), webSocket(portSocket) {
// events data
ipe_arr = ipe_doc.createNestedArray("clients_list");
ipia_arr = ipia_doc.createNestedArray("clients_list");
//handling callback
webSocket.onEvent([this] (uint8_t num, WStype_t type, uint8_t* pl, size_t length) {
// this->test();
switch(type) {
case WStype_DISCONNECTED: {
this->_clientDisconnect(num, pl);
break;
}
case WStype_CONNECTED: {
this->_clientConnect(num, pl, length);
break;
}
case WStype_TEXT:
{
this->_clientText(num, pl, length);
break;
}
}
});
//begin websocket
webSocket.begin();
}
void embeddedWoT_WebSocket::sendWebSocketTXT(String txt, String event_endpoint) {
for(i=0; i<ipe_arr.size(); i++) {
String ws_ip = ipe_arr[i]["ip"];
JsonArray ae = e_doc[ws_ip];
Serial.printf("Sending Websocket string %s. Triggered event %s. Sending to ip %s\n", txt.c_str(), event_endpoint.c_str(), ws_ip.c_str());
for(j=0; j<ae.size(); j++) {
if(!ae[j][event_endpoint.c_str()].isNull() && ae[j][event_endpoint.c_str()]) {
unsigned char ws_num = ipe_doc[ws_ip];
webSocket.sendTXT(ws_num, txt);
Serial.println("Done");
}
}
}
}
void embeddedWoT_WebSocket::loop() {
webSocket.loop();
}
void embeddedWoT_WebSocket::test() {
Serial.printf("Nel test %s\n", this->events_endpoint[0].c_str());
}
bool embeddedWoT_WebSocket::_setEventHandled(String ip_s, int num) {
bool done = false;
bool conn = false;
JsonObject obj_e;
JsonObject obj_ipe;
if(e_doc[ip_s].isNull()) {
obj_e = e_doc.createNestedArray(ip_s).createNestedObject();
obj_ipe = ipe_arr.createNestedObject();
obj_ipe["ip"] = ip_s;
obj_ipe["num"] = num;
ipe_doc[ip_s] = num;
}
else {
for(j = 0; !conn && j<e_doc[ip_s].size(); j++) {
if(!e_doc[ip_s][j][events_endpoint[i]].isNull())
conn = true;
}
if(!conn)
obj_e = e_doc[ip_s].createNestedObject();
}
if(conn) {
done = true;
webSocket.sendTXT(num, "Connection already established");
}
else {
done = true;
webSocket.sendTXT(num, "Connection confirmed - event accomplished");
if(events_subscriptionSchema[i])
obj_e[events_endpoint[i]] = false;
else {
obj_e[events_endpoint[i]] = true;
webSocket.sendTXT(num, "Subscription confirmed");
}
}
return conn;
}
bool embeddedWoT_WebSocket::_setIAHandled(String ip_s, int num, String endpoint) {
bool done = false;
bool conn = false;
JsonObject obj_ia;
JsonObject obj_ipia;
//PREAMBOLO PER LA CONNESSIONE DI UN NUOVO HOST
//Se é giá connesso lo avviso e faccio in modo che non venga fatto altro,
//manipolando il fatto che si é ripresentato di nuovo lo stesso host di prima.
if(ia_doc[ip_s].isNull()) {
obj_ia = ia_doc.createNestedArray(ip_s).createNestedObject();
obj_ia[endpoint] = true;
obj_ipia = ipia_arr.createNestedObject();
obj_ipia["ip"] = ip_s;
obj_ipia["num"] = num;
ipia_doc[ip_s] = num;
}
else {
for(j = 0; !conn && j<ia_doc[ip_s].size(); j++) {
if(!ia_doc[ip_s][j][endpoint].isNull())
conn = true;
}
if(!conn)
obj_ia = ia_doc[ip_s].createNestedObject();
}
if(conn) {
done = true;
webSocket.sendTXT(num, "Connection already established");
}
return done;
}
void embeddedWoT_WebSocket::_clientConnect(uint8_t num, uint8_t* pl, size_t length) {
IPAddress ip;
String ip_s = "";
const char* payload = (char *) pl;
String resp = "";
bool done = false;
ip = webSocket.remoteIP(num);
ip_s = ip.toString();
Serial.printf("\n%d", num);
Serial.print(" -> Connection request from ");
Serial.println(ip);
Serial.printf("Payload: %s\n", payload);
//FIRST CHECK IF IS CALLED AN EVENT ENDPOINT
//Foreach event endpoint defined
for(i = 0; !done && i < events_number; i++) {
Serial.printf("Siamo negli events, %d %d %s\n", events_number, i, this->events_endpoint[i].c_str());
//if the endpoint called matches one of the endpoint previously set
if(events_endpoint[i].equals(payload)) {
done = this->_setEventHandled(ip_s, num);
done = true;
}
}
// THEN CHECK IF IS CALLED AN ACTION ENDPOINT
//Foreach action endpoint defined
//Serial.printf("Nella callback %s\n", this->properties_endpoint[0].c_str());
for(i = 0; !done && i < actions_number; i++) {
Serial.printf("Siamo nelle actions, %d %d %s\n", actions_number, i, this->actions_endpoint[i].c_str());
//if the endpoint called matches one of the actions endpoint
if (actions_endpoint[i].equals(payload)) {
done = this->_setIAHandled(ip_s, num, actions_endpoint[i]);
done = true;
// obj_ia[actions_endpoint[i]] = true;
webSocket.sendTXT(num, "Connection confirmed - action accomplished");
resp = actions_cb[i]("{}");
webSocket.sendTXT(num, resp);
}
}
for(i = 0; !done && i < properties_number; i++) {
Serial.printf("Siamo nelle properties, %d %d %s\n", properties_number, i, this->properties_endpoint[i].c_str());
if (properties_endpoint[i].equals(payload)) {
done = this->_setIAHandled(ip_s, num, properties_endpoint[i]);
done = true;
// obj_ia[properties_endpoint[i]] = true;
webSocket.sendTXT(num, "Connection confirmed - property accomplished");
resp = properties_cb[i]();
webSocket.sendTXT(num, resp);
}
}
serializeJsonPretty(e_doc, Serial);
Serial.println();
serializeJsonPretty(es_doc, Serial);
Serial.println();
serializeJsonPretty(ipe_doc, Serial);
Serial.println();
serializeJsonPretty(ia_doc, Serial);
Serial.println();
serializeJsonPretty(ipia_doc, Serial);
Serial.println();
}
void embeddedWoT_WebSocket::bindEventSchema(DynamicJsonDocument doc) {
int eventsBound = doc.size();
JsonObject obj = doc.as<JsonObject>();
//JsonObject::iterator it = obj.begin();
int i = 0;
//setter
this->es_doc = doc;
//for debug
// serializeJsonPretty(this->es_doc, Serial);
this->events_subscriptionSchema[eventsBound];
this->events_cancellationSchema[eventsBound];
for (JsonPair kw : obj) {
// String key = it->key;
Serial.printf("Subscription for #%d is ", i);
if (!doc[kw.key()]["subscription"].isNull()) {
Serial.println("true");
this->events_subscriptionSchema[i] = true;
} else {
Serial.println("false");
this->events_subscriptionSchema[i] = false;
}
Serial.printf("Cancellation for #%d is ", i);
if (!doc[kw.key()]["cancellation"].isNull()) {
Serial.println("true");
this->events_subscriptionSchema[i] = true;
} else {
Serial.println("false");
this->events_subscriptionSchema[i] = false;
}
i++;
}
}
void embeddedWoT_WebSocket::exposeProperties(const String *endpoints, properties_handler callbacks[], int prop_num) {
this->properties_endpoint = endpoints;
this->properties_cb = callbacks;
this->properties_number = prop_num;
}
void embeddedWoT_WebSocket::exposeActions(const String *endpoints, actions_handler callbacks[], int act_num) {
this->actions_endpoint = endpoints;
this->actions_cb = callbacks;
this->actions_number = act_num;
}
void embeddedWoT_WebSocket::exposeEvents(const String *endpoints, int evt_num) {
this->events_endpoint = endpoints;
this->events_number = evt_num;
//Serial.printf("Events endpoint 0 is %s\n", this->events_endpoint[0].c_str());
}
void embeddedWoT_WebSocket::_clientDisconnect(uint8_t num, uint8_t* pl) {
IPAddress ip;
String ip_s = "";
const char* payload = (char *) pl;
String resp = "";
bool deleted = false;
Serial.printf("\n%d", num);
Serial.println(" -> Disconnected");
// remove all client subscriptions
for(i=0; i<ipe_arr.size(); i++)
if(ipe_arr[i]["num"] == num) {
deleted = true;
serializeJson(ipe_arr[i]["ip"], ip_s);
ip_s.replace("\"", "");
ipe_arr.remove(i);
ipe_doc.remove(ip_s);
e_doc.remove(ip_s);
break;
}
if(!deleted) {
// remove all client requests
for(i=0; i<ipia_arr.size(); i++) {
if(ipia_arr[i]["num"] == num) {
ip_s = "";
serializeJson(ipia_arr[i]["ip"], ip_s);
ip_s.replace("\"", "");
ipia_arr.remove(i);
ipia_doc.remove(ip_s);
ia_doc.remove(ip_s);
break;
}
}
}
serializeJsonPretty(e_doc, Serial);
Serial.println();
serializeJsonPretty(es_doc, Serial);
Serial.println();
serializeJsonPretty(ipe_doc, Serial);
Serial.println();
serializeJsonPretty(ia_doc, Serial);
Serial.println();
serializeJsonPretty(ipia_doc, Serial);
Serial.println();
}
void embeddedWoT_WebSocket::_clientText(uint8_t num, uint8_t* pl, size_t length) {
IPAddress ip;
String ip_s = "";
const char* payload = (char *) pl;
String resp = "";
DynamicJsonDocument resp_doc(400);
DeserializationError err;
bool parsingDone = false;
int validInput = 0;
String t_name = "";
String t_value = "";
String validate = "";
String message = "";
DynamicJsonDocument tmp_doc(400);
JsonObject tmp = tmp_doc.createNestedObject();
ip = webSocket.remoteIP(num);
ip_s = ip.toString();
Serial.printf("\n%d", num);
Serial.print(" -> Get Text from ");
Serial.println(ip);
Serial.printf("Payload: %s\n", payload);
err = deserializeJson(resp_doc, payload);
if(err) {
Serial.printf("deserializeJson() failed with code %s", err.c_str());
webSocket.sendTXT(num, "deserializeJson failed");
}
else {
//HERE CHECK IF THE HOST IS SUBSCRIBED TO A CERTAIN EVENT
//SO YOU HAVE TO REFER TO es_doc, THAT IS THE JSON DOCUMENT CONTAINING SCHEMAS
// String message = "";
serializeJson(resp_doc, message);
Serial.printf("Il message é %s\n", message.c_str());
//foreach pending request for websocket events
for(i = 0; !parsingDone && i<e_doc[ip_s].size(); i++) {
//foreach event
for(j = 0; !parsingDone && j<events_number; j++) {
//if the j event is the connected event to the i request
if(!e_doc[ip_s][i][events_endpoint[j]].isNull()) {
//if i'm not already subscribed && the subscription is needed
if(!e_doc[ip_s][i][events_endpoint[j]] && events_subscriptionSchema[j]) {
validInput = 0;
//foreach subscription in es_doc schema
for(k = 0; !parsingDone && k<es_doc[events_endpoint[j]]["subscription"].size(); k++) {
t_name = "";
serializeJson(es_doc[events_endpoint[j]]["subscription"][k]["name"], t_name);
t_name.replace("\"", "");
serializeJsonPretty(resp_doc, Serial);
Serial.printf("Prima c'era resp_doc, mentre t_name é %s\n\tevents_endpoint[j] é %s\n\tk é %d\n", t_name.c_str(), events_endpoint[j].c_str(), k);
//if in the text sent via ws there is a key like the one in the subscription schema
if(!resp_doc[t_name].isNull()) {
//set the value in the schema as the value in the temp object
t_value = "";
serializeJson(es_doc[events_endpoint[j]]["subscription"][k]["value"], t_value);
t_value.replace("\"", "");
//tmp["t_name"] = t_value;
tmp[t_name] = t_value.c_str();
//Serial.printf("\tes_doc[events_endpoint[j]]['subscription'][k]['value'] = %s\n", t_value.c_str());
validInput++;
}
else
parsingDone = false;
}
//if all inputs scanned are valid
if(validInput == k) {
parsingDone = true;
validate = "";
serializeJson(tmp, validate);
serializeJsonPretty(tmp, Serial);
//Serial.printf("Sembrerebbe che sia stato parsato tutto bene\n");
//if the validating json array is like the sent message
if(validate.equals(message)) {
e_doc[ip_s][i][events_endpoint[j]] = true;
webSocket.sendTXT(num, "Subscription confirmed");
}
else
webSocket.sendTXT(num, "Subscription refused");
}
}
//else if i'm already subscribed and cancellationschema is needed
else if(e_doc[ip_s][i][events_endpoint[j]] && events_cancellationSchema[j]) {
// DynamicJsonDocument tmp_doc(40);
// JsonObject tmp = tmp_doc.createNestedObject();
validInput = 0;
for(k = 0; !parsingDone && k<es_doc[events_endpoint[j]]["cancellation"].size(); k++) {
t_name = "";
serializeJson(es_doc[events_endpoint[j]]["cancellation"][k]["name"], t_name);
t_name.replace("\"", "");
if(!resp_doc[t_name].isNull()) {
t_value = "";
serializeJson(es_doc[events_endpoint[j]]["cancellation"][k]["value"], t_value);
tmp[t_name] = t_value;
validInput++;
}
else
parsingDone = false;
}
if(validInput == k) {
parsingDone = true;
validate = "";
serializeJson(tmp, validate);
if(validate.equals(message)) {
e_doc[ip_s][i][events_endpoint[j]] = false;
webSocket.sendTXT(num, "Cancellation confirmed");
}
else
webSocket.sendTXT(num, "Cancellation refused");
}
}
}
}
}
//If has not matched for subscription or cancellations
if(!parsingDone) {
//foreach element in interaction afforcance array (so if it is an handled action or an handled property)
for(i = 0; !parsingDone && i < ia_doc[ip_s].size(); i++) {
//foreach action
for(j = 0; !parsingDone && j < actions_number; j++) {
Serial.printf("Sono arrivato alle IA, j=%d\n", j);
//if there is an action handled via websocket
if(!ia_doc[ip_s][i][actions_endpoint[j]].isNull() && ia_doc[ip_s][i][actions_endpoint[j]]) {
//if() {
resp = "";
parsingDone = true;
ia_doc[ip_s][i][actions_endpoint[j]] = true;
//resp = request5(payload);
resp = actions_cb[j](payload);
webSocket.sendTXT(num, resp);
/*
for(k = 0; !parsingDone && k < actions_number; k++) {
switch(k) {
case 0: {
if(k == j) {
parsingDone = true;
ia_doc[ip_s][i][actions_endpoint[j]] = true;
//resp = request5(payload);
webSocket.sendTXT(num, resp);
}
}
break;
}
}*/
//}
}
}
}
}
if(!parsingDone)
webSocket.sendTXT(num, "Invalid message");
}
serializeJsonPretty(e_doc, Serial);
Serial.println();
serializeJsonPretty(es_doc, Serial);
Serial.println();
serializeJsonPretty(ipe_doc, Serial);
Serial.println();
serializeJsonPretty(ia_doc, Serial);
Serial.println();
serializeJsonPretty(ipia_doc, Serial);
Serial.println();
}