-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbcbaws.h
188 lines (154 loc) · 4.46 KB
/
bcbaws.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
#ifndef BCBAWS
#define BCBAWS
#include <Arduino.h>
#include "ArduinoJson.h"
#include "AsyncJson.h"
#include <ESPAsyncWebServer.h>
#include "bcbsdcard.h"
#include <SPIFFS.h>
#include "State.h"
// handles state and communication with the client
void parseCommand(String command);
// define functions
String getJson(bool b);
// web server variables
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
bool wifiavail = false;
void notifyClients() {
ws.textAll(getJson(false)); // send non-slider updating data
}
void notifyInitialClients(const String msg) {
ws.textAll(msg); // send slider updating data
}
void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) { // receive the comm from the client and convert it into a string
AwsFrameInfo *info = (AwsFrameInfo *)arg;
if (info->final && info->index == 0 && info->len == len &&
info->opcode == WS_TEXT) {
data[len] = 0;
parseCommand((char *)data);
}
}
// handle websocket connections
void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client,
AwsEventType type, void *arg, uint8_t *data, size_t len) {
switch (type) {
case WS_EVT_CONNECT:
notifyInitialClients(getJson(true));
break;
case WS_EVT_DISCONNECT:
Serial.printf("WebSocket client #%u disconnected\n", client->id());
break;
case WS_EVT_DATA:
handleWebSocketMessage(arg, data, len);
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
}
}
// init websockets
void initWebSocket() {
ws.onEvent(onEvent);
server.addHandler(&ws);
}
// init webserver
void initWebServer() {
// Route for root / web page served from spiffs
// TODO add routines for dynamically updating index.htm
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/index.htm", "text/html");
});
server.on("/settings", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send(SPIFFS, "/settings.htm", "text/html");
});
// start the web server
server.begin();
}
// convert state to json
String getJson(bool b) {
StaticJsonDocument<250> data;
// struct mp3Status {
// int slider1;
// int slider2;
// };
data["slider1"] = state.slider1;
data["slider2"] = state.slider2;
data["hour"] = state.alarmHour;
data["minute"] = state.alarmMinute;
data["set"] = state.alarmSet;
if (b) {
data["initial"] = "true";
}
if (state.reload) {
data["reload"] = "true";
state.reload = false;
}
String response;
serializeJson(data, response);
state.json = response;
writeFile(SPIFFS, "/data.json", state.json.c_str());
return response;
}
// convert json to state
void setState() {
StaticJsonDocument<250> data;
// struct mp3Status {
// int slider1;
// int slider2;
// };
deserializeJson(data, state.json);
state.slider1 = data["slider1"];
state.slider2 = data["slider2"];
}
//parse the command coming from the client(s)
void parseCommand(String command) {
Serial.println(command);
if (command.substring(0, 2) == "s1") { // slider 1 value
String val1 = command.substring(2);
int val2 = atoi((char *)&val1);
state.slider1 = val2;
notifyClients();
}
if (command.substring(0, 2) == "s2") { // slider 2 value
String val1 = command.substring(2);
int val2 = atoi((char *)&val1);
state.slider2 = val2;
notifyClients();
}
// file upload handler
if (command.substring(0, 1) == "a") {
int lastCommaIndex = command.lastIndexOf(',');
String sentAlarm = command.substring(lastCommaIndex + 1);
int lastColonIndex = sentAlarm.lastIndexOf(':');
String sentHour = sentAlarm.substring(0, lastColonIndex);
state.alarmHour = (atoi((char *)&sentHour));
String sentMinute = sentAlarm.substring(lastColonIndex + 1);
state.alarmMinute = (atoi((char *)&sentMinute));
if (state.alarmHour == -1) {
state.alarmSet = false;
} else {
state.alarmSet = true;
}
notifyClients();
}
if (command.substring(0, 4) == "upld") {
state.filename = command.substring(5);
deleteFile(SPIFFS, "/temp.txt");
}
if (command.substring(0, 4) == "comp") {
deleteFile(SPIFFS, ("/" + state.filename).c_str());
renameFile(SPIFFS, "/temp.txt", ("/" + state.filename).c_str());
state.filename = "";
}
if (command.substring(0, 4) == "file") {
String message = command.substring(5);
appendFile(SPIFFS, "/temp.txt", message.c_str());
}
if (command == "reload") {
state.reload = true;
notifyClients();
state.reload = false;
}
}
#endif