forked from mtudor/HomeySqueezebox
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.js
More file actions
139 lines (110 loc) · 4.28 KB
/
app.js
File metadata and controls
139 lines (110 loc) · 4.28 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
"use strict";
const Homey = require('homey');
const SqueezeServer = require('squeezenode');
const util = require('/lib/util.js');
const moment = require('moment');
const maxPlaylistCacheAge = 2; // In minutes
class SqueezeboxApp extends Homey.App {
onInit() {
var playlistsCache = [];
var playlistTime = moment();
this.log('Squeezebox player for Homey started...');
this.playPlaylist = new Homey.FlowCardAction('playPlaylist');
this.playPlaylist
.register()
.registerRunListener((args, state) => {
this.log('playPlaylist triggered');
const protocol = args.device.getSetting('protocol');
const server = args.device.getSetting('server');
const port = args.device.getSetting('port');
const id = args.device.getSetting('id');
const playlistId = args.playlist.id;
return this._playPlaylist(protocol, server, port, id, playlistId)
.then(reply => {
console.log(reply);
return Promise.resolve(true);
})
.catch(e => {
console.log('3', e);
return Promise.reject('Error while starting playlist');
});
})
.getArgument('playlist')
.registerAutocompleteListener((query, args) => {
this.log('AutocompleteListener playPlaylist triggered');
const protocol = args.device.getSetting('protocol');
const server = args.device.getSetting('server');
const port = args.device.getSetting('port');
var cacheAge = moment.duration(moment().diff(playlistTime));
this.log('cache age', cacheAge.as('minutes'));
if (playlistsCache.length > 0 && cacheAge.as('minutes') <= maxPlaylistCacheAge) {
this.log('Using playlist cache');
var results = playlistsCache.filter( result => {
return result.name.toLowerCase().indexOf( query.toLowerCase() ) > -1;
});
this.log('filtered', results);
return Promise.resolve(results);
} else {
this.log('Playlist cache empty or old, fetching new');
this._getPlaylists(protocol, server, port)
.then(playlists => {
this.log('Playlist received, sending to Homey');
playlistsCache = playlists;
playlistTime = moment();
return Promise.resolve(playlists);
})
.catch(e => {
console.log(e);
return Promise.reject(Homey.__("msg_deviceOffline", {"errorMsg": e}));
});
};
});
}
_getPlaylists(protocol, server, port) {
const url = protocol + server;
const mySqueezeServer = new SqueezeServer(url, port);
var playlists = [];
return new Promise(function(resolve, reject){
mySqueezeServer.register()
.then(() => {
mySqueezeServer.playlists()
.then((reply) => {
if (reply.count > 0) {
playlists = reply.playlists.map(function (item) {
return {
id: item.id,
name: item.playlist
};
});
resolve(playlists);
} else {
this.log('No playlists on server');
reject(Homey.__("msg_noPlaylistsOnServer"));
}
})
.catch(e => {
reject(Homey.__("msg_deviceOffline", {"errorMsg": e}));
});
})
.catch(e => {
reject(Homey.__("msg_deviceOffline", {"errorMsg": e}));
});
});
}
_playPlaylist(protocol, server, port, id, playlistId) {
console.log('Playing playlist', playlistId);
const url = protocol + server;
const mySqueezeServer = new SqueezeServer(url, port);
return mySqueezeServer.register().then(() => {
return mySqueezeServer.players[id].loadPlaylist(playlistId).catch(e => {
console.log('1', e);
throw Error(Homey.__("msg_unableToPlayPlaylist"));
});
})
.catch(e => {
console.log('2', e);
throw Error(e);
});
}
};
module.exports = SqueezeboxApp;