-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-SpotifyHelper.js
94 lines (79 loc) · 1.98 KB
/
MMM-SpotifyHelper.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
//
// Module : MMM-SpotifyHelper
//
Module.register("MMM-SpotifyHelper", {
defaults: {
urls: [],
volume: 50,
volumeStep: 10,
showNotif: true
},
currentUrlId: null,
notificationReceived: function(notification, payload) {
switch (notification) {
case "SPOTIFY_HELPER_VOLUME":
this.updateVolume(payload);
break;
case "SPOTIFY_HELPER_SOURCE":
this.updateSource(payload);
break;
}
},
updateVolume: function(value) {
if (value === 'UP') {
this.config.volume = this.config.volume + this.config.volumeStep;
if (this.config.volume > 100) {
this.config.volume = 100;
}
} else if (value === 'DOWN') {
this.config.volume = this.config.volume - this.config.volumeStep;
if (this.config.volume < 0) {
this.config.volume = 0;
}
}
this.sendNotification("SPOTIFY_VOLUME", this.config.volume);
this.showNotif('Volume set : '+this.config.volume);
},
updateSource: function (value) {
if (this.currentUrlId === null) {
this.currentUrlId = 0;
} else {
if (value === 'NEXT') {
this.currentUrlId++;
if (this.currentUrlId > this.config.urls.length -1) {
this.currentUrlId = 0;
}
} else if (value === 'PREVIOUS') {
this.currentUrlId--;
if (this.currentUrlId < 0) {
this.currentUrlId = this.config.urls.length -1;
}
}
}
var currentUrl = this.config.urls[this.currentUrlId];
if (currentUrl !== 'undefined') {
var type = currentUrl.uri.match(/(?<=^spotify:)(\w+)(?:)/g)[0];
var payload = (type === 'track')
? {"uris": [currentUrl.uri]}
: {"context_uri": currentUrl.uri}
;
this.sendNotification("SPOTIFY_PLAY", payload);
var message = 'title' in currentUrl
? type + ' ' + currentUrl.title
: currentUrl.uri
;
this.showNotif(message+ ' Started');
}
},
showNotif: function (message) {
if (this.config.showNotif) {
this.sendNotification("SHOW_ALERT",
{
type: "notification",
title: 'MMM-SpotifyHelper',
message: message
}
);
}
}
});