-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·263 lines (230 loc) · 8.71 KB
/
app.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
"use strict";
var Meridian = require("node-meridian"),
RoonApi = require("node-roon-api"),
RoonApiSettings = require('node-roon-api-settings'),
RoonApiStatus = require('node-roon-api-status'),
RoonApiVolumeControl = require('node-roon-api-volume-control'),
RoonApiSourceControl = require('node-roon-api-source-control');
var roon = new RoonApi({
extension_id: 'com.roonlabs.meridian',
display_name: 'Meridian Volume/Source Control',
display_version: "1.0.0",
publisher: 'Roon Labs, LLC',
email: 'contact@roonlabs.com',
website: 'https://github.com/RoonLabs/roon-extension-meridian',
});
var mysettings = roon.load_config("settings") || {
serialport: "",
ip: "",
setsource: "CD",
displaysource: "CD",
initialvolume: 45,
mode: "TN51",
};
var meridian = { };
function makelayout(settings) {
var l = {
values: settings,
layout: [],
has_error: false
};
l.layout.push({
type: "dropdown",
title: "Protocol Mode",
values: [
{ value: "TN49", title: "TN49" },
{ value: "TN51", title: "TN51" },
{ value: "DS 6ii03", title: "DS 6ii03" },
],
setting: "mode",
});
if (settings.mode == "218") {
l.layout.push({
type: "string",
title: "IP Address",
maxlength: 15,
setting: "ip",
});
} else {
l.layout.push({
type: "string",
title: "Serial Port",
maxlength: 256,
setting: "serialport",
});
}
l.layout.push({
type: "string",
title: "Source displayed on device (select source and see what speakers display, may need multiple entries with some speakers)",
maxlength: 5,
setting: "displaysource",
});
l.layout.push({
type: "dropdown",
title: "Source for Convenience Switch",
values: [
{ value: "CD", title: "CD" },
{ value: "RD", title: "Radio" },
{ value: "LP", title: "LP/Aux/SLS" },
{ value: "TV", title: "TV" },
{ value: "T1", title: "Tape/Tape1/iPod" },
{ value: "T2", title: "Tape2/Sat" },
{ value: "CR", title: "CDR/Disc" },
{ value: "CB", title: "Cable" },
{ value: "TX", title: "Text/DVD" },
{ value: "V1", title: "VCR1/Mixer/PVR" },
{ value: "V2", title: "VCR2/USB" },
{ value: "LD", title: "LDisc/Game" }
],
setting: "setsource",
});
l.layout.push({
type: "integer",
title: "Initial Volume",
min: 1,
max: 99,
setting: "initialvolume",
});
return l;
}
var svc_settings = new RoonApiSettings(roon, {
get_settings: function(cb) {
cb(makelayout(mysettings));
},
save_settings: function(req, isdryrun, settings) {
let l = makelayout(settings.values);
req.send_complete(l.has_error ? "NotValid" : "Success", { settings: l });
if (!isdryrun && !l.has_error) {
var oldmode = mysettings.mode;
var oldip = mysettings.ip;
var oldport = mysettings.serialport;
mysettings = l.values;
svc_settings.update_settings(l);
let force = false;
if (oldmode != mysettings.mode) force = true;
if (mysettings.mode == "218") {
if (oldip != mysettings.ip) force = true;
} else {
if (oldport != mysettings.serialport) force = true;
}
if (force) setup();
roon.save_config("settings", mysettings);
}
}
});
var svc_status = new RoonApiStatus(roon);
var svc_volume_control = new RoonApiVolumeControl(roon);
var svc_source_control = new RoonApiSourceControl(roon);
roon.init_services({
provided_services: [ svc_volume_control, svc_source_control, svc_settings, svc_status ]
});
function setup() {
if (meridian.control)
meridian.control.stop();
meridian.control = new Meridian(mysettings.mode);
meridian.control.on('connected', ev_connected);
meridian.control.on('disconnected', ev_disconnected);
meridian.control.on('volume', ev_volume);
meridian.control.on('source', ev_source);
if (meridian.source_control) { meridian.source_control.destroy(); delete(meridian.source_control); }
if (meridian.volume_control) { meridian.volume_control.destroy(); delete(meridian.volume_control); }
var opts = { volume: mysettings.initialvolume, source: mysettings.setsource };
if (mysettings.mode == "218") {
if (!mysettings.ip) {
svc_status.set_status("Not configured, please check settings.", true);
return;
}
opts.ip = mysettings.ip;
} else {
if (!mysettings.serialport) {
svc_status.set_status("Not configured, please check settings.", true);
return;
}
opts.port = mysettings.serialport;
}
console.log(opts);
meridian.control.start(opts);
}
function is_selected(v) {
return mysettings.displaysource.split(/[\/ ,]/).map(x => x.toLowerCase()).indexOf(v.toLowerCase()) != -1;
}
function ev_connected(status) {
let control = meridian.control;
console.log("[Meridian Extension] Connected");
svc_status.set_status("Connected to Meridian", false);
control.set_volume(mysettings.initialvolume);
control.set_source(mysettings.setsource);
meridian.volume_control = svc_volume_control.new_device({
state: {
display_name: "Meridian", // XXX need better less generic name -- can we get serial number from the controller?
volume_type: "number",
volume_min: 1,
volume_max: 99,
volume_value: control.properties.volume > 0 ? control.properties.volume : 65,
volume_step: 1.0,
is_muted: control.properties.source == "Muted"
},
set_volume: function (req, mode, value) {
let newvol = mode == "absolute" ? value : (control.properties.volume + value);
if (newvol < this.state.volume_min) newvol = this.state.volume_min;
else if (newvol > this.state.volume_max) newvol = this.state.volume_max;
control.set_volume(newvol);
req.send_complete("Success");
},
set_mute: function (req, action) {
if (action == "on")
control.mute();
else if (action == "off")
control.set_source(mysettings.setsource);
else if (control.properties.source == "Muted")
control.set_source(mysettings.setsource);
else
control.mute();
req.send_complete("Success");
}
});
meridian.source_control = svc_source_control.new_device({
state: {
display_name: "Meridian", // XXX need better less generic name -- can we get serial number from the controller?
supports_standby: true,
status: control.properties.source == "Standby" ? "standby" : (is_selected(control.properties.source) ? "selected" : "deselected")
},
convenience_switch: function (req) {
control.set_source(mysettings.setsource, err => { req.send_complete(err ? "Failed" : "Success"); });
req.send_complete("Success");
},
standby: function (req) {
this.state.status = "standby";
control.standby();
req.send_complete("Success");
}
});
}
function ev_disconnected(status) {
console.log("[Meridian Extension] Disconnected");
if (mysettings.mode == "218")
svc_status.set_status("Could not connect to Meridian on \"" + mysettings.ip + "\"", true);
else
svc_status.set_status("Could not connect to Meridian on \"" + mysettings.serialport + "\"", true);
if (meridian.source_control) { meridian.source_control.destroy(); delete(meridian.source_control); }
if (meridian.volume_control) { meridian.volume_control.destroy(); delete(meridian.volume_control); }
}
function ev_volume(val) {
console.log("[Meridian Extension] received volume change from device:", val);
if (meridian.volume_control)
meridian.volume_control.update_state({ volume_value: val });
}
function ev_source(val) {
console.log("[Meridian Extension] received source change from device:", val);
if (val == "Muted" && meridian.volume_control)
meridian.volume_control.update_state({ is_muted: true });
else if (val == "Standby" && meridian.source_control)
meridian.source_control.update_state({ status: "standby" });
else {
if (meridian.volume_control)
meridian.volume_control.update_state({ is_muted: false });
meridian.source_control.update_state({ status: (is_selected(val) ? "selected" : "deselected") });
}
}
setup();
roon.start_discovery();