-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (128 loc) · 3.64 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
console.log("Yeelight FrontEnd Server starting!");
// Modules
const express = require("express");
const debug = require("debug")("yilai");
const path = require("path");
const yilaiConf = require(path.join(__dirname, "config"));
// Yeelight
const YeelightSearch = require("yeelight-wifi");
const yeelightSearch = new YeelightSearch();
var app = express();
if (yilaiConf.global.cors) {
// Allow CORS
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Content-Type, Authorization, Origin, Accept, Content-Length, X-Requested-With, Access-Control-Allow-Origin"
);
// Intercepts OPTIONS method
if ("OPTIONS" === req.method) {
//respond with 200
res.sendStatus(200);
} else {
next();
}
});
}
app.use(express.static("public"));
app.get("/", (req, res) => {
yeelightSearch.refresh();
res.send({ result: "ok" });
});
// Routes
require("./routes/light")(app, yeelightSearch);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error("Not Found");
err.status = 404;
next(err);
});
yeelightSearch.on("found", lightBulb => {
var yeeid = lightBulb.getId();
var yeemodel = lightBulb.getModel();
var yeename = lightBulb.getName();
debug("Found Yeelight ID: %s Model: %s Name: %s", yeeid, yeemodel, yeename);
lightBulb
.getValues(
"power",
"bright",
"rgb",
"color_mode",
"hue",
"sat",
"ct",
"flowing",
"delayoff",
"flow_params",
"music_on"
)
.then(reqid => {
lightBulb.once("response", (id, result) => {
if (reqid === id) {
lightBulb["power"] = result[0];
lightBulb["bright"] = result[1];
lightBulb["rgb"] = result[2];
lightBulb["color_mode"] = result[3];
lightBulb["hue"] = result[4];
lightBulb["sat"] = result[5];
lightBulb["ct"] = result[6];
lightBulb["flowing"] = result[7];
lightBulb["delayoff"] = result[8];
lightBulb["flow_params"] = result[9];
lightBulb["music_on"] = result[10];
debug(
`power ${result[0]} and brightness ${result[1]} for request ${id}`
);
}
});
});
lightBulb.on("notifcation", json => {
//debug(`Async notification: %o`, json);
if (json.method == "props" && json.params) {
for (var property in json.params) {
if (json.params.hasOwnProperty(property)) {
debug(
`Light ${lightBulb.id} ${property} is ${json.params[property]}`
);
lightBulb[property] = json.params[property];
}
}
}
});
});
function lightsTimer() {
debug("Periodic Lights search");
yeelightSearch.refresh();
}
function refreshTimer() {
debug("Refresh Lights data");
yeelightSearch.getYeelights().forEach(function(light) {
light.getValues(
"power",
"bright",
"rgb",
"color_mode",
"hue",
"sat",
"ct",
"flowing",
"delayoff",
"flow_params",
"music_on"
);
});
}
setInterval(lightsTimer, yilaiConf.yilai.periodic_search * 1000);
process.on("uncaughtException", function(err) {
console.error(new Date().toUTCString() + " uncaughtException:", err.message);
console.error(err.stack);
process.exit(1);
});
const PORT = process.env.PORT || 8000;
var server = app.listen(PORT, function() {
var host = server.address().address;
var port = server.address().port;
console.log("Yeelight Server listening at http://[%s]:%d", host, port);
});