-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest4.js
49 lines (39 loc) · 1.37 KB
/
test4.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
var ClientAPI = require('./taas-client')
, util = require('util')
;
new ClientAPI.ClientAPI({ steward: { name: '127.0.0.1' } }).on('open', function(channel, loginP) {
if (loginP) throw new Error('script should be run locally');
}).on('ready', function(channel, data) {
if (channel === 'management') return getToWork(this);
if (data.indexOf('read') !== -1) throw new Error('script requires write access');
}).on('close', function(channel) {
console.log(channel + ' close');
process.exit(1);
}).on('error', function(err, channel) {
console.log(channel + ' error: ' + err.message);
process.exit(1);
});
var getToWork = function(client) {
var actor, count, deviceID, entry;
var spoke = function(entry) {
return function(response) {
console.log(entry.whoami + ' responds:');
console.log(util.inspect(response, { depth: null }));
done();
};
};
var done = function() {
if (--count < 1) process.exit(0);
};
count = 1;
for (actor in client.actors) {
if (!client.actors.hasOwnProperty(actor)) continue;
entry = client.actors[actor];
if (entry.whatami.indexOf('/device/sensor/macguffin/sound') !== 0) continue;
deviceID = entry.whoami.split('/')[1];
console.log('telling ' + entry.whoami + ' to speak.');
count++;
client.performDevice(deviceID, 'speak', 'hello world.', spoke(entry));
}
done();
};