Repository navigation
Expand file tree
/
Copy pathremote.cpp
More file actions
74 lines (62 loc) · 1.66 KB
/
Copy pathremote.cpp
File metadata and controls
74 lines (62 loc) · 1.66 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
#include "remote.h"
Remote::Remote(
Modekeeper& modekeeper)
: webServer(80),
modekeeper(modekeeper)
{
}
void Remote::Setup() {
websocketServer.listen(8080);
SPIFFS.begin();
webServer.onNotFound([this]() {
if (!Remote::HandleFileRead(webServer.uri())) {
webServer.send(404, "text/plain", "404: Not Found");
}
});
webServer.begin();
}
bool Remote::HandleFileRead(String path) {
if (path.endsWith("/")) {
path += "index.htm";
}
String contentType = "text/html";
if (SPIFFS.exists(path)) {
File file = SPIFFS.open(path, "r");
size_t sent = webServer.streamFile(file, contentType);
file.close();
return true;
}
return false;
}
void Remote::HandleData(int8_t c) {
switch (c) {
case '1':
modekeeper.SetMode(Modekeeper::Mode::Clock);
break;
case '2':
modekeeper.SetMode(Modekeeper::Mode::SingleColor);
break;
case '3':
modekeeper.SetMode(Modekeeper::Mode::Noise);
break;
}
}
void Remote::OnMessage(WebsocketsClient& client, WebsocketsMessage& message) {
HandleData(message.rawData()[0]);
client.send("Echo: " + message.data());
}
void Remote::PollAllWsClients() {
for (auto& client : websocketClients) {
client.poll();
}
}
void Remote::Handle() {
// Websocket
if (websocketServer.poll()) {
auto ws_client = websocketServer.accept();
ws_client.onMessage([this](WebsocketsClient& client, WebsocketsMessage message) { OnMessage(client, message); });
websocketClients.push_back(ws_client);
}
PollAllWsClients();
webServer.handleClient();
}