forked from forkineye/ESPixelStick
-
Notifications
You must be signed in to change notification settings - Fork 5
/
wshandler.h
566 lines (490 loc) · 18.9 KB
/
wshandler.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
* wshandler.h
*
* Project: ESPixelStick - An ESP8266 and E1.31 based pixel driver
* Copyright (c) 2016 Shelby Merrick
* http://www.forkineye.com
*
* This program is provided free for you to use in any way that you wish,
* subject to the laws and regulations where you are using it. Due diligence
* is strongly suggested before using this code. Please give credit where due.
*
* The Author makes no warranty of any kind, express or implied, with regard
* to this program or the documentation contained in this document. The
* Author shall not be liable in any event for incidental or consequential
* damages in connection with, or arising out of, the furnishing, performance
* or use of these programs.
*
*/
#ifndef WSHANDLER_H_
#define WSHANDLER_H_
#include "ESPixelStick.h"
#include "gpio.h"
#if defined(ESPS_MODE_PIXEL)
#include "PixelDriver.h"
extern PixelDriver pixels; // Pixel object
#elif defined(ESPS_MODE_SERIAL)
#include "SerialDriver.h"
extern SerialDriver serial; // Serial object
#endif
#if defined(ESPS_ENABLE_UDPRAW)
#include "udpraw.h"
extern UdpRaw udpraw;
#endif
extern EffectEngine effects; // EffectEngine for test modes
extern AsyncWebSocket ws;
extern ESPAsyncE131 e131; // ESPAsyncE131 with X buffers
extern config_t config; // Current configuration
extern uint32_t *seqError; // Sequence error tracking for each universe
extern uint16_t uniLast; // Last Universe to listen for
extern bool reboot; // Reboot flag
extern unsigned long mqtt_last_seen; // millis() timestamp of last message
extern uint32_t mqtt_num_packets; // count of message rcvd
extern const char CONFIG_FILE[];
/*
Packet Commands
E1 - Get Elements
G1 - Get Config
G2 - Get Config Status
G3 - Get Current Effect and Effect Config Options
G4 - Get Gamma table values
T0 - Disable Testing
T1 - Static Testing
T2 - Blink Test
T3 - Flash Test
T4 - Chase Test
T5 - Rainbow Test
T6 - Fire flicker
T7 - Lightning
T8 - Breathe
V1 - View Stream
S1 - Set Network Config
S2 - Set Device Config
S3 - Set Effect Startup Config
S4 - Set Gamma and Brightness (but dont save)
XJ - Get RSSI,heap,uptime, e131 stats
X6 - Reboot
*/
EFUpdate efupdate;
uint8_t * WSframetemp;
uint8_t * confuploadtemp;
// send unsolicited update to all clients with the client who initiated it
void runningEffectSendAll(String updateSource) {
String response;
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
effects.runningEffectToJson (json);
json["updateSource"] = updateSource;
json.printTo(response);
ws.textAll("G3" + response);
}
void procX(uint8_t *data, AsyncWebSocketClient *client) {
switch (data[1]) {
case 'J': {
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
// system statistics
JsonObject &system = json.createNestedObject("system");
system["rssi"] = (String)WiFi.RSSI();
system["freeheap"] = (String)ESP.getFreeHeap();
system["uptime"] = (String)millis();
switch (config.ds) {
case DataSource::E131:
system["datasource"] = "E131";
system["effectname"] = "N/A";
break;
case DataSource::MQTT:
system["datasource"] = "MQTT";
system["effectname"] = (String)effects.getEffect();
break;
case DataSource::WEB:
system["datasource"] = "web";
system["effectname"] = (String)effects.getEffect();
break;
case DataSource::IDLEWEB:
system["datasource"] = "Idle Effect";
system["effectname"] = (String)effects.getEffect();
break;
default:
system["datasource"] = "unknown";
system["effectname"] = (String)effects.getEffect();
break;
}
// E131 statistics
JsonObject &e131J = json.createNestedObject("e131");
uint32_t seqErrors = 0;
for (int i = 0; i < ((uniLast + 1) - config.universe); i++)
seqErrors =+ seqError[i];
e131J["universe"] = (String)config.universe;
e131J["uniLast"] = (String)uniLast;
e131J["num_packets"] = (String)e131.stats.num_packets;
e131J["seq_errors"] = (String)seqErrors;
e131J["packet_errors"] = (String)e131.stats.packet_errors;
e131J["last_clientIP"] = e131.stats.last_clientIP.toString();
e131J["last_seen"] = e131.stats.last_seen ? (String) (millis() - e131.stats.last_seen) : "never";
// MQTT statistics
JsonObject &mqtt = json.createNestedObject("mqtt");
mqtt["num_packets"] = (String)mqtt_num_packets;
mqtt["last_seen"] = mqtt_last_seen ? (String) (millis() - mqtt_last_seen) : "never";
#if defined(ESPS_ENABLE_UDPRAW)
// UDP raw statistics
JsonObject &udp = json.createNestedObject("udp");
udp["num_packets"] = (String)udpraw.stats.num_packets;
udp["short_packets"] = (String)udpraw.stats.short_packets;
udp["long_packets"] = (String)udpraw.stats.long_packets;
udp["last_clientIP"] = udpraw.stats.last_clientIP.toString();
udp["last_seen"] = udpraw.stats.last_seen ? (String) (millis() - udpraw.stats.last_seen) : "never";
#endif
String response;
json.printTo(response);
client->text("XJ" + response);
break;
}
case '6': // Init 6 baby, reboot!
reboot = true;
}
}
void procE(uint8_t *data, AsyncWebSocketClient *client) {
switch (data[1]) {
case '1':
// Create buffer and root object
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
#if defined (ESPS_MODE_PIXEL)
// Pixel Types
JsonObject &p_type = json.createNestedObject("p_type");
p_type["WS2811 800kHz"] = static_cast<uint8_t>(PixelType::WS2811);
p_type["GE Color Effects"] = static_cast<uint8_t>(PixelType::GECE);
// Pixel Colors
JsonObject &p_color = json.createNestedObject("p_color");
p_color["RGB"] = static_cast<uint8_t>(PixelColor::RGB);
p_color["GRB"] = static_cast<uint8_t>(PixelColor::GRB);
p_color["BRG"] = static_cast<uint8_t>(PixelColor::BRG);
p_color["RBG"] = static_cast<uint8_t>(PixelColor::RBG);
p_color["GBR"] = static_cast<uint8_t>(PixelColor::GBR);
p_color["BGR"] = static_cast<uint8_t>(PixelColor::BGR);
#elif defined (ESPS_MODE_SERIAL)
// Serial Protocols
JsonObject &s_proto = json.createNestedObject("s_proto");
s_proto["DMX512"] = static_cast<uint8_t>(SerialType::DMX512);
s_proto["Renard"] = static_cast<uint8_t>(SerialType::RENARD);
// Serial Baudrates
JsonObject &s_baud = json.createNestedObject("s_baud");
s_baud["38400"] = static_cast<uint32_t>(BaudRate::BR_38400);
s_baud["57600"] = static_cast<uint32_t>(BaudRate::BR_57600);
s_baud["115200"] = static_cast<uint32_t>(BaudRate::BR_115200);
s_baud["230400"] = static_cast<uint32_t>(BaudRate::BR_230400);
s_baud["250000"] = static_cast<uint32_t>(BaudRate::BR_250000);
s_baud["460800"] = static_cast<uint32_t>(BaudRate::BR_460800);
#endif
String response;
json.printTo(response);
client->text("E1" + response);
break;
}
}
void procG(uint8_t *data, AsyncWebSocketClient *client) {
switch (data[1]) {
case '1': {
String response;
serializeConfig(response, false, true);
client->text("G1" + response);
break;
}
case '2': {
// Create buffer and root object
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
json["ssid"] = (String)WiFi.SSID();
json["hostname"] = (String)WiFi.hostname();
json["ip"] = WiFi.localIP().toString();
json["mac"] = WiFi.macAddress();
json["version"] = (String)VERSION;
json["built"] = (String)BUILD_DATE;
json["flashchipid"] = String(ESP.getFlashChipId(), HEX);
json["usedflashsize"] = (String)ESP.getFlashChipSize();
json["realflashsize"] = (String)ESP.getFlashChipRealSize();
json["freeheap"] = (String)ESP.getFreeHeap();
String response;
json.printTo(response);
client->text("G2" + response);
break;
}
case '3': {
String response;
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
effects.runningEffectToJson(json);
effects.EffectListToJson(json);
// tell the client its "unique" id based on the IP and port as seen by us
json["browserIPPort"] = String(client->remoteIP().toString()
+ ":" + client->remotePort());
json.printTo(response);
client->text("G3" + response);
//LOG_PORT.print(response);
break;
}
case '4': {
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.createObject();
JsonArray &gamma = json.createNestedArray("gamma");
for (int i=0; i<256; i++) {
gamma.add(GAMMA_TABLE[i] >> 8);
}
String response;
json.printTo(response);
client->text("G4" + response);
break;
}
}
}
void procP(uint8_t *data, AsyncWebSocketClient *client) {
client->text("PP" + handleGPIO(reinterpret_cast<char*>(data + 2)) );
}
void procS(uint8_t *data, AsyncWebSocketClient *client) {
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.parseObject(reinterpret_cast<char*>(data + 2));
if (!json.success()) {
LOG_PORT.println(F("*** procS(): Parse Error ***"));
LOG_PORT.println(reinterpret_cast<char*>(data));
return;
}
bool reboot = false;
switch (data[1]) {
case '1': // Set Network Config
dsNetworkConfig(json);
saveConfig();
client->text("S1");
break;
case '2': // Set Device Config
// Reboot if MQTT changed
if (config.mqtt != json["mqtt"]["enabled"])
reboot = true;
dsDeviceConfig(json);
saveConfig();
if (reboot)
client->text("S1");
else
client->text("S2");
break;
case '3': // Set Effect Startup Config
dsEffectConfig(json);
saveConfig();
client->text("S3");
break;
case '4': // Set Gamma (but no save)
dsGammaConfig(json);
client->text("S4");
break;
}
}
void procT(uint8_t *data, AsyncWebSocketClient *client) {
if (data[1] == '0') {
//TODO: Store previous data source when effect is selected so we can switch back to it
config.ds = DataSource::E131;
effects.clearAll();
}
else if ( (data[1] >= '1') && (data[1] <= '8') ) {
String TCode;
TCode += (char)data[0];
TCode += (char)data[1];
const EffectDesc* effectInfo = effects.getEffectInfo(TCode);
if (effectInfo) {
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.parseObject(reinterpret_cast<char*>(data + 2));
config.ds = DataSource::WEB;
effects.setEffect( effectInfo->name );
if ( effectInfo->hasColor ) {
if (json.containsKey("r") && json.containsKey("g") && json.containsKey("b")) {
effects.setColor({json["r"], json["g"], json["b"]});
}
}
if ( effectInfo->hasMirror ) {
if (json.containsKey("mirror")) {
effects.setMirror(json["mirror"]);
}
}
if ( effectInfo->hasReverse ) {
if (json.containsKey("reverse")) {
effects.setReverse(json["reverse"]);
}
}
if ( effectInfo->hasAllLeds ) {
if (json.containsKey("allleds")) {
effects.setAllLeds(json["allleds"]);
}
}
if (json.containsKey("speed")) {
effects.setSpeed(json["speed"]);
}
if (json.containsKey("brightness")) {
effects.setBrightness(json["brightness"]);
}
client->text("OK");
// send unsolicited update to all clients with the client who initiated it
runningEffectSendAll( String(client->remoteIP().toString()
+ ":" + client->remotePort()));
}
}
if (config.mqtt)
publishState();
}
void procV(uint8_t *data, AsyncWebSocketClient *client) {
switch (data[1]) {
case '1': { // View stream
#if defined(ESPS_MODE_PIXEL)
client->binary(pixels.getData(), config.channel_count);
#elif defined(ESPS_MODE_SERIAL)
if (config.serial_type == SerialType::DMX512)
client->binary(&serial.getData()[1], config.channel_count);
else
client->binary(&serial.getData()[2], config.channel_count);
#endif
break;
}
}
}
void handle_fw_upload(AsyncWebServerRequest *request, String filename,
size_t index, uint8_t *data, size_t len, bool final) {
if (!index) {
WiFiUDP::stopAll();
LOG_PORT.print(F("* Upload Started: "));
LOG_PORT.println(filename.c_str());
efupdate.begin();
}
if (!efupdate.process(data, len)) {
LOG_PORT.print(F("*** UPDATE ERROR: "));
LOG_PORT.println(String(efupdate.getError()));
}
if (efupdate.hasError())
request->send(200, "text/plain", "Update Error: " +
String(efupdate.getError()));
if (final) {
request->send(200, "text/plain", "Update Finished: " +
String(efupdate.getError()));
LOG_PORT.println(F("* Upload Finished."));
efupdate.end();
SPIFFS.begin();
saveConfig();
reboot = true;
}
}
void handle_config_upload(AsyncWebServerRequest *request, String filename,
size_t index, uint8_t *data, size_t len, bool final) {
static File file;
if (!index) {
WiFiUDP::stopAll();
LOG_PORT.print(F("* Config Upload Started: "));
LOG_PORT.println(filename.c_str());
if (confuploadtemp) {
free (confuploadtemp);
confuploadtemp = nullptr;
}
confuploadtemp = (uint8_t*) malloc(CONFIG_MAX_SIZE);
}
LOG_PORT.printf("index %d len %d\n", index, len);
memcpy(confuploadtemp + index, data, len);
confuploadtemp[index + len] = 0;
if (final) {
int filesize = index+len;
LOG_PORT.print(F("* Config Upload Finished:"));
LOG_PORT.printf(" %d bytes", filesize);
DynamicJsonBuffer jsonBuffer;
JsonObject &json = jsonBuffer.parseObject(reinterpret_cast<char*>(confuploadtemp));
if (!json.success()) {
LOG_PORT.println(F("*** Parse Error ***"));
LOG_PORT.println(reinterpret_cast<char*>(confuploadtemp));
request->send(500, "text/plain", "Config Update Error." );
} else {
dsNetworkConfig(json);
dsDeviceConfig(json);
dsEffectConfig(json);
saveConfig();
request->send(200, "text/plain", "Config Update Finished: " );
// reboot = true;
}
if (confuploadtemp) {
free (confuploadtemp);
confuploadtemp = nullptr;
}
}
}
void wsEvent( __attribute__ ((unused)) AsyncWebSocket *server, AsyncWebSocketClient *client,
AwsEventType type, void * arg, uint8_t *data, size_t len) {
switch (type) {
case WS_EVT_DATA: {
AwsFrameInfo *info = static_cast<AwsFrameInfo*>(arg);
if (info->final && info->index == 0 && info->len == len) {
if (info->opcode == WS_TEXT) {
data[len] = 0;
switch (data[0]) {
case 'X':
procX(data, client);
break;
case 'E':
procE(data, client);
break;
case 'G':
procG(data, client);
break;
case 'P':
procP(data, client);
break;
case 'S':
procS(data, client);
break;
case 'T':
procT(data, client);
break;
case 'V':
procV(data, client);
break;
}
} else {
LOG_PORT.println(F("-- binary message --"));
}
} else { // multiframe WS message
if ( (info->message_opcode == WS_TEXT) && (info->len < CONFIG_MAX_SIZE) ) {
if ( (info->index == 0) && (info->num == 0) ) {
if (WSframetemp) {
free (WSframetemp);
WSframetemp = nullptr;
}
WSframetemp = (uint8_t*) malloc(CONFIG_MAX_SIZE);
}
memcpy(WSframetemp + info->index, data, len);
if ( (info->index + len) == info->len) {
if (info->final) {
WSframetemp[info->len] = 0;
switch (WSframetemp[0]) {
case 'S':
procS(WSframetemp, client);
break;
}
if (WSframetemp) {
free (WSframetemp);
WSframetemp = nullptr;
}
}
}
}
}
break;
}
case WS_EVT_CONNECT:
LOG_PORT.print(F("* WS Connect - "));
LOG_PORT.println(client->id());
break;
case WS_EVT_DISCONNECT:
LOG_PORT.print(F("* WS Disconnect - "));
LOG_PORT.println(client->id());
break;
case WS_EVT_PONG:
LOG_PORT.println(F("* WS PONG *"));
break;
case WS_EVT_ERROR:
LOG_PORT.println(F("** WS ERROR **"));
break;
}
}
#endif /* WSHANDLER_H_ */