-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.js
102 lines (83 loc) · 3.87 KB
/
test.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
/* jshint asi: true, node: true, laxbreak: true, laxcomma: true, undef: true, unused: true */
var TelldusAPI = require('./telldus-live');
var secrets = require('./secrets').MySecrets;
var publicKey = secrets.publicKey?secrets.publicKey:'...'
, privateKey = secrets.privateKey?secrets.privateKey:'...'
, token = secrets.token?secrets.token:'...'
, tokenSecret = secrets.tokenSecret?secrets.tokenSecret:'...'
, cloud
;
cloud = new TelldusAPI.TelldusAPI({ publicKey : publicKey
, privateKey : privateKey }).login(token, tokenSecret, function(err, user) {
if (!!err) return console.log('login error: ' + err.message);
console.log('user: '); console.log(user); console.log(''); console.log('');
cloud.getSensors(function(err, sensors) {
var f, i;
if (!!err) return console.log('getSensors: ' + err.message);
f = function(offset, p, s) {
return function(err, sensor) {
var i, prop, props, type;
if (!!err) return console.log(s + ' id=' + p.id + ': ' + err.message);
console.log('sensor #' + offset + ' ' + s + ': '); console.log(sensor);
props = { temp : [ 'temperature', 'celcius', 'meteo' ]
, humidity : [ 'humidity', 'percentage', 'meteo' ]
};
type = null;
for (i = 0; i < sensor.data.length; i++) {
type = props[sensor.data[i].name];
if (!!type) break;
}
if (!type) return;
console.log('/device/climate/' + (sensor.protocol || 'telldus') + '/' + type[2]);
console.log(' uuid=teldus:' + sensor.id);
console.log(' name: ' + sensor.name);
console.log(' status: ' + (p.online === '1' ? 'present' : 'absent'));
console.log(' lastSample: ' + sensor.lastUpdated * 1000);
console.log(' info:');
for (i = 0; i < sensor.data.length; i++) {
prop = props[sensor.data[i].name];
if (prop) console.log(' ' + prop[0] + ': "' + prop[1] + '"');
}
console.log(' values:');
for (i = 0; i < sensor.data.length; i++) {
prop = props[sensor.data[i].name];
if (prop) console.log(' ' + prop[0] + ': ' + sensor.data[i].value);
}
console.log('');
};
};
for (i = 0; i < sensors.length; i++) cloud.getSensorInfo(sensors[i], f(i, sensors[i], 'getSensorInfo'));
}).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);
console.log('device #' + offset + ' ' + s + ': '); console.log(device);
types = { 'selflearning-switch' : 'onoff'
, 'selflearning-dimmer' : 'dimmer'
, 'codeswitch' : 'onoff' };
type = null;
d = device.model.split(':');
type = types[d[0]];
if (!type) return;
console.log('/device/switch' + '/' + (d[d.length - 1] || 'telldus') + '/' + type);
console.log(' uuid=teldus:' + device.id);
console.log(' perform: off, on');
console.log(' name: ' + device.name);
console.log(' status: ' + (device.online === '0' ? 'absent' : device.status));
console.log(' info:');
if (type === 'dimmer') console.log(' dimmer: percentage');
console.log(' values:');
if (type === 'dimmer') console.log(' dimmer: ' + Math.round((1-(255 - device.statevalue)/255)*100) + '%');
console.log('');
};
};
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);
});