forked from scratchfoundation/scratch-vm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwemo_server.js
More file actions
86 lines (74 loc) · 1.96 KB
/
Copy pathwemo_server.js
File metadata and controls
86 lines (74 loc) · 1.96 KB
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
var express = require('express')
var app = express();
var Wemo = require('wemo-client');
var wemo = new Wemo();
var devices = [];
var client = undefined;
var cors = require('cors');
app.use(cors())
setInterval(function() {
//wemo.discover(deviceDiscovered);
wemo.discover(function(err, deviceInfo) {
console.log('Wemo Device Found: %j', deviceInfo);
// Get the client for the found device
client = wemo.client(deviceInfo);
// You definitely want to listen to error events (e.g. device went offline),
// Node will throw them as an exception if they are left unhandled
client.on('error', function(err) {
console.log('Error: %s', err.code);
});
// Handle BinaryState events
client.on('binaryState', function(value) {
console.log('Binary State changed to: %s', value);
});
// Turn the switch on
devices.push(client);
});
}, 3000);
function deviceDiscovered(info) {
for (var i=0; i<devices.length; i++) {
if (devices[i].serialNumber == info.serialNumber)
return
}
console.log("New device found: " + info.serialNumber);
devices.push(info);
client = wemo.client(info);
}
app.get('/search', function (req, res) {
if(devices.length == 0) {
console.log("No devices found");
return;
} else {
res.send(JSON.stringify(devices));
}
});
app.get('/connect/:index', function (req, res) {
var index = req.params.index;
console.log(index);
if(index >= 0 && index < devices.length) {
client = wemo.client(devices[index]);
//setListeners(client);
res.send("connected");
}
});
app.get('/on', function (req, res) {
if(client == undefined) {
console.log("No client set");
return;
} else {
client.setBinaryState(1);
res.send("on");
}
});
app.get('/off', function (req, res) {
if(client == undefined) {
console.log("No client set");
return;
} else {
client.setBinaryState(0);
res.send("off");
}
});
app.listen(3000, function () {
console.log("Server running on localhost:3000");
});