-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (49 loc) · 1.19 KB
/
app.js
File metadata and controls
59 lines (49 loc) · 1.19 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
const express = require("express");
const app = express();
const axios = require("axios");
app.use(express.static("public"));
app.use(express.json());
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});
app.get("/api/lights", async function (req, res) {
const { ip, username } = req.query;
try {
const bridgeResult = await axios.get(`http://${ip}/api/${username}/lights`);
if (
bridgeResult.data &&
bridgeResult.data.length &&
bridgeResult.data[0].error
) {
throw new Error(bridgeResult.data[0].error.description);
}
return res.json({
success: true,
data: bridgeResult.data,
});
} catch (e) {
console.error(e);
return res.json({
success: false,
});
}
});
app.put("/api/lights", async function (req, res) {
const { ids, hue, ip, username } = req.body;
for (let id of ids) {
try {
await axios.put(`http://${ip}/api/${username}/lights/${id}/state`, {
on: true,
hue,
sat: 254,
bri: 254,
});
} catch (e) {
console.error(e);
}
}
return res.end();
});
app.listen(3030, function () {
console.log("Listening on port 3030");
});