-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (39 loc) · 1.18 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
"use strict";
const express = require("express");
const avea = require("avea_bulb");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }))
app.get("/", function (req, res) {
res.send("Hello World!");
});
app.listen(3000, function () {
console.log("Example app listening on port 3000!");
});
const router = express.Router();
const registry = new avea.Registry();
registry.discover();
router.route("/bulbs")
.get((req, res) => {
res.json({bulbs: registry.all()});
});
router.route("/bulbs/:id")
.get((req, res) => {
res.json(registry.getBulb(req.params.id));
})
.post((req, res) => {
const color = req.body.color;
if (undefined !== color) {
const rgb = color.split(",");
registry.setColor(req.params.id, new avea.Color(rgb[0], rgb[1], rgb[2], rgb[3]));
}
const brightness = req.body.brightness;
if (undefined !== brightness) {
registry.setBrightness(req.params.id, brightness);
}
registry.update(req.params.id).then(state => {
res.json(state);
});
})
;
app.use("/api", router);