-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
270 lines (195 loc) · 6.46 KB
/
index.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
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
const { Control, Discovery } = require('magic-home');
const express = require("express");
const PORT = process.env.PORT || 3000;
let devices = [];
// Functions
const scanDevices = (timeout) => {
console.log(`Scanning for devices ... (${timeout}ms)`);
return Discovery.scan(timeout).then(d => {
devices = d;
const s = d.length == 1 ? "" : "s";
console.log(`Found ${d.length} device${s}`)
devices.forEach(element => {
console.log(element);
});
})
}
const getDevices = (id, address) => {
return devices.filter(device => (
(
id === undefined ||
device.id === id
) &&
(
address === undefined ||
device.address === address)
));
}
const hexToRgb = (hex) => {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
const newHex = hex.replace(shorthandRegex, function (m, r, g, b) {
return r + r + g + g + b + b;
});
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(newHex);
return result
? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
}
: null;
}
const onScan = () => {
scanDevices(60000).then(onScan, onScan);
}
// Scan devices and set interval to scan every minute
scanDevices(5000).then(onScan, onScan);
// Express
const app = express();
// Middleware
app.use(express.json());
app.use((err, req, res, next) => {
if (err) {
res.status(500).send(`Something went wrong! (${err})`);
} else {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
}
});
// Endpoints
// Color endpoint
app.post("/api/color", (req, res) => {
try {
const color = req.body.color;
const brightness = req.body.brightness ? req.body.brightness : 100;
const id = req.body.id;
const address = req.body.address;
if (typeof color === 'undefined') {
res.status('500').send(`'color' must be defined`);
return;
}
let r = 255, g = 255, b = 255;
if (typeof color === 'number') {
const c = req.body.color
r = Math.floor(c / (256 * 256));
g = Math.floor(c / 256) % 256;
b = c % 256;
} else if (typeof color === 'string') {
const rgb = hexToRgb(color);
if (rgb == null) throw "Couldn't parse color string"
r = rgb.r;
g = rgb.g;
b = rgb.b;
} else {
res.status('500').send(`'color' must be of type string (hex) or number (decimal)`);
return;
}
// Filter devices and loop over them to set color
const localDevices = getDevices(id, address)
const promises = [];
localDevices.forEach(device => {
const control = new Control(device.address, {
wait_for_reply: false
});
promises.push(control.setColorWithBrightness(r, g, b, brightness));
})
Promise.all(promises)
.then(() => res.sendStatus('200'))
.catch(err => res.status(500).send(err.message));
} catch (e) {
res.status(500).send(e);
}
});
// Power endpoint
app.post("/api/power", (req, res) => {
try {
const id = req.body.id;
const address = req.body.address;
const power = req.body.power;
if (typeof power !== 'boolean') {
res.status('500').send(`'power' must be a boolean`);
return;
}
// Filter devices and loop over them to set power
const localDevices = getDevices(id, address)
const promises = [];
localDevices.forEach(device => {
const control = new Control(device.address, {
wait_for_reply: false,
})
promises.push(control.setPower(power));
})
Promise.all(promises)
.then(() => res.sendStatus('200'))
.catch(err => res.status(500).send(err.message));
} catch (e) {
res.status(500).send(e);
}
});
//Test effects
app.post("/api/effect", (req, res) => {
try {
const id = req.body.id;
const address = req.body.address;
const effect = req.body.effect;
speed = req.body.speed;
if (typeof effect !== 'string') {
res.status('500').send(`'effect' must be a string`);
return;
}
if (typeof speed !== 'number') {
speed = 100;
}
// Filter devices and loop over them to ativate effet
const localDevices = getDevices(id, address)
const promises = [];
localDevices.forEach(device => {
const control = new Control(device.address, {
wait_for_reply: false,
})
promises.push(control.setPattern(effect, speed));
})
Promise.all(promises)
.then(() => res.sendStatus('200'))
.catch(err => {
res.status(500).send(err.message)
});
} catch (e) {
res.status(500).send(e);
}
});
// Devices endpoint
app.get("/api/devices", (req, res) => {
res.json(devices);
})
// Device endpoint
app.get("/api/device/:id", (req, res) => {
const id = req.params.id;
if (typeof id === 'undefined') {
res.status(500).send(`'id' must be defined`);
return;
}
const device = devices.find(element => element.id === id);
if (device === undefined) {
res.status(500).send(`No device with id ${id} was found`);
return;
}
new Control(device.address)
.queryState()
.then(state => res.json(state))
.catch(err => res.status(500).send(err));
})
// Start listening for requests
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});