-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
154 lines (138 loc) · 3.5 KB
/
main.js
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
import Timer from "timer";
import Net from "net";
import MDNS from "mdns";
import { RGB } from "rgb-led";
import { HTTPService } from "http-service";
const led = new RGB([12, 13, 14]);
let hostName = "on-air";
trace("Setup complete!\n");
trace(Net.get("SSID") + "\n");
trace(Net.get("IP") + "\n");
const serviceReady = new Promise((resolve) => {
new MDNS({ hostName }, function (message, value) {
if (message === MDNS.hostName) {
if (value) {
hostName = value;
this.add({ name: "onair", protocol: "tcp", port: 80, txt: {} });
trace(`${hostName} hostname claimed!\n`);
resolve();
} else {
trace(`Working on mdns claim\n`);
}
}
});
});
serviceReady.then(() => {
led.green();
Timer.delay(3000);
led.off();
});
const service = new HTTPService();
service.onError(() => {
throw new Error("ServerError");
});
service.get("/api/state", () => {
return {
body: String(led.currentColor),
};
});
service.post("/api/toggle", () => {
led.toggle();
});
service.post("/api/color", (request) => {
try {
const { color } = request.body;
if (led[color] !== undefined) {
led[color]();
} else if (Array.isArray(color)) {
led.setColor(color);
} else {
throw new Error("Invalid color value");
}
return { status: 204 };
} catch (error) {
trace(`Unable to parse ${request.body}\n`);
return { status: 422, body: error.toString() };
}
});
function isSelected(color, currentColor) {
switch (color) {
case "red":
if (currentColor[0] === 0) return "selected";
case "green":
if (currentColor[1] === 0) return "selected";
case "blue":
if (currentColor[2] === 0) return "selected";
default:
return "";
}
}
const indexHandler = (_request) => {
const body = `
<!DOCTYPE html>
<html lang="en">
<head>
<title>On-air light control</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: sans-serif;
padding: 2rem;
}
label, input {
display: block;
}
input, form { margin-bottom: 1rem; }
</style>
</head>
<body>
<h1>On-air light control</h1>
<form action="toggle" method="post">
<button type="submit">Toggle light</button>
</form>
<form action="color" method="post">
<label id="color-select-label" htmlFor="color">Select a color</label>
<select id="color" name="color" onchange="this.form.submit()">
<option ${isSelected("red", led.currentColor)}>red</option>
<option ${isSelected("green", led.currentColor)}>green</option>
<option ${isSelected("blue", led.currentColor)}>blue</option>
</select>
</form>
</body>
</html>
`;
return { headers: ["Content-type", "text/html"], body };
};
service.get("/", indexHandler);
service.post("/toggle", () => {
led.toggle();
return {
status: 303,
headers: ["Location", "/"],
};
});
service.post("/color", (request) => {
trace(`Setting color: ${request.body}\n`);
try {
const { color } = request.body;
if (led[color] !== undefined) {
led[color]();
} else if (Array.isArray(color)) {
led.setColor(color);
} else {
throw new Error("Invalid color value");
}
return {
status: 303,
headers: ["Location", "/"],
};
} catch (error) {
trace(`Unable to parse ${request.body}\n`);
return { status: 422, body: error.toString() };
}
});
/**
TODO:
- more refinement of color control
**/