-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroonadapter.js
246 lines (212 loc) · 7.05 KB
/
roonadapter.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const EventEmitter = require('events');
const RoonApi = require( "node-roon-api");
const RoonApiStatus = require( "node-roon-api-status");
const RoonApiImage = require( "node-roon-api-image");
const RoonApiTransport = require( "node-roon-api-transport");
const slug = require( "slug");
const fs = require( 'fs');
class RoonAdapter extends EventEmitter{
// ********************************************
// * Constructors
// ********************************************
constructor(roonPairingTokenFile, author, hostinfo) {
super();
this._zones = {};
this._author = author;
this._hostname = hostinfo.hostname;
this._connected = false;
this._roon = new RoonApi({
extension_id: "svh-roon-steelseries",
display_name: "Simple Now Playing display intended for Steelseries Oleds",
display_version: "1.0.0",
publisher: author,
email: "stef@personaloffice365.com",
website: "https://twitter.com/vanHooijdonk",
log_level: "none",
core_paired: this.corePaired.bind(this),
core_unpaired: this.coreUnpaired.bind(this),
});
// overriding the default safe, to use the users apps settings fodler
this.roon.save_config = function(k, v) {
try {
let config;
try {
let content = fs.readFileSync(
roonPairingTokenFile,
{ encoding: 'utf8' });
config = JSON.parse(content) || {};
} catch (e) {
config = {};
}
if (v === undefined || v === null)
delete(config[k]);
else
config[k] = v;
fs.writeFileSync(
roonPairingTokenFile,
JSON.stringify(config, null, ' '));
} catch (e) { }
};
// overriding the default safe, to use the users apps settings fodler
this.roon.load_config = function(k) {
try {
let content = fs.readFileSync(
roonPairingTokenFile,
{ encoding: 'utf8' });
return JSON.parse(content)[k];
} catch (e) {
return undefined;
}
};
}
// ********************************************
// * Properties
// ********************************************
get roon() {
return this._roon;
}
get roonCore() {
return this._roonCore;
}
get zones() {
// Everyone gets their own copy
return Object.assign({}, this._zones);
}
isConnected() {
return this._connected;
}
// ********************************************
// * Public methods
// ********************************************
start() {
// Start Roon
this.roonApiStatus = new RoonApiStatus(this.roon);
this.roon.init_services({
required_services: [ RoonApiTransport, RoonApiImage ],
provided_services: [ this.roonApiStatus ]
});
this.roonApiStatus.set_status("Extension enabled on " + this._hostname, false);
this.roon.start_discovery();
console.info("Roon Extension started discovery.");
}
stop(){
this.roonApiStatus = null;
console.info("Roon Extension stopped.");
}
// ********************************************
// * Private methods
// ********************************************
corePaired(core) {
this._roonCore = core;
console.info("Roon Extention paired with Core.");
const transport = core.services.RoonApiTransport;
transport.subscribe_zones((response, data) => {
switch(response) {
case "Subscribed":
this.setZonesFromData(data.zones);
break;
case "Changed":
if(data.zones_changed) {
this.setZonesFromData(data.zones_changed);
}
if(data.zones_seek_changed) {
this.updateZonesFromSeekData(data.zones_seek_changed);
}
break;
default:
// this.logger.warn(`Unhandled subscription response "${response}"`);
break;
}
});
this._connected = true;
this.emit('core-paired');
}
coreUnpaired(core) {
// core.moo.transport.logger.log("Roon core unpaired");
this._nowPlaying = null;
this._connected = false;
this.emit('core-unpaired');
console.info("Roon Extention was un-paired with Core.");
}
/**
* Sets the zones from Roon data. Creates a unique slugified zone name for each zone.
* Duplicate display names will get an index appended to the zone name.
*
* @param {Object} zoneData The Roon zone data.
*/
setZonesFromData(zoneData) {
const zoneNames = [];
if(Array.isArray(zoneData)) {
// Loop all zones and create unique internal entries
zoneData.forEach((zone) => {
let zoneName = slug(zone.display_name);
if(Object.prototype.hasOwnProperty.call(zoneNames, zoneName)) {
zoneNames[zoneName] += 1;
zoneName = `${zoneName}_${zoneNames[zoneName]}`;
} else {
zoneNames[zoneName] = 1;
}
zone._zoneName = zoneName;
// Track by native zone ID. This will keep our unique names consistent while we're running
this._zones[zone.zone_id] = zone;
// log current zone status
if(zone.now_playing && zone.now_playing.two_line){
this.emit('zone-playing',
zone._zoneName,
zone.state,
zone.now_playing.two_line.line1,
zone.now_playing.two_line.line2);
}
else{
this.emit('zone-playing', zone._zoneName, zone.state, null);
}
});
this.emit('zones-updated', this.zones);
}
}
sendRoonStatus(zoneName){
if(this.isConnected() && this.roonApiStatus) {
this.roonApiStatus.set_status(
"On " + this._hostname + " showing zone " + zoneName + " on Steelseries keyboard", false);
}
}
updateZonesFromSeekData(zoneData) {
// log("seek zone", zone, data.zones_seek_changed);
if(Array.isArray(zoneData)) {
zoneData.forEach((seekZone) => {
const zone = this.zones[seekZone.zone_id];
if(zone && zone.now_playing) {
zone.now_playing.seek_position = seekZone.seek_position;
// only publish to Steelseries if we want this zone to show.
this.emit('zone-playing-seekupdate',
zone._zoneName, // zone name
zone.state, // playing
seekZone.seek_position, // 40
zone.now_playing.length, // 120
zone.now_playing.three_line.line1, // song title
zone.now_playing.three_line.line2, // artists
zone.now_playing.three_line.line3); // album
}
});
}
}
/**
* Gets the zone by the internal zone name.
*
* @param {<type>} zoneName The zone name.
*
* @return {<type>} The zone by zone name, or null if not found.
*/
getZoneByZoneName(zoneName) {
let result = null;
for(const zoneId of Object.keys(this.zones)) {
const zone = this.zones[zoneId];
if(zone._zoneName === zoneName) {
result = zone;
break;
}
}
return result;
}
}
module.exports = RoonAdapter