forked from tbouron/MMM-Sonos
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MMM-Sonos.js
167 lines (147 loc) · 6.87 KB
/
MMM-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
Module.register('MMM-Sonos', {
defaults: {
animationSpeed: 1000,
showFullGroupName: false,
showArtist: true,
showAlbum: true,
showMetadata: true,
showAlbumCover: true
},
items: {},
start: function () {
Log.log('Sonos frontend started');
this.sendSocketNotification('SONOS_START');
},
getStyles: function () {
return ['MMM-Sonos.css'];
},
getScripts: function () {
return [this.file('node_modules/feather-icons/dist/feather.min.js')];
},
socketNotificationReceived: function (id, payload) {
Log.log(`Notification received: ${id}`, payload);
switch (id) {
case 'SET_SONOS_GROUPS':
this.items = payload;
this.updateDom(this.config.animationSpeed);
break;
case 'SET_SONOS_CURRENT_TRACK':
if (this.items.hasOwnProperty(payload.group.ID)) {
this.items[payload.group.ID] = {
...this.items[payload.group.ID],
group: payload.group,
track: payload.track,
};
this.updateDom(this.config.animationSpeed);
}
break;
case 'SET_SONOS_VOLUME':
if (this.items.hasOwnProperty(payload.group.ID)) {
this.items[payload.group.ID] = {
...this.items[payload.group.ID],
group: payload.group,
volume: payload.volume
};
this.updateDom();
}
break;
case 'SET_SONOS_MUTE':
if (this.items.hasOwnProperty(payload.group.ID)) {
this.items[payload.group.ID] = {
...this.items[payload.group.ID],
group: payload.group,
isMuted: payload.isMuted
};
this.updateDom();
}
break;
case 'SET_SONOS_PLAY_STATE':
if (this.items.hasOwnProperty(payload.group.ID)) {
this.items[payload.group.ID] = {
...this.items[payload.group.ID],
group: payload.group,
state: payload.state
};
this.updateDom(this.config.animationSpeed);
}
break;
default:
Log.info(`Notification with ID "${id}" unsupported. Ignoring...`);
break;
}
},
getHeader: function () {
if (this.data.header && Object.values(this.items).some(item => item.state === 'playing' && item.track)) {
return this.data.header;
}
},
getDom: function () {
if (Object.values(this.items).length === 0) {
return document.createElement('div');
}
const container = document.createElement('div');
container.className = 'sonos light';
container.append(...Object.values(this.items)
.filter(item => item.state === 'playing' && item.track)
.map(item => {
const container = document.createElement('div');
const textContainer = document.createElement('div');
if (this.config.showAlbumCover) {
const albumCover = document.createElement('div');
albumCover.className = this.data.position.endsWith('right') ? 'albumcover cover-right' : 'albumcover cover-left';
if (item.track.albumArtURI !== null && item.track.albumArtURI !== undefined && item.track.albumArtURI.trim() !== "") {
albumCover.innerHTML = `<img src="${item.track.albumArtURI}" width="80" height="80">`;
}
container.append(albumCover);
textContainer.className = this.data.position.endsWith('right') ? 'sonos-info-margin-right' : 'sonos-info-margin-left';
}
const track = document.createElement('div');
track.className = 'track marquee';
track.innerHTML = `<span><strong class="bright">${item.track.title}</strong></span>`;
track.firstChild.style.animationDuration = track.innerHTML.length / 6 + "s";
textContainer.append(track);
const artist = [];
if (this.config.showArtist && item.track.artist) {
artist.push(`<span class="bright">${item.track.artist}</span>`);
}
if (this.config.showAlbum && item.track.album) {
artist.push(`${item.track.album}`);
}
if (artist.length > 0) {
const artistElement = document.createElement('div');
artistElement.className = 'artist small marquee';
artistElement.innerHTML = '<span>' + artist.join(' ○ ') + '</span>';
textContainer.append(artistElement);
}
if (this.config.showMetadata) {
let volume;
if (item.isMuted === true) {
volume = `${this.getIcon('volume-x', 'dimmed')}`;
} else {
volume = `${this.getIcon(item.volume < 50 ? 'volume-1' : 'volume-2', 'dimmed')} <span>${item.volume}</span>`;
}
const groupName = this.config.showFullGroupName
? item.group.ZoneGroupMember.map(member => member.ZoneName).join(' + ')
: item.group.Name;
const metadata = document.createElement('div');
metadata.className = 'metadata small normal';
metadata.innerHTML =
`<span>${this.getIcon('speaker', 'dimmed')} <span class="group-name ticker">${groupName}</span></span>` +
' ' +
`<span>${volume}</span>` +
' ' +
`<span>${this.getIcon('activity', 'dimmed')} <span>${Math.floor(item.track.duration / 60)}:${Math.ceil(item.track.duration % 60).toString().padStart(2, '0')}</span></span>`;
textContainer.append(metadata);
}
container.append(textContainer);
const clearContainer = document.createElement('div');
clearContainer.style = 'clear:both;';
container.append(clearContainer);
return container;
}));
return container;
},
getIcon: function (iconId, classes) {
return `<svg class="feather ${classes}"><use xlink:href="${this.file('node_modules/feather-icons/dist/feather-sprite.svg')}#${iconId}"/></svg>`;
}
});