forked from bencevans/node-sonos
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlogicalDevice.js
More file actions
126 lines (94 loc) · 3.42 KB
/
logicalDevice.js
File metadata and controls
126 lines (94 loc) · 3.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var async = require('async'),
Sonos = require('./sonos').Sonos,
sonosSearch = require('./sonos').search,
util = require('util'),
url = require('url');
function LogicalDevice(devices, coordinator, groupId) {
if (devices.length === 0) {
throw new Error('Logical device must be initialized with at least one device (' + devices.length + ' given)');
}
var coordinatorDevice = coordinator || devices[0];
Sonos.call(this, coordinatorDevice.host, coordinatorDevice.port);
var sonosDevices = devices.map(function(device) {
return new Sonos(device.host, device.post);
});
this.devices = sonosDevices;
this.groupId = groupId;
}
util.inherits(LogicalDevice, Sonos);
LogicalDevice.prototype.initialize = function(cb) {
async.forEach(this.devices, function(device, done) {
device.initialize(done);
}, cb);
};
LogicalDevice.prototype.destroy = function(cb) {
async.forEach(this.devices, function(device, done) {
device.destroy(done);
}, cb);
};
LogicalDevice.prototype.setVolume = function(volume, cb) {
this.getVolume(function(oldVolume) {
var diff = volume - oldVolume;
async.forEach(this.devices, function(device, done) {
var oldDeviceVolume = device.state.volume;
var newDeviceVolume = oldDeviceVolume + diff;
newDeviceVolume = Math.max(newDeviceVolume, 0);
newDeviceVolume = Math.min(newDeviceVolume, 100);
device.setVolume(newDeviceVolume, done);
}, cb);
}.bind(this));
};
LogicalDevice.prototype.getVolume = function(cb) {
var sum = 0;
this.devices.forEach(function(device) {
sum += device.state.volume || 0;
});
cb(sum / this.devices.length);
};
/**
* Create a Search Instance (emits 'DeviceAvailable' with a found Logical Sonos Component)
* @param {Function} Optional 'DeviceAvailable' listener (sonos)
* @return {Search/EventEmitter Instance}
*/
var search = function(callback) {
var search = sonosSearch();
search.once('DeviceAvailable', function(device) {
device.getTopology(function(err, topology) {
if (err) callback(err);
else {
var devices = topology.zones;
var groups = {};
// bucket devices by groupid
devices.forEach(function(device) {
if (device.coordinator === 'false' || device.name === 'BRIDGE' || device.name === 'BOOST') return; // devices to ignore in search
if (!groups[device.group]) groups[device.group] = { members: [] };
var parsedLocation = url.parse(device.location);
var sonosDevice = new Sonos(parsedLocation.hostname, parsedLocation.port);
if (device.coordinator === 'true') groups[device.group].coordinator = sonosDevice;
groups[device.group].members.push(sonosDevice);
});
// initialze all of the logical devices brefore callback
var logicalDevices = Object.keys(groups).map(function(groupId) {
var group = groups[groupId];
return new LogicalDevice(group.members, group.coordinator, groupId);
});
async.forEach(logicalDevices, function(device, done) {
device.initialize(function(err) {
if (err) done(err);
else {
done(null);
}
});
}, function(err) {
if (err) callback(err);
else {
callback(null, logicalDevices);
}
});
}
});
});
return search;
};
module.exports = LogicalDevice;
module.exports.search = search;