-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhelpers.js
More file actions
executable file
·55 lines (46 loc) · 1.42 KB
/
helpers.js
File metadata and controls
executable file
·55 lines (46 loc) · 1.42 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
var Orvibo = require("node-orvibo");
var o = new Orvibo();
var devices = {};
var discoveryTimer = [];
var subscribeTimer = [];
var stateTimer = [];
o.listen(function () {
discoveryTimer = setInterval(function () {
o.discover();
}, 1000);
});
o.on("deviceadded", function (device) {
clearInterval(discoveryTimer);
o.discover();
if (typeof devices[device.macAddress] === 'undefined') {
subscribeTimer[device.macAddress] = setInterval(function() {
o.subscribe(device)
}, 1000);
} else {
devices[device.macAddress].state = device.state;
}
});
o.on('subscribed', function(device) {
clearInterval(subscribeTimer[device.macAddress]);
devices[device.macAddress] = device;
});
o.on('setstate', function(device, state) {
devices[device.macAddress].state = device;
clearInterval(stateTimer[device.macAddress]);
});
exports.changeState = function(params, callback) {
if (typeof devices[params.mac] === 'defined') {
var state = (params.state == 'on');
stateTimer[params.mac] = setInterval(function() {
o.setState({device: devices[params.mac], state: state});
}, 1000);
}
callback();
};
exports.getDevices = function(callback) {
callback(devices);
};
exports.queryDevice = function(params, callback) {
var device = (typeof devices[params.mac] !== 'undefined') ? devices[params.mac] : false;
callback(device);
};