-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNanoleaf.js
53 lines (45 loc) · 1.26 KB
/
Nanoleaf.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
class Nanoleaf {
constructor(host, token) {
this.api_url = `http://${host}/api/v1/${token}`
}
async isOn() {
const res = await fetch(`${this.api_url}/state`)
const data = await res.json()
return data.on.value
}
async panels() {
const panels = await this.panelData()
// Create a 2D array to represent the layout
const layout = panels.positionData.reduce((acc, panel) => {
const row = Math.floor(panel.y / panels.sideLength);
const col = Math.floor(panel.x / panels.sideLength);
acc[row] = acc[row] || [];
acc[row][col] = panel.panelId;
return acc;
}, []);
// Convert the layout array to a string representation
return layout.map(row => row ? row.reverse().join(' ') : '').slice(1)
}
async panelData() {
const res = await fetch(`${this.api_url}/panelLayout/layout`)
return res.json()
}
effect(data) {
return {
'write': {
'command': 'display',
'version': '2.0',
'animType': 'static',
'animData': data,
'palette': [],
'colorType': 'HSB'
}
}
}
async sendEffect(data) {
return fetch(`${this.api_url}/effects`, {
method: 'PUT', body: JSON.stringify(this.effect(data)),
})
}
}
module.exports = Nanoleaf