-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembeddedWoT_WebSocket.h
More file actions
148 lines (134 loc) · 5.75 KB
/
embeddedWoT_WebSocket.h
File metadata and controls
148 lines (134 loc) · 5.75 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
#ifndef embeddedWoT_WebSocket_h
#define embeddedWoT_WebSocket_h
#include "Arduino.h"
#include <WebSocketsServer.h>
#ifndef WOT_HANDLER_FUNC
#define WOT_HANDLER_FUNC
typedef String (*properties_handler)();
typedef String (*actions_handler)(String);
#endif
/**
* Class for managing WebSocket requests for WoT Embedded devices.
*/
class embeddedWoT_WebSocket {
public:
/**
* Constructor. Requires WebSocket port
* @param portSocket the port of the WebSocket
*/
embeddedWoT_WebSocket(int portSocket);//: ia_doc(1000), ipia_doc(2000), e_doc(1000), es_doc(20), ipe_doc(2000);
/**
* Sends a WebSocket message for a certain event.
* It will send the same message to all hosts subscribed in the specified endpoint.
* @param txt the text to send
* @param event_endpoint the endpoint to send message to
*/
void sendWebSocketTXT(String txt, String event_endpoint);
/**
* Loops the webSocket for accepting requests.
*/
void loop();
void test();
/**
* Binds the event schema for subscriptions and/or events_cancellations.
* The document should have the following form:
* {"/test0/events/event1": {
* "subscription": [
* {
* "name": "sbs1",
* "value": "true"
* }
* ],
* "data": [
* {
* "name": "dataschema1",
* "value": "100"
* }
* ],
* "cancellation": [
* {
* "name": "cnc1",
* "value": "true"
* }
* ]
* }
*
* @param doc the document containing the events schema.
*/
void bindEventSchema(DynamicJsonDocument doc);
/**
* Expose properties via WebSocket protocol.
* It requires an array of endpoints and an array of callback functions of type properties_handler (String fun(void)).
* When the i endpoint of the array will be called, the response will be given by the i function of the array of callbacks.
* @param endpoints array of endpoints
* @param callbacks array of properties_handler callbacks
* @param prop_num the total number of endpoints/callbacks
*/
void exposeProperties(const String *endpoints, properties_handler callbacks[], int prop_num);
/**
* Expose actions via WebSocket protocol.
* It requires an array of endpoints and an array of callback functions of type actions_handler (String fun(String)).
* When the i endpoint of the array will be called, the response will be given by the i function of the array of callbacks.
* @param endpoints array of endpoints
* @param callbacks array of actions_handler callbacks
* @param act_num the total number of endpoints/callbacks
*/
void exposeActions(const String *endpoints, actions_handler callbacks[], int act_num);
/**
* Expose properties via WebSocket protocol.
* For emitting an event, use sendWebSocketTXT() and refer the defined endpoint.
*
* If you set an empty subscription schema, you have to subscribe anyway sending an empty object as first WS message.
* Else if you set a subscription schema, you have to use one of them before receiving events
* @param endpoints array of endpoints
* @param evt_num the total number of endpoints/callbacks
*/
void exposeEvents(const String *endpoints, int evt_num);
private:
void _clientDisconnect(uint8_t num, uint8_t* pl);
void _clientConnect(uint8_t num, uint8_t* pl, size_t length);
void _clientText(uint8_t num, uint8_t* pl, size_t length);
bool _setEventHandled(String ip_s, int num);
bool _setIAHandled(String ip_s, int num, String endpoint);
// document to handle Interaction Affordances WebSocket requests
DynamicJsonDocument ia_doc;
// document to store the ip addresses of clients connected to WebSocket channel for Interaction Affordances requests
DynamicJsonDocument ipia_doc;
// the array inside ipia_doc
JsonArray ipia_arr;
// document to handle Events WebSocket requests
DynamicJsonDocument e_doc;
// document to handle Events Schemas
DynamicJsonDocument es_doc;
// document to store the ip addresses of clients connected to WebSocket channel for Events requests
DynamicJsonDocument ipe_doc;
// the array inside ipe_doc
JsonArray ipe_arr;
// document to handle Actions WebSocket requests
DynamicJsonDocument ac_doc;
// websocket connection handler
WebSocketsServer webSocket;
//on i position contains true if i event requires subscription
bool events_subscriptionSchema[];
//on i position contains true if i event requires cancellation
bool events_cancellationSchema[];
//contains the event endpoints
const String* events_endpoint;
//contains the actions endpoints
const String* actions_endpoint;
//contains the properties endpoints
const String* properties_endpoint;
//contains the action callbacks
actions_handler *actions_cb;
//contains the properties callbacks
properties_handler *properties_cb;
//Number of properties exposed
int properties_number;
//Number of actions exposed
int actions_number;
//Number of events exposed
int events_number;
int i, j, k, n;
};
// #include "embeddedWoT_WebSocket.cpp"
#endif