forked from lordpengwin/squeezenode
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
346 lines (280 loc) · 10.6 KB
/
Copy pathserver.js
File metadata and controls
346 lines (280 loc) · 10.6 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
The MIT License (MIT)
Copyright (c) 2013 Piotr Raczynski, pio[dot]raczynski[at]gmail[dot]com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
var inherits = require('super');
var fs = require('fs');
var SqueezeRequest = require('./squeezerequest');
var SqueezePlayer = require('./squeezeplayer');
/**
* Create a SqueezeServer object
*
* @param address The URL of the server
* @param port The port that the server listens on
* @param username The username for authentication
* @param password The password for authentication
* @param sa A flag to skip apps
*/
function SqueezeServer(address, port, username, password, sa, sp) {
SqueezeServer.super_.apply(this, arguments);
var defaultPlayer = "00:00:00:00:00:00";
var self = this;
this.players = [];
this.apps = [];
var subs = {};
/**
* Subscribe to an event on a channel
*
* @param channel The channel that received the event
* @param sub The callback for the event
*/
this.on = function (channel, sub) {
subs[channel] = subs[channel] || [];
subs[channel].push(sub);
};
/**
* Send an event.
*
* @param channel The even channel
*/
this.emit = function (channel) {
let args = [].slice.call(arguments, 1);
for (let sub in subs[channel]) {
subs[channel][sub].apply(void 0, args);
}
};
/**
* Method to the the number of players.
*
* @param callback The function to call with the result.
*/
this.getPlayerCount = function (callback) {
this.request(defaultPlayer, ["player", "count", "?"], callback);
};
/**
* Method to get the ID of a player given it's index
*
* @param index The index of the player to get the ID for
* @param callback The function to call with the result.
*/
this.getPlayerId = function (index, callback) {
this.request(defaultPlayer, ["player", "id", index, "?"], callback);
};
/**
* Method to get the IP address of a player
*
* @param playerId The ID or index of the player
* @param callback The function to call with the result.
*/
this.getPlayerIp = function (playerId, callback) {
this.request(defaultPlayer, ["player", "ip", playerId, "?"], callback);
};
/**
* Method to get the name of a player.
*
* @param playerId The ID or index of the player
* @param callback The function to call with the result.
*/
this.getPlayerName = function (playerId, callback) {
this.request(defaultPlayer, ["player", "name", playerId, "?"], callback);
};
/**
* Get a list of the synchronization group members
*
* @param callback The function to call with the result.
*/
this.getSyncGroups = function (callback) {
this.request(defaultPlayer, ["syncgroups", "?"], callback);
};
/**
* Get a list of the apps installed in the server
*
* @param callback The function to call with the result.
*/
this.getApps = function (callback) {
this.request(defaultPlayer, ["apps", 0, 100], callback);
};
//
/**
* Get the content of a music folder.
*
* @param folderId The ID of the folder to get. Pass 0 or empty string "" to display root of music folder
* @param callback The function to call with the result.
*/
this.musicfolder = function (folderId, callback) {
this.request(defaultPlayer, ["musicfolder", 0, 100, "folder_id:" + folderId], callback);
};
/**
* Get a information about the players.
*
* @param callback The function to call with the result.
*/
this.getPlayers = function (callback) {
self.request(defaultPlayer, ["players", 0, 100], function (reply) {
if (reply.ok)
reply.result = reply.result.players_loop;
callback(reply);
});
};
/**
* Get a list of the artists from the server
*
* @param callback The callback to call with the result
* @param limit The maximum number of results
*/
this.getArtists = function (callback, limit) {
self.request(defaultPlayer, ["artists", 0, limit], function (reply) {
if (reply.ok)
reply.result = reply.result.artists_loop;
callback(reply);
})
};
/**
* Get a list of the albums from the server
*
* @param callback The callback to call with the result
* @param limit The maximum number of results
*/
this.getAlbums = function (callback, limit) {
self.request(defaultPlayer, ["albums", 0, limit], function (reply) {
if (reply.ok)
reply.result = reply.result.albums_loop;
callback(reply);
})
};
/**
* Get a list of the genre's from the server
*
* @param callback The callback to call with the result
* @param limit The maximum number of results
*/
this.getGenres = function (callback, limit) {
self.request(defaultPlayer, ["genres", 0, limit], function (reply) {
if (reply.ok)
reply.result = reply.result.genres_loop;
callback(reply);
})
};
/**
* Get a 'info' list from the server
*
* @param callback The callback to call with the result
* @param limit The maximum number of results
* @param slot The query to make to the server [genres, albums, artists, playlists ]
*/
this.getInfo = function (callback, limit, slot) {
self.request(defaultPlayer, [slot, 0, limit], function (reply) {
if (reply.ok)
reply.result = reply.result[slot + '_loop'];
callback(reply);
})
};
this.registerPlayers = function (players) {
for (let pl in players) {
if (! self.players[players[pl].playerid]) { // player not on the list
self.players[players[pl].playerid] = new SqueezePlayer(players[pl].playerid, players[pl].name, self.address, self.port, self.username, self.password);
}
}
}
/**
* Get Player oject for name, needs the players registred and normisles names with a lc search removeving puntuation
*
* @param name string
* @param only reten the one player if only one
*/
this.findPlayerObjectByName = function (name, only) {
name = this.normalizePlayer(name);
// Look for the player in the players list that matches the given name. Then return the corresponding player object
// from the squeezeserver stored by the player's id
for (var id in this.players) {
if (
this.normalizePlayer(this.players[id].name) === name || // name matches the requested player
(name === "" && (only && this.players.length === 1)) // name is undefined and there's only one player,
// so assume that's the one we want.
) {
return this.players[id];
}
}
return undefined
}
/**
* Do any necessary clean up of player names
*
* @param playerName The name of the player to clean up
* @param filter optional fn(str) returns str
* @returns The normalized player name
*/
this.normalizePlayer = function (playerName) {
playerName || (playerName = ''); // protect against `playerName` being undefined
playerName = playerName.replace('-', ' ');
playerName = playerName.replace(' ', ' ');
playerName = playerName.replace(' ', ' ');
playerName = playerName.toLowerCase(playerName);
return playerName;
}
/**
* Find out if we can contact the server and get some basic information
*
* @param skipApps A flag to skip getting information about the apps
*/
function register(skipApps,skipPlayers) {
if (!skipPlayers) {
// Get the list of players from the server
self.getPlayers(function (reply) {
// Process the player information and create Player objects for each one
if (reply.ok)
self.registerPlayers(reply.result);
// Send a signal that we are done
if (skipApps) {
self.emit('register', reply, undefined);
} else {
self.emit('registerPlayers', reply);
}
});
}
if(!skipApps) {
// Once the players have been obtained, request a list of the apps
self.on('registerPlayers', function (reply) {
self.getApps(function (areply) {
if (areply.ok) {
let apps = areply.result.appss_loop;
let dir = __dirname + '/';
fs.readdir(dir, function (err, files) {
files.forEach(function (file) {
let fil = file.substr(0, file.lastIndexOf("."));
for (let pl in apps) {
if (fil === apps[pl].cmd) {
let app = require(dir + file);
self.apps[apps[pl].cmd] = new app(defaultPlayer, apps[pl].name, apps[pl].cmd, self.address, self.port, self.username, self.password);
/* workaround, app needs existing player id so first is used here */
}
}
});
self.emit('register', reply, areply);
});
} else {
self.emit('register', reply, areply);
}
});
});
}
}
register(sa,sp);
}
inherits(SqueezeServer, SqueezeRequest);
module.exports = SqueezeServer;