-
Notifications
You must be signed in to change notification settings - Fork 2
/
tellduslive-list-all-devices.js
64 lines (50 loc) · 2.32 KB
/
tellduslive-list-all-devices.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
// Node-RED function to
// a) list all Telldus devices (device = everything in Telldus Live except sensors)
//
// The code below relies on the following modules to be installed in the Node-RED environment, including being set up
// in the functionGlobalContext section of the Node-RED settings.js config file (see last section of http://nodered.org/docs/writing-functions.html page)
//
// a) Telldus-Live module (https://github.com/TheThingSystem/node-telldus-live)
//
//
// The function will fire one output message for each device it gets information about from Telldus Live.
// By default the device id and name is included in the messages, but this can be customised as needed.
//
// Define Telldus Live API credentials
var publicKey = '<enter your public key here>'
, privateKey = '<enter your private key here>'
, token = '<enter your token here>'
, tokenSecret = '<enter your token secret here>'
, cloud
;
// Create and log into new TelldusAPI object
cloud = new context.global.telldusLive.TelldusAPI({ publicKey : publicKey
, privateKey : privateKey });
cloud.login(token, tokenSecret, function(err, user) {
if (!!err) return console.log('login error: ' + err.message);
// Get list of all devices. Use async call to avoid blocking
cloud.getDevices(function(err, devices) {
var f, i;
if (!!err) return console.log('getDevices: ' + err.message);
f = function(offset, p, s) {
return function(err, device) {
var d, type, types;
if (!!err) return console.log(s + ' id=' + p.id + ': ' + err.message);
var devInfo = 'Device list, id=' + device.id + ', name=' + device.name;
node.log (devInfo);
// Fire one output message for each pass of this function.
// An effect of this function being a callback (which is async by its nature) is that the devices
// will be returned in random order (i.e. not sorted from lowest to highest)
node.send({payload:devInfo});
return;
};
};
// Loop over all devices, asynchcronously get the data for each
for (i = 0; i < devices.length; i++) {
if (devices[i].type === 'device') cloud.getDeviceInfo(devices[i], f(i, devices[i], 'getDeviceInfo'));
}
});
}).on('error', function(err) {
console.log('background error: ' + err.message);
});
return;