-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
181 lines (153 loc) · 3.58 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
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
import { Env, loadEnv } from "@hedia/env";
const BLUE = [51, 51, 255];
const PINK = [255, 51, 153];
main().catch((err) => console.error(err));
async function main() {
await loadEnv();
const nanoleafBaseUrl = Env.getURL("NANOLEAF_BASE_URL");
const nanoleafAuthToken = Env.getString("NANOLEAF_AUTH_TOKEN");
const apps = {
"hedia-com-oauth2": {
panelId: 51890,
count: 0,
color: BLUE,
},
"hedia-com-developer": {
panelId: 34924,
count: 0,
color: BLUE,
},
"hedia-com-id": {
panelId: 26826,
count: 0,
color: BLUE,
},
"hedia-com-data": {
panelId: 50223,
count: 0,
color: BLUE,
},
"hedia-com-event": {
panelId: 61943,
count: 0,
color: BLUE,
},
"hedia-com-webhook": {
panelId: 62639,
count: 0,
color: BLUE,
},
};
try {
const eventStream = fetchEventStream("https://scalingo.hedia.org/counters");
for await (const event of eventStream) {
try {
const counters = JSON.parse(event.data);
for (const appname in apps) {
const app = apps[appname];
const newCount = counters?.app?.[appname]?.count ?? 0;
const diff = newCount - app.count;
if (diff > 0) {
console.log(`\nApp ${appname} was ${app.count}, now ${newCount}`);
app.count = newCount;
}
const color = getColor(diff);
if (color !== app.color) {
console.log(`Setting ${appname} color to ${color}`);
try {
await setPanelColor(app.panelId, color, nanoleafBaseUrl, nanoleafAuthToken);
app.color = color;
} catch (err) {
console.error("Error setting panel color:", err);
sleep(1000);
}
}
}
} catch (err) {
console.error("Error parsing event data:", err);
sleep(1000);
}
}
} catch (err) {
console.error("Error fetching event stream:", err);
sleep(1000);
}
}
async function* fetchEventStream(url) {
const response = await fetch(url);
const reader = response.body.getReader({});
const decoder = new TextDecoder("utf-8");
let message = "";
while (true) {
const { value, done } = await reader.read();
if (done) {
break;
}
const text = decoder.decode(value);
if (typeof text !== "string") {
continue;
}
message += text;
if (message.endsWith("\n\n")) {
yield parseValue(message);
message = "";
}
}
function parseValue(value) {
if (typeof value !== "string") {
return null;
}
const lines = value.split("\n");
return lines
.map((line) => splitOnFirst(line, ": "))
.reduce((event, [key, value]) => {
if (key === "event" || key === "data") {
event[key] = value;
}
return event;
}, {});
}
function splitOnFirst(value, separator) {
const index = value.indexOf(separator);
return [value.slice(0, index), value.slice(index + separator.length)];
}
}
function getColor(count) {
if (count === 0) {
return BLUE;
} else {
return PINK;
}
}
async function setPanelColor(panelId, [red, green, blue], nanoleafBaseUrl, nanoleafAuthToken) {
await writeEffect(
{
command: "display",
animType: "static",
animData: `1 ${panelId} 1 ${red} ${green} ${blue} 0 0`,
loop: false,
palette: [],
transTime: {
minValue: 50,
maxValue: 100,
},
},
nanoleafBaseUrl,
nanoleafAuthToken,
);
}
async function writeEffect(effect, nanoleafBaseUrl, nanoleafAuthToken) {
const input = new URL(`/api/v1/${nanoleafAuthToken}/effects`, nanoleafBaseUrl);
await fetch(input, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
write: effect,
}),
});
}
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}