forked from CFenner/MMM-Sonos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsonos.js
More file actions
executable file
·115 lines (115 loc) · 3.51 KB
/
Copy pathsonos.js
File metadata and controls
executable file
·115 lines (115 loc) · 3.51 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
/* Magic Mirror
* Module: MagicMirror-Sonos-Module
*
* By Christopher Fenner https://github.com/CFenner
* MIT Licensed.
*/
Module.register('sonos', {
defaults: {
showStoppedRoom: true,
showAlbumArt: true,
showRoomName: true,
showDetails: false,
animationSpeed: 1000,
updateInterval: 0.5, // every 0.5 minutes
apiBase: 'http://localhost',
apiPort: 5005,
apiEndpoint: 'zones'
},
start: function() {
Log.info('Starting module: ' + this.name);
this.update();
// refresh every x minutes
setInterval(
this.update.bind(this),
this.config.updateInterval * 60 * 1000);
// showAbumArt forced to true if showDetails is false
if (!this.config.showDetails) {
this.config.showAbumArt = true;
}
},
update: function(){
this.sendSocketNotification(
'SONOS_UPDATE',
this.config.apiBase + ":" + this.config.apiPort + "/" + this.config.apiEndpoint);
},
render: function(data){
var text = '';
$.each(data, function (i, item) {
var room = item.coordinator.roomName;
var state = item.coordinator.state.zoneState;
var artist = item.coordinator.state.currentTrack.artist;
var track = item.coordinator.state.currentTrack.title;
var cover = item.coordinator.state.currentTrack.absoluteAlbumArtURI;
var streamInfo = item.coordinator.state.currentTrack.streamInfo;
if(item.members.length > 1){
room = '';
$.each(item.members, function (j, member) {
room += member.roomName + ', ';
});
room = room.slice(0, -2);
}
text += this.renderRoom(state, artist, track, cover, room);
}.bind(this));
this.loaded = true;
// only update dom if content changed
if(this.dom !== text){
this.dom = text;
this.updateDom(this.config.animationSpeed);
}
},
renderRoom: function(state, artist, track, cover, roomName) {
var room = '';
// if Sonos Playbar is in TV mode, no title is provided and therefore the room should not be displayed
var isEmpty = (artist && artist.trim().length) == 0
&& (track && track.trim().length) == 0
&& (cover && cover.trim().length) == 0;
// show song if PLAYING
if(state === 'PLAYING' && !isEmpty) {
if (this.config.showDetails) {
room += this.html.name.format(artist, track)
}
room += (this.config.showAlbumArt?this.html.art.format(cover):'');
//+"<span>"+streamInfo+"</span>"
}
// show room name if 'showRoomName' is set and PLAYING or 'showStoppedRoom' is set
if(this.config.showRoomName && (state === 'PLAYING' || this.config.showStoppedRoom)) {
room += this.html.room.format(roomName);
}
return this.html.roomWrapper.format(room);
},
html: {
loading: '<div class="dimmed light small">Loading music ...</div>',
roomWrapper: '<li>{0}</li>',
room: '<div class="room xsmall">{0}</div>',
song: '<div>{0}</div>',
name: '<div class="name normal medium"><div>{0}</div><div>{1}</div></div>',
art: '<div class="art"><img src="{0}"/></div>'
},
getScripts: function() {
return [
'String.format.js',
'//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.js'
];
},
getStyles: function() {
return ['sonos.css'];
},
getDom: function() {
var content = '';
if (!this.loaded) {
content = this.html.loading;
}else if(this.data.position.endsWith("left")){
content = '<ul class="flip">'+this.dom+'</ul>';
}else{
content = '<ul>'+this.dom+'</ul>';
}
return $('<div class="sonos">'+content+'</div>')[0];
},
socketNotificationReceived: function(notification, payload) {
if (notification === 'SONOS_DATA') {
Log.info('received SONOS_DATA');
this.render(payload);
}
}
});