Repository navigation
Expand file tree
/
Copy pathBlinkerArduinoWS.h
More file actions
185 lines (154 loc) · 5.02 KB
/
Copy pathBlinkerArduinoWS.h
File metadata and controls
185 lines (154 loc) · 5.02 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
#ifndef BlinkerWebSocket_H
#define BlinkerWebSocket_H
#include <Blinker/BlinkerProtocol.h>
#include <modules/WebSockets/WebSocketsServer.h>
// #include <WebSocketsServer.h>
// #include <Hash.h>
#define WS_SERVERPORT 81
WebSocketsServer webSocket = WebSocketsServer(WS_SERVERPORT);
static char msgBuf[BLINKER_MAX_READ_SIZE];
static bool isConnect = false;
static bool isAvail = false;
static void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length)
{
switch(type)
{
case WStype_DISCONNECTED:
#ifdef BLINKER_DEBUG_ALL
BLINKER_LOG2(BLINKER_F("Disconnected! "), num);
#endif
isConnect = false;
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(num);
#ifdef BLINKER_DEBUG_ALL
BLINKER_LOG6(BLINKER_F("num: "), num, BLINKER_F(", Connected from: "), ip, BLINKER_F(", url: "), (char *)payload);
#endif
// send message to client
webSocket.sendTXT(num, "{\"state\":\"connected\"}");
isConnect = true;
}
break;
case WStype_TEXT:
#ifdef BLINKER_DEBUG_ALL
BLINKER_LOG6(BLINKER_F("num: "), num, BLINKER_F(", get Text: "), (char *)payload, BLINKER_F(", length: "), length);
#endif
if (length < BLINKER_MAX_READ_SIZE) {
// msgBuf = (char*)malloc((length+1)*sizeof(char));
// memcpy (msgBuf, (char*)payload, length);
// buflen = length;
strcpy(msgBuf, (char*)payload);
isAvail = true;
}
// send message to client
// webSocket.sendTXT(num, "message here");
// send data to all connected clients
// webSocket.broadcastTXT("message here");
break;
case WStype_BIN:
// BLINKER_LOG4("num: ", num, " get binary length: ", length);
// hexdump(payload, length);
// send message to client
// webSocket.sendBIN(num, payload, length);
break;
}
}
class BlinkerArduinoWS
{
public :
BlinkerArduinoWS() {}
void begin(String deviceName)
{
webSocket.begin();
webSocket.onEvent(webSocketEvent);
BLINKER_LOG1(BLINKER_F("webSocket server started"));
BLINKER_LOG4(BLINKER_F("ws://"), deviceName, BLINKER_F(".local:"), WS_SERVERPORT);
}
bool available()
{
webSocket.loop();
if (isAvail) {
isAvail = false;
return true;
}
else {
return false;
}
}
String lastRead() { return STRING_format(msgBuf); }
void print(String s_data)
{
#ifdef BLINKER_DEBUG_ALL
BLINKER_LOG2(BLINKER_F("Response: "), s_data);
#endif
if(connected()) {
bool state = STRING_contains_string(s_data, BLINKER_CMD_NOTICE);
if (!state) {
state = (STRING_contains_string(s_data, BLINKER_CMD_STATE)
&& STRING_contains_string(s_data, BLINKER_CMD_CONNECTED));
}
if (!state) {
if (!checkPrintSpan()) {
respTime = millis();
return;
}
}
respTime = millis();
#ifdef BLINKER_DEBUG_ALL
BLINKER_LOG1(BLINKER_F("Succese..."));
#endif
webSocket.broadcastTXT(s_data);
}
else {
#ifdef BLINKER_DEBUG_ALL
BLINKER_LOG1(BLINKER_F("Faile... Disconnected"));
#endif
}
#ifdef BLINKER_DEBUG_ALL
BLINKER_FreeHeap();
#endif
}
bool connect()
{
webSocket.loop();
return connected();
}
bool connected() { return *isHandle; }
void disconnect() { webSocket.disconnect(); }
private :
bool checkPrintSpan() {
if (millis() - respTime < BLINKER_PRINT_MSG_LIMIT) {
if (respTimes > BLINKER_PRINT_MSG_LIMIT) {
#ifdef BLINKER_DEBUG_ALL
BLINKER_ERR_LOG1(BLINKER_F("WEBSOCKETS CLIENT NOT ALIVE OR MSG LIMIT"));
#endif
return false;
}
else {
respTimes++;
return true;
}
}
else {
respTimes = 0;
return true;
}
}
// bool checkWSCanPrint() {
// if (millis() - respTime >= BLINKER_PRINT_MSG_LIMIT || respTime == 0) {
// return true;
// }
// else {
// #ifdef BLINKER_DEBUG_ALL
// BLINKER_ERR_LOG1("WEBSOCKETS CLIENT NOT ALIVE OR MSG LIMIT");
// #endif
// return false;
// }
// }
protected :
bool* isHandle = &isConnect;
uint8_t respTimes = 0;
uint32_t respTime = 0;
};
#endif