-
Notifications
You must be signed in to change notification settings - Fork 47
/
sonos.js
172 lines (145 loc) · 4.79 KB
/
sonos.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { DeviceDiscovery, Sonos, Helpers, Services } from 'sonos';
import request from 'axios';
import _ from 'lodash';
const TUNEIN_ID = 65031;
class ContentDirectoryEnhanced extends Services.ContentDirectory {
_enumItems(resultcontainer) {
if (resultcontainer === undefined) {
return;
}
if (!Array.isArray(resultcontainer)) {
resultcontainer = [resultcontainer];
}
const convertItem = function (item) {
const res = Helpers.ParseDIDLItem(
item,
this.host,
this.port,
_.get(item, 'res._')
);
return {
...res,
class: item['upnp:class'],
_raw: {
...item,
},
};
}.bind(this);
return resultcontainer.map(convertItem);
}
}
class SonosEnhanced extends Sonos {
musicServices() {
return new Services.MusicServices(this.host, this.port);
}
contentDirectoryService() {
return new ContentDirectoryEnhanced(this.host, this.port);
}
async getZPInfo() {
const data = await request(
'http://' + this.host + ':' + this.port + '/status/zp'
)
.then((response) => response.data)
.then(Helpers.ParseXml);
return data.ZPSupportInfo.ZPInfo;
}
async getAvailableServices() {
const data = await this.musicServices().ListAvailableServices();
const servicesObj = await Helpers.ParseXml(
data.AvailableServiceDescriptorList
);
console.log(servicesObj);
const serviceDescriptors = servicesObj.Services.Service.map((obj) => {
const stringsUri = _.get(obj, 'Presentation.Strings.Uri');
const mapUri = _.get(obj, 'Presentation.PresentationMap.Uri');
const manifestUri = _.get(obj, 'Manifest.Uri');
return _.assign({}, obj, obj.Policy, {
manifestUri,
presentation: {
stringsUri,
mapUri,
},
});
});
const services = [];
[TUNEIN_ID, ...data.AvailableServiceTypeList.split(',')].forEach(
async (t) => {
const serviceId =
Math.floor(Math.abs((t - 7) / 256)) || Number(t);
const match = _.find(serviceDescriptors, {
Id: String(serviceId),
});
if (match) {
match.ServiceIDEncoded = Number(t);
services.push(match);
}
}
);
return services;
}
async queryMusicLibrary(
searchType,
searchTerm,
requestOptions = {},
separator = ':'
) {
const searchTypes = {
artists: 'A:ARTIST',
albumArtists: 'A:ALBUMARTIST',
albums: 'A:ALBUM',
genres: 'A:GENRE',
composers: 'A:COMPOSER',
tracks: 'A:TRACKS',
playlists: 'A:PLAYLISTS',
sonos_playlists: 'SQ',
share: 'S',
};
const defaultOptions = {
BrowseFlag: 'BrowseDirectChildren',
Filter: '*',
StartingIndex: '0',
RequestedCount: '100',
SortCriteria: '',
};
let searches = searchTypes[searchType]
? `${searchTypes[searchType]}${separator}`
: searchType;
if (searchTerm && searchTerm !== '') {
searches = `${searches}${separator}${encodeURIComponent(
searchTerm
)}`;
}
let opts = {
ObjectID: searches,
};
if (requestOptions && requestOptions.start !== undefined) {
opts.StartingIndex = requestOptions.start;
}
if (requestOptions && requestOptions.total !== undefined) {
opts.RequestedCount = requestOptions.total;
}
// opts = _.extend(defaultOptions, opts)
opts = Object.assign({}, defaultOptions, opts);
const result = await this.contentDirectoryService().GetResult(opts);
return result;
}
}
export async function discoverMultiple(options = { timeout: 5000 }) {
return new Promise((resolve, reject) => {
const discovery = DeviceDiscovery(options);
const devices = [];
discovery.on('DeviceAvailable', (device, model) => {
if (model.match(/^BR/)) {
return;
}
devices.push(new SonosEnhanced(device.host));
});
discovery.once('timeout', () => {
if (devices.length > 0) {
resolve(devices);
} else {
reject(new Error('No devices found'));
}
});
});
}