-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwemo.js
60 lines (54 loc) · 1.55 KB
/
wemo.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
var WeMo = require('wemo');
var switchName = process.env.RADAR_SWITCH_NAME; // 'GarageSwitch1';
function getSwitch (action) {
WeMo.Search(switchName, function(err, device) {
var wemoSwitch = new WeMo(device.ip, device.port);
console.log(wemoSwitch);
if (err) {
console.error(`Cannot get switch: ${err}`);
return;
}
if (action === 'on') {
turnOn(wemoSwitch);
}
if (action === 'off') {
turnOff(wemoSwitch);
}
});
}
function turnOn (wemoSwitch) {
wemoSwitch.getBinaryState(function(err, result) {
console.log(err, result);
if (err) {
console.error(err);
}
if (parseInt(result) === 0) {
wemoSwitch.setBinaryState(1, function(err, result) {
if (err) {
console.error(err);
}
return console.log('Switch turned on');
});
}
console.log('Switch is on');
});
}
function turnOff (wemoSwitch) {
wemoSwitch.getBinaryState(function(err, result) {
console.log(err, result);
if (err) {
console.error(err);
}
if (parseInt(result) === 1) {
wemoSwitch.setBinaryState(0, function(err, result) {
if (err) {
console.error(err);
}
return console.log('Switch turned off');
});
}
console.log('Switch is off');
});
}
// getSwitch('off');
module.exports = getSwitch;