forked from tbouron/MMM-Sonos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_helper.js
More file actions
142 lines (127 loc) · 4.89 KB
/
node_helper.js
File metadata and controls
142 lines (127 loc) · 4.89 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
const NodeHelper = require('node_helper');
const {AsyncDeviceDiscovery, Listener: listener, Helpers: helpers} = require('sonos');
module.exports = NodeHelper.create({
discovery: null,
asyncDevice: null,
init: function() {
this.discovery = new AsyncDeviceDiscovery();
},
stop: function() {
if (listener.isListening()) {
listener.stopListener().then(() => {
console.debug('Stopped all listeners to Sonos devices');
}).catch(error => {
console.error(`Failed to stop listeners to Sonos devices, connections might be dangling: ${error.message}`);
});
}
},
socketNotificationReceived: function (id, payload) {
switch (id) {
case 'SONOS_START':
this.discoverGroups();
break;
default:
Log.info(`Notification with ID "${id}" unsupported. Ignoring...`);
break;
}
},
discoverGroups: function(attempts = 0) {
if (!this.asyncDevice) {
this.asyncDevice = this.discovery.discover().then(device => {
listener.on('ZonesChanged', () => {
console.log(`Zones have changed. Rediscovering all groups ...`);
this.discoverGroups();
});
return listener.subscribeTo(device).then(() => {
return device;
})
})
}
this.asyncDevice.then(device => {
return device.getAllGroups();
}).then(groups => {
this.setGroups(groups);
}).catch(error => {
attempts++;
const timeout = Math.min(Math.pow(attempts, 2), 30);
console.error(`Failed to get groups: ${error.message}. Retrying in ${timeout} seconds ...`);
if (listener.isListening()) {
listener.stopListener().then(() => {
console.debug('Stopped all listeners to Sonos devices');
}).catch(error => {
console.error(`Failed to stop listeners to Sonos devices, connections might be dangling: ${error.message}`);
});
}
this.asyncDevice = null;
setTimeout(() => {
this.discoverGroups(attempts);
}, timeout * 1000);
});
},
setGroups(groups) {
Promise.all(groups.map(group => {
const sonos = group.CoordinatorDevice();
return Promise.all([
sonos.currentTrack(),
sonos.getCurrentState(),
sonos.getVolume(),
sonos.getMuted(),
sonos.getCurrentUriMetadata()
]).then(data => {
return {
group,
track: data[0],
state: data[1],
volume: data[2],
isMuted: data[3],
uriMetadata: data[4],
};
});
})).then(items => {
this.sendSocketNotification('SET_SONOS_GROUPS', items.reduce((map, item) => {
map[item.group.ID] = item;
return map;
}, {}));
return items;
}).then(groups => {
this.setListeners(groups.map(item => item.group));
});
},
setListeners: function(groups) {
groups.forEach(group => {
console.log(`Registering listeners for group "${group.Name}" (host "${group.host}")`);
const sonos = group.CoordinatorDevice();
sonos.on('Mute', isMuted => {
console.log('This speaker is %s.', isMuted ? 'muted' : 'unmuted')
});
sonos.on('CurrentTrack', track => {
console.log(`[Group ${group.Name} - ${group.host}] Track changed to "${track.title}" by "${track.artist}"`);
this.sendSocketNotification('SET_SONOS_CURRENT_TRACK', {
group,
track
});
});
sonos.on('Volume', volume => {
console.log(`[Group ${group.Name} - ${group.host}] Volume changed to "${volume}"`);
this.sendSocketNotification('SET_SONOS_VOLUME', {
group,
volume
});
});
sonos.on('Muted', isMuted => {
console.log(`[Group ${group.Name} - ${group.host}] Group is ${isMuted ? 'muted' : 'unmuted'}`);
this.sendSocketNotification('SET_SONOS_MUTE', {
group,
isMuted
});
});
sonos.on('PlayState', state => {
console.log(`[Group ${group.Name} - ${group.host}] Play state change to "${state}"`);
this.sendSocketNotification('SET_SONOS_PLAY_STATE', {
group,
state
});
});
});
}
});