-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (50 loc) · 1.71 KB
/
app.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
var chaconEmitter = require('./chaconEmitter');
var express = require('express');
var path = require('path');
var pin = 0
var app = express();
chaconEmitter.init();
function sendOnCommand(emitterId, deviceId) {
chaconEmitter.transmit(chaconEmitter.buildOrder(emitterId, deviceId, true));
}
function sendOffCommand(emitterId, deviceId) {
chaconEmitter.transmit(chaconEmitter.buildOrder(emitterId, deviceId, false));
}
function sendDimCommand(emitterId, deviceId, dimValue) {
chaconEmitter.transmit(chaconEmitter.buildDimOrder(emitterId, deviceId, dimValue), true);
}
app.get('/switch/:deviceId/:emitterId/:state', function (req, res) {
var deviceId = parseInt(req.params.deviceId);
var emitterId = parseInt(req.params.emitterId);
var state = req.params.state;
// console.log(req.route);
// console.log('deviceId:%s', deviceId);
// console.log('emitterId:%s', emitterId);
// console.log('state:%s', state);
if (state == 'on') {
sendOnCommand(emitterId, deviceId);
}
else if (state == 'off') {
sendOffCommand(emitterId, deviceId);
}
else if (state == 'dim') {
var dimValue = req.query.value;
if (dimValue == 'ON') {
sendOnCommand(emitterId, deviceId);
}
else if (dimValue == 'OFF') {
sendOffCommand(emitterId, deviceId);
}
else {
dimValue = parseInt(req.query.value);
// console.log('dimValue:%s', dimValue);
if (0 <= dimValue && dimValue <= 100) {
// console.log('dimming!');
sendDimCommand(emitterId, deviceId, dimValue);
}
}
}
res.status(200);
res.send();
});
app.listen(8080, '0.0.0.0');