-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeviceInfo.js
105 lines (91 loc) · 2.8 KB
/
deviceInfo.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
103
104
105
var Sonos = require('sonos');
var _ = require('underscore');
// Functions to process device information
function getBridges (deviceList) {
var bridges = []
deviceList.forEach(function (device) {
if (device.CurrentZoneName === 'BRIDGE' && bridges.indexOf(device.ip + ':' + device.port) === -1) {
bridges.push(device.ip + ':' + device.port)
}
})
return bridges
}
function getBridgeDevices (deviceList) {
var bridgeDevices = []
deviceList.forEach(function (device) {
if (device.CurrentZoneName === 'BRIDGE') {
bridgeDevices.push(device)
}
})
return bridgeDevices
}
function getZones (deviceList) {
var zones = []
deviceList.forEach(function (device) {
if (zones.indexOf(device.CurrentZoneName) === -1 && device.CurrentZoneName !== 'BRIDGE') {
zones.push(device.CurrentZoneName)
}
})
return zones
}
function getZoneDevices (zone, deviceList) {
var zoneDevices = []
deviceList.forEach(function (device) {
if (device.CurrentZoneName === zone) {
zoneDevices.push(device)
}
})
return zoneDevices
}
function getZoneCoordinator (zone, deviceList) {
var coordinator
deviceList.forEach(function (device) {
if (device.CurrentZoneName === zone && device.coordinator === 'true') {
coordinator = device
}
})
return coordinator
}
// Display device information in structured form
module.exports = {
dumpInfo: function (devices, callback) {
var result = '';
result += '\nBridges:\n--------\n';
getBridges(devices).forEach(function (bridge) {
result += bridge;
getBridgeDevices(devices).forEach(function (device) {
result += '\t' + JSON.stringify(device);
})
})
result += '\nZones (coordinator):\n--------------------\n';
getZones(devices).forEach(function (zone) {
var coordinator = getZoneCoordinator(zone, devices);
if (coordinator !== undefined) {
result += '\n' + zone + ' (' + coordinator.ip + ':' + coordinator.port + ')\n';
}
getZoneDevices(zone, devices).forEach(function (device) {
result += JSON.stringify(device) + '\n';
})
})
return callback(result);
},
nowPlaying: function(devices, callback) {
getZones(devices).forEach(function (zone, track) {
var coordinator = getZoneCoordinator(zone, devices);
if (coordinator !== undefined) {
console.log('Getting info from ', coordinator);
var player = new Sonos.Sonos(coordinator.ip);
player.currentTrack(function (err, track) {
//if (err) throw err;
console.log(zone, track || 'Nothing Playing');
var result = {};
result.zone = zone;
result.track = track;
return callback(result);
})
}
})
},
findZone: function(devices, callback) {
}
}