-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
124 lines (102 loc) · 3.02 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
// Web app requires
var express = require('express');
var https = require('https');
var fs = require('fs');
var app = express();
// get configurations from config.js file
var config = require ('./config.js');
// Set up basic auth
// Wemo requires
var UpnpControlPoint = require("./lib/upnp-controlpoint").UpnpControlPoint;
var wemo = require("./lib/wemo");
// hold a reference to the Wemo switch
var wemoSwitch;
var currentBinaryState;
var lastCommandDate = null;
// how many milliseconds the server waits to send a signal to the switch (to protect whatever is plugged into it)
var maxInterval = 500;
function validateLastCommandTiming() {
if (lastCommandDate === null) {
lastCommandDate = new Date();
return true;
}
if ((new Date()) - lastCommandDate > maxInterval) {
lastCommandDate = new Date();
return true;
}
return false;
};
function sendWaitValidationResponse(res) {
res.send(409, 'You must wait ' + maxInterval.toString() + ' ms between requests.');
}
function sendNoWemoResponse(res) {
res.send(500, 'No Wemo switch detected.');
}
// Set up basic HTTP auth
app.use(express.basicAuth(function(username, password) {
return username === config.username && password === config.password;
}));
// Set up API URLs
app.get('/on', function(req, res) {
if (wemoSwitch) {
if (validateLastCommandTiming()) {
wemoSwitch.setBinaryState(true);
res.send(200, 'Wemo switch turned on.');
} else {
sendWaitValidationResponse(res);
}
} else {
sendNoWemoResponse(res);
}
});
app.get('/off', function(req, res) {
if (wemoSwitch) {
if (validateLastCommandTiming()) {
wemoSwitch.setBinaryState(false);
res.send(200, 'Wemo switch turned off.');
} else {
sendWaitValidationResponse(res);
}
} else {
sendNoWemoResponse(res);
}
});
app.get('/toggle', function(req, res) {
if (wemoSwitch) {
if (validateLastCommandTiming()) {
wemoSwitch.setBinaryState(!currentBinaryState);
res.send(200, 'Wemo switch toggled.');
} else {
sendWaitValidationResponse(res);
}
} else {
sendNoWemoResponse(res);
}
});
// set up URLs to handle API
var handleDevice = function(device) {
if (device.deviceType === wemo.WemoControllee.deviceType) {
wemoSwitch = new wemo.WemoControllee(device);
wemoSwitch.on('BinaryState', function(stateValue) {
currentBinaryState = stateValue;
});
console.log('Wemo Switch found!');
}
};
// UPNP control point (used to search for the Wemo switch)
var cp = new UpnpControlPoint();
// When a device is found, call handleDevice callback, which looks for the Wemo switch
cp.on("device", handleDevice);
// Start the search
cp.search();
// Start the web server
var port = process.env.PORT || 3000;
var privateKey = fs.readFileSync(config.privateKey).toString();
var certificate = fs.readFileSync(config.certificate).toString();
var options = {
key: privateKey,
cert: certificate
};
https.createServer(options, app).listen(port, function() {
console.log('Express app listening on port ' + port);
});