-
Notifications
You must be signed in to change notification settings - Fork 417
/
websocket.esp
187 lines (186 loc) · 4.59 KB
/
websocket.esp
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
void ICACHE_FLASH_ATTR procMsg(AsyncWebSocketClient *client, size_t sz)
{
// We should always get a JSON object (stringfied) from browser, so parse it
DynamicJsonBuffer jsonBuffer;
//JsonObject &root = jsonBuffer.parseObject(msg);
char json[sz + 1];
memcpy(json, (char *)(client->_tempObject), sz);
json[sz] = '\0';
JsonObject &root = jsonBuffer.parseObject(json);
if (!root.success())
{
#ifdef DEBUG
Serial.println(F("[ WARN ] Couldn't parse WebSocket message"));
#endif
free(client->_tempObject);
client->_tempObject = NULL;
//delete client->_tempObject;
return;
}
// Web Browser sends some commands, check which command is given
const char *command = root["command"];
// Check whatever the command is and act accordingly
if (strcmp(command, "remove") == 0)
{
const char *uid = root["uid"];
String filename = "/P/";
filename += uid;
SPIFFS.remove(filename);
}
else if (strcmp(command, "configfile") == 0)
{
File f = SPIFFS.open("/config.json", "w+");
if (f)
{
size_t len = root.measurePrettyLength();
root.prettyPrintTo(f);
//f.print(msg);
f.close();
shouldReboot = true;
//ESP.restart();
writeEvent("INFO", "sys", "Config stored in the SPIFFS", String(len) + " bytes");
#ifdef DEBUG
Serial.print(F("[ INFO ] Config stored in the SPIFFS ("));
Serial.print(len);
Serial.println(F(" bytes)"));
#endif
}
}
else if (strcmp(command, "userlist") == 0)
{
int page = root["page"];
sendUserList(page, client);
}
else if (strcmp(command, "status") == 0)
{
sendStatus();
}
else if (strcmp(command, "restart") == 0)
{
shouldReboot = true;
}
else if (strcmp(command, "destroy") == 0)
{
formatreq = true;
}
else if (strcmp(command, "geteventlog") == 0)
{
int page = root["page"];
sendEventLog(page);
}
else if (strcmp(command, "getlatestlog") == 0)
{
int page = root["page"];
sendLatestLog(page);
}
else if (strcmp(command, "clearevent") == 0)
{
SPIFFS.remove("/eventlog.json");
writeEvent("WARN", "sys", "Event log cleared!", "");
}
else if (strcmp(command, "clearlatest") == 0)
{
SPIFFS.remove("/latestlog.json");
writeEvent("WARN", "sys", "Latest Access log cleared!", "");
}
else if (strcmp(command, "userfile") == 0)
{
const char *uid = root["uid"];
String filename = "/P/";
filename += uid;
File f = SPIFFS.open(filename, "w+");
// Check if we created the file
if (f)
{
//f.print(msg);
root.printTo(f);
}
f.close();
ws.textAll("{\"command\":\"result\",\"resultof\":\"userfile\",\"result\": true}");
}
else if (strcmp(command, "testrelay") == 0)
{
activateRelay = true;
previousMillis = millis();
ws.textAll("{\"command\":\"giveAccess\"}");
}
else if (strcmp(command, "scan") == 0)
{
WiFi.scanNetworksAsync(printScanResult, true);
}
else if (strcmp(command, "gettime") == 0)
{
timerequest = true;
}
else if (strcmp(command, "settime") == 0)
{
time_t t = root["epoch"];
setTime(t);
timerequest = true;
}
else if (strcmp(command, "getconf") == 0)
{
File configFile = SPIFFS.open("/config.json", "r");
if (configFile)
{
size_t len = configFile.size();
AsyncWebSocketMessageBuffer *buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer)
{
configFile.readBytes((char *)buffer->get(), len + 1);
ws.textAll(buffer);
}
configFile.close();
}
}
free(client->_tempObject);
client->_tempObject = NULL;
}
// Handles WebSocket Events
void ICACHE_FLASH_ATTR onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
{
if (type == WS_EVT_ERROR)
{
#ifdef DEBUG
Serial.printf("[ WARN ] WebSocket[%s][%u] error(%u): %s\r\n", server->url(), client->id(), *((uint16_t *)arg), (char *)data);
#endif
}
else if (type == WS_EVT_DATA)
{
AwsFrameInfo *info = (AwsFrameInfo *)arg;
uint64_t index = info->index;
uint64_t infolen = info->len;
if (info->final && info->index == 0 && infolen == len)
{
//the whole message is in a single frame and we got all of it's data
client->_tempObject = malloc(len);
if (client->_tempObject != NULL)
{
memcpy((uint8_t *)(client->_tempObject), data, len);
}
procMsg(client, infolen);
}
else
{
//message is comprised of multiple frames or the frame is split into multiple packets
if (index == 0)
{
if (info->num == 0 && client->_tempObject == NULL)
{
client->_tempObject = malloc(infolen);
}
}
if (client->_tempObject != NULL)
{
memcpy((uint8_t *)(client->_tempObject) + index, data, len);
}
if ((index + len) == infolen)
{
if (info->final)
{
procMsg(client, infolen);
}
}
}
}
}