-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
365 lines (293 loc) · 10.4 KB
/
index.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
var libQ = require('kew');
var fs = require('fs-extra');
var config = new (require('v-conf'))();
var exec = require('child_process').exec;
var execSync = require('child_process').execSync;
var heos = require('heos-api')
var net = require('net')
var denonDevices = [];
var singleDevice = [];
var activeDevice;
const port = 23;
var host;
var volumeSettings = {
maxvolume: '98',
volumecurve: 'linear',
volumesteps: 1,
volumeOverride: true
};
var currentVolume;
var currentMute;
var Volume = {
vol: null,
mute: null,
disableVolumeControl: false
};
module.exports = denonAvrVolumeControl;
function denonAvrVolumeControl(context) {
var self = this;
this.context = context;
this.commandRouter = this.context.coreCommand;
this.logger = this.context.logger;
this.configManager = this.context.configManager;
denonAvrVolumeControl.prototype.onVolumioStart = function () {
var self = this;
var configFile = this.commandRouter.pluginManager.getConfigurationFile(this.context, 'config.json');
this.config = new (require('v-conf'))();
this.config.loadFile(configFile);
// Tack onto the saveVolumeOptions method to ensure we can setup/cleanup on a mixer change
this.commandRouter.sharedVars.registerCallback('alsa.outputdevicemixer', this.setupHeosAndIdDevices.bind(this));
return libQ.resolve();
}
denonAvrVolumeControl.prototype.onStart = function () {
var self = this;
var defer = libQ.defer();
//Setup the plugin to do its thing
self.setupHeosAndIdDevices()
.then(() => defer.resolve());
// Once the Plugin has successfull started resolve the promise
return defer.promise;
};
denonAvrVolumeControl.prototype.onStop = function () {
var self = this;
var defer = libQ.defer();
// Once the Plugin has successfull stopped resolve the promise
defer.resolve();
return libQ.resolve();
};
denonAvrVolumeControl.prototype.onRestart = function () {
var self = this;
var defer = libQ.defer();
//Setup the plugin to do its thing
self.setupHeosAndIdDevices()
.then(() => defer.resolve());
return defer.promise;
};
// Configuration Methods -----------------------------------------------------------------------------
denonAvrVolumeControl.prototype.getUIConfig = function () {
var defer = libQ.defer();
var self = this;
var lang_code = this.commandRouter.sharedVars.get('language_code');
self.commandRouter.i18nJson(__dirname + '/i18n/strings_' + lang_code + '.json',
__dirname + '/i18n/strings_en.json',
__dirname + '/UIConfig.json')
.then(function (uiconf) {
defer.resolve(uiconf);
})
.fail(function () {
defer.reject(new Error());
});
return defer.promise;
};
denonAvrVolumeControl.prototype.getConfigurationFiles = function () {
return ['config.json'];
}
denonAvrVolumeControl.prototype.setUIConfig = function (data) {
var self = this;
//Perform your installation tasks here
};
denonAvrVolumeControl.prototype.getConf = function (varName) {
var self = this;
//Perform your installation tasks here
};
denonAvrVolumeControl.prototype.setConf = function (varName, varValue) {
var self = this;
//Perform your installation tasks here
};
denonAvrVolumeControl.prototype.setupHeosAndIdDevices = function () {
var self = this;
var defer = libQ.defer();
if (activeDevice) activeDevice.end();
activeDevice = undefined;
denonDevices = [];
if (this.commandRouter.executeOnPlugin('audio_interface', 'alsa_controller', 'getConfigParam', 'mixer_type') == 'None') {
//Set everything up and allow the volume to be changed. We only want to be messing with the volume if nothing else is.
heos.discoverAndConnect().then(connection => {
connection
.on({ commandGroup: 'player', command: 'get_players' }, (players) => {
if (players.payload) {
players.payload.forEach(player => {
if (player.model && player.model.toLowerCase().includes('denon avr')) denonDevices.push([player.model, player.ip])
});
singleDevice = denonDevices ? denonDevices[0] : [];
if (singleDevice) {
host = singleDevice[1];
// self.logger.info(`Denon AVR Volume Control::Device found: ${singleDevice[0]} with IP address: ${singleDevice[1]}`);
activeDevice = new net.Socket();
activeDevice.connect(port, host, function () {
self.logger.info(`Denon AVR Volume Control::Connected to ${singleDevice[0]} on ${host}`);
setTimeout(() => {
self.sendCommand('MV?');
}, 1001); // Give it some time before we blast off
// activeDevice.write('MV?');
});
activeDevice.on('data', function (data) {
self.logger.debug(`Denon AVR Volume Control:: ${data}`);
self.decodeVolume(data);
});
activeDevice.on('close', function () {
self.logger.info('Denon AVR Volume Control::Connection Closed');
});
activeDevice.on('error', function (error) {
self.logger.error(`Denon AVR Volume Control::Connection Error ${error}`);
});
}
}
})
.onAll((message) => self.logger.info(`Denon AVR Volume Control::${JSON.stringify(message, null, ' ')}`)) // Change later to debug message
.write('system', 'register_for_change_events', { enable: 'off' })
.write('player', 'get_players', {})
// .write('system', 'register_for_change_events', { enable: 'on' })
// Get output card number
let outputdevice = self.commandRouter.executeOnPlugin('audio_interface', 'alsa_controller', 'getConfigParam', 'outputdevice');
// Override the volume control using the built-in method
self.commandRouter.executeOnPlugin('audio_interface', 'alsa_controller', 'setDeviceVolumeOverride', {
card: outputdevice, pluginType: 'system_hardware', pluginName: 'denon_avr_volume_control', overrideAvoidSoftwareMixer: true
});
defer.resolve();
})
.catch((e) => {
self.logger.error(`Denon AVR Volume Control::${e}`);
defer.reject(`Denon AVR Volume Control::${e}`);
})
}
return defer.promise;
}
self.decodeVolume = function (data) {
if (data.includes('MV') && !data.includes('MVMAX')) {
// Process volume from the AVR
let vol = (data.toString().replace('MV', '') == '--') ? 0 : data.toString().replace('MV', '');
vol = vol.length === 3 ? Number(vol) : Number(vol) / 10; // I don't why it's working like this, it's like it's opposite day
self.logger.verbose('After mod: ' + vol);
Volume.vol = (vol >= 0 && vol <= 98) ? vol : Volume.vol;
self.commandRouter.volumioupdatevolume(Volume);
self.logger.info(`Denon AVR Volume Control::Current volume is: ${Volume.vol}`);
} else if (data.includes('MVMAX')) {
// Process Max volume restriction from the AVR
let maxvol = data.toString().replace('MVMAX', '');
maxvol = (maxvol.length === 3) ? (Number(maxvol) / 10) : Number(maxvol);
volumeSettings.maxvolume = (maxvol >= 0 && maxvol <= 98) ? maxvol : volumeSettings.maxvolume;
// self.commandRouter.volumioUpdateVolumeSettings(volumeSettings);
self.logger.info(`Denon AVR Volume Control::Max volume allowed is: ${volumeSettings.maxvolume}`);
}
}
self.sendCommand = function (cmd) {
var defer = libQ.defer();
if (activeDevice && cmd) {
setTimeout(() => {
activeDevice.write(cmd, (err) => {
(!err) ? defer.resolve : defer.reject;
})
}, 51); // Denon API requires at least 50ms between successive commands.
}
return defer.promise;
}
denonAvrVolumeControl.prototype.updateVolumeSettings = function (data) {
var self = this;
var defer = libQ.defer();
// self.logger.error(`AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA: ${JSON.stringify(data, null, ' ')}`);
// volumeSettings = {
// device: valdevice,
// name: outdevicename,
// mixer: valmixer,
// mixertype: valmixertype,
// maxvolume: valvolumemax,
// volumecurve: valvolumecurvemode,
// volumestart: valvolumestart,
// volumesteps: valvolumesteps,
// volumeOverride: false
// }
defer.resolve(data);
return defer.promise;
}
denonAvrVolumeControl.prototype.retrievevolume = function () {
var self = this;
var defer = libQ.defer();
libQ.resolve(Volume)
.then(function (Volume) {
defer.resolve(Volume);
self.commandRouter.volumioupdatevolume(Volume);
});
return defer.promise;
}
denonAvrVolumeControl.prototype.alsavolume = function (VolumeInteger) {
var self = this;
var defer = libQ.defer();
self.logger.info('denonAvrVolumeControl::SetAlsaVolume ' + VolumeInteger);
switch (VolumeInteger) {
case 'mute':
// Mute
self.sendCommand('MUON').then(() => {
Volume.mute = true;
Volume.disableVolumeControl = false;
defer.resolve(Volume);
});
break;
case 'unmute':
// Unmute
self.sendCommand('MUOFF').then(() => {
Volume.mute = false;
Volume.disableVolumeControl = false;
defer.resolve(Volume);
});
break;
case 'toggle':
// Mute or unmute, depending on current state
if (Volume.mute) {
defer.resolve(self.alsavolume('unmute'));
} else {
defer.resolve(self.alsavolume('mute'));
}
break;
case '+':
VolumeInteger = Number(Volume.vol) + Number(volumeSettings.volumesteps);
if (VolumeInteger > 98) {
VolumeInteger = 98;
}
if (VolumeInteger > volumeSettings.maxvolume) {
VolumeInteger = volumeSettings.maxvolume;
}
Volume.vol = VolumeInteger;
Volume.mute = false;
Volume.disableVolumeControl = false;
self.sendCommand('MVUP').then(() => {
defer.resolve(Volume);
});
break;
case '-':
VolumeInteger = Number(Volume.vol) + Number(volumeSettings.volumesteps);
if (VolumeInteger < 0) {
VolumeInteger = 0;
}
if (VolumeInteger > volumeSettings.maxvolume) {
VolumeInteger = volumeSettings.maxvolume;
}
Volume.vol = VolumeInteger;
Volume.mute = false;
Volume.disableVolumeControl = false;
self.sendCommand('MVDOWN').then(() => {
defer.resolve(Volume);
});
break;
default:
// Set the volume with numeric value 0-98
if (VolumeInteger < 0) {
VolumeInteger = 0;
}
if (VolumeInteger > 98) {
VolumeInteger = 98;
}
if (VolumeInteger > volumeSettings.maxvolume) {
VolumeInteger = volumeSettings.maxvolume;
}
Volume.vol = VolumeInteger;
Volume.mute = false;
Volume.disableVolumeControl = false;
self.sendCommand(`MV${VolumeInteger < 10 ? '0' + VolumeInteger : VolumeInteger}`).then(() => {
defer.resolve(Volume);
});
}
return defer.promise;
}
}