-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocketEventHandler.ino
More file actions
312 lines (279 loc) · 11.3 KB
/
websocketEventHandler.ino
File metadata and controls
312 lines (279 loc) · 11.3 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
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
void websocketEventHandler(AsyncWebSocket* server, AsyncWebSocketClient* client, AwsEventType type, void* arg, uint8_t* data, size_t len) {
switch (type) {
case WS_EVT_CONNECT:
{
log_d("client %i connected on %s", client->id(), server->url());
{
String s;
client->text(playList.toString(s));
s.clear();
favoritesToString(s);
client->text(!s.equals("") ? s : "favorites\nThe folder '" + String(FAVORITES_FOLDER) + "' could not be found!\n");
}
client->printf("status\n%s\n", _paused ? "paused" : "playing");
client->printf("%s\n%i\n", CURRENT_HEADER, playList.currentItem());
client->printf("%s\n%i\n", VOLUME_HEADER, _playerVolume);
client->text(showstation);
client->text(streamtitle);
if (_paused && _currentSize) client->printf("progress\n%i\n%i\n", _currentPosition, _currentSize);
}
break;
case WS_EVT_DISCONNECT:
log_d("client %i disconnected from %s", client->id(), server->url());
break;
case WS_EVT_ERROR:
log_e("ws error");
ws.close(client->id());
break;
case WS_EVT_PONG:
log_i("ws pong");
break;
case WS_EVT_DATA:
{
AwsFrameInfo* info = (AwsFrameInfo*)arg;
if (info->opcode == WS_TEXT) {
if (info->final && info->index == 0 && info->len == len)
handleSingleFrame(client, data, len);
else
handleMultiFrame(client, data, len, info);
}
break;
}
default: log_i("unhandled ws event!");
}
ws.cleanupClients();
log_d("Heap: %d Free: ", ESP.getHeapSize(), ESP.getFreeHeap());
log_d("Smallest free stack: %i bytes", uxTaskGetStackHighWaterMark(NULL));
log_d("Largest free continuous memory block: %i bytes", heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT));
}
void handleSingleFrame(AsyncWebSocketClient* client, uint8_t* data, size_t len) {
data[len] = 0;
char* pch = strtok(reinterpret_cast<char*>(data), "\n");
if (!pch) return;
static size_t _pausedPosition = 0;
if (_paused && !strcmp("unpause", pch)) {
playerMessage msg;
msg.action = playerMessage::CONNECTTOHOST;
msg.value = _pausedPosition;
snprintf(msg.url, PLAYLIST_MAX_URL_LENGTH, playList.url(playList.currentItem()).c_str());
xQueueSend(playerQueue, &msg, portMAX_DELAY);
return;
}
if (!_paused && !strcmp("pause", pch)) {
pch = strtok(NULL, "\n");
if (!pch) return;
_paused = true;
_pausedPosition = atoi(pch);
ws.textAll("status\npaused\n");
playerMessage msg;
msg.action = playerMessage::STOPSONG;
xQueueSend(playerQueue, &msg, portMAX_DELAY);
}
else if (!strcmp("volume", pch)) {
pch = strtok(NULL, "\n");
if (!pch) return;
playerMessage msg;
msg.action = playerMessage::SET_VOLUME;
const uint8_t volume = atoi(pch);
msg.value = volume > VS1053_MAXVOLUME ? VS1053_MAXVOLUME : volume;
xQueueSend(playerQueue, &msg, portMAX_DELAY);
_playerVolume = msg.value;
//TODO: send to all but not this client
ws.printfAll("%s\n%i\n", VOLUME_HEADER, volume);
}
else if (!strcmp("previous", pch)) {
if (playList.currentItem() > 0) {
playList.setCurrentItem(playList.currentItem() - 1);
startItem(playList.currentItem());
}
}
else if (!strcmp("next", pch)) {
if (playList.currentItem() == PLAYLIST_STOPPED) return;
if (playList.currentItem() < playList.size() - 1) {
startNextItem();
}
}
else if (!strcmp("filetoplaylist", pch) || !strcmp("_filetoplaylist", pch)) {
const bool startnow = (pch[0] == '_');
const uint32_t previousSize = playList.size();
pch = strtok(NULL, "\n");
while (pch) {
playList.add({ HTTP_FILE, "", pch, 0 });
pch = strtok(NULL, "\n");
}
const uint32_t itemsAdded{ playList.size() - previousSize };
client->printf("%s\nAdded %i items to playlist", MESSAGE_HEADER, itemsAdded);
log_d("Added %i library items to playlist", itemsAdded);
if (!itemsAdded) return;
if (startnow || playList.currentItem() == PLAYLIST_STOPPED) {
playList.setCurrentItem(previousSize);
startItem(playList.currentItem());
}
upDatePlaylistOnClients();
}
else if (!strcmp("playitem", pch)) {
pch = strtok(NULL, "\n");
if (!pch) return;
const uint8_t index = atoi(pch);
if (index < playList.size()) {
playList.setCurrentItem(index);
startItem(playList.currentItem());
}
}
else if (!strcmp("deleteitem", pch)) {
pch = strtok(NULL, "\n");
if (!pch) return;
const uint8_t index = atoi(pch);
if (index >= playList.size()) return;
playList.remove(index);
// deleted item was before current item
if (index < playList.currentItem()) {
playList.setCurrentItem(playList.currentItem() - 1);
upDatePlaylistOnClients();
}
// deleted item was the current item
else if (playList.currentItem() == index) {
// play the next item if there is one
if (playList.currentItem() < playList.size()) {
upDatePlaylistOnClients();
startItem(playList.currentItem());
} else {
playlistHasEnded();
upDatePlaylistOnClients();
playerMessage msg;
msg.action = playerMessage::STOPSONG;
xQueueSend(playerQueue, &msg, portMAX_DELAY);
}
}
// deleted item was after current item
else {
upDatePlaylistOnClients();
}
}
else if (!strcmp("clearlist", pch)) {
if (!playList.size()) return;
playerMessage msg;
msg.action = playerMessage::STOPSONG;
xQueueSend(playerQueue, &msg, portMAX_DELAY);
playList.clear();
log_d("Playlist cleared");
playlistHasEnded();
upDatePlaylistOnClients();
}
else if (!strcmp("presetstation", pch) || !strcmp("_presetstation", pch)) {
const bool startnow = (pch[0] == '_');
pch = strtok(NULL, "\n");
if (!pch) return;
const uint32_t index = atoi(pch);
if (index >= NUMBER_OF_PRESETS) return;
const uint32_t previousSize = playList.size();
playList.add({ HTTP_PRESET, "", "", index });
if (playList.size() == previousSize) {
client->printf("%s\nCould not add '%s' to playlist", MESSAGE_HEADER, preset[index].name.c_str());
return;
}
log_d("Added '%s' to playlist", preset[index].name.c_str());
client->printf("%s\nAdded '%s' to playlist", MESSAGE_HEADER, preset[index].name.c_str());
if (startnow || playList.currentItem() == PLAYLIST_STOPPED) {
playList.setCurrentItem(playList.size() - 1);
startItem(playList.currentItem());
}
upDatePlaylistOnClients();
}
else if (!strcmp("jumptopos", pch)) {
pch = strtok(NULL, "\n");
if (!pch) return;
playerMessage msg;
msg.action = playerMessage::CONNECTTOHOST;
msg.value = atoi(pch);
snprintf(msg.url, sizeof(msg.url), playList.url(playList.currentItem()).c_str());
xQueueSend(playerQueue, &msg, portMAX_DELAY);
}
else if (!strcmp("currenttofavorites", pch)) {
pch = strtok(NULL, "\n");
if (!pch) return;
playListItem item;
playList.get(playList.currentItem(), item);
if (saveItemToFavorites(client, pch, item)) {
String s;
ws.textAll(favoritesToString(s));
}
}
else if (!strcmp("favoritetoplaylist", pch) || !strcmp("_favoritetoplaylist", pch)) {
const bool startNow = (pch[0] == '_');
pch = strtok(NULL, "\n");
if (!pch) return;
if (playList.size() == PLAYLIST_MAX_ITEMS) {
client->printf("%s\nCould not add '%s' to playlist!", MESSAGE_HEADER, pch);
return;
}
handleFavoriteToPlaylist(client, pch, startNow);
upDatePlaylistOnClients();
}
else if (!strcmp("deletefavorite", pch)) {
pch = strtok(NULL, "\n");
if (!pch) return;
char filename[strlen(FAVORITES_FOLDER) + strlen(pch) + 1];
snprintf(filename, sizeof(filename), "%s%s", FAVORITES_FOLDER, pch);
if (!FFat.remove(filename)) {
client->printf("%s\nCould not delete %s", MESSAGE_HEADER, pch);
} else {
String s;
ws.textAll(favoritesToString(s));
}
}
else if (!strcmp("foundlink", pch) || !strcmp("_foundlink", pch)) {
if (playList.size() == PLAYLIST_MAX_ITEMS) {
client->printf("%s\nCould not add new url to playlist", MESSAGE_HEADER);
return;
}
const char* url = strtok(NULL, "\n");
if (!url) return;
const char* name = strtok(NULL, "\n");
if (!name) return;
playList.add({ HTTP_FOUND, name, url, 0 });
upDatePlaylistOnClients();
const bool startnow = (pch[0] == '_');
if (startnow || playList.currentItem() == PLAYLIST_STOPPED) {
playList.setCurrentItem(playList.size() - 1);
startItem(playList.currentItem());
}
}
else {
log_i("unhandled single frame ws event! %s", pch);
}
}
void handleMultiFrame(AsyncWebSocketClient* client, uint8_t* data, size_t len, AwsFrameInfo* info) {
static String message;
message.concat(data, len); //todo: check result
if ((info->index + len) == info->len && info->final) {
log_d("Final multi frame message for %i bytes", info->index + len);
if (message.startsWith("_filetoplaylist") || message.startsWith("filetoplaylist")) {
const bool startnow = (message[0] == '_');
const uint32_t previousSize = playList.size();
auto pos = message.indexOf("\n");
if (-1 == pos) return;
pos++;
while (pos < info->len) {
char url[PLAYLIST_MAX_URL_LENGTH];
auto cnt = 0;
while (message.charAt(pos) != '\n') {
url[cnt++] = message.charAt(pos++);
if (pos == info->len) return;
}
url[cnt] = 0;
playList.add({ HTTP_FILE, "", url, 0 });
pos++;
}
const uint32_t itemsAdded{ playList.size() - previousSize };
client->printf("%s\nAdded %i items to playlist", MESSAGE_HEADER, itemsAdded);
log_d("Added %i items to playlist", itemsAdded);
if (itemsAdded && (startnow || playList.currentItem() == PLAYLIST_STOPPED)) {
playList.setCurrentItem(previousSize);
startItem(playList.currentItem());
}
if (itemsAdded) upDatePlaylistOnClients();
}
message.clear();
}
}