musiccast-repairkit/scripts/sync-volume.js
nicobo 2a89588dc4 ~ major version boost
~ removed `conf.` prefix for scenario-specific options
+ added '-c' shortcut to '--config'
~ upgraded from yamaha-yxc-nodejs 0.x to 3.x
~ general dependencies upgrade
- few docs & logs changes
2023-07-06 11:06:04 +02:00

66 lines
2.1 KiB
JavaScript

const log = require('../logging');
const YamahaYXC = require('yamaha-yxc-nodejs').YamahaYXC;
module.exports = class Scenario {
/**
Expects a configuration in the form :
{
"source": <hostname/ip of the MusicCast device to listen to volume changes>,
"target": [ <list of hostname/ip of the MC devices to reflect the volume of the source device> ]
E.g. { "source": "192.168.1.42", "targets": [ "192.168.1.43", "192.168.1.44" ] }
*/
constructor( configuration ) {
// Instanciates the source device and enrich with technical infos
var source = new YamahaYXC(configuration.source);
source.getDeviceInfo().
then( result => {
// result is a JSON object
source.deviceInfo = result;
log.debug("Source : %s",source);
}
);
this.source = source;
// Instanciates the target devices
this.targets = [];
if ( Array.isArray(configuration.target) ) {
for ( var t=0 ; t<configuration.target.length ; t++ ) {
this.targets.push( new YamahaYXC(configuration.target[t]) );
}
} else {
// We allow 'configuration.target' to be a string if only one value is given
this.targets.push( new YamahaYXC(configuration.target) );
}
log.debug("Targets : %o",this.targets);
}
/**
When the volume of the source has changed, will set the volume of all target devices to the same value.
E.g. event = { main: { volume: 47 }, device_id: 'AC44F2852577' }
*/
onEvent( event ) {
// Filters out events without a volume
if ( !event.main || typeof event.main.volume == 'undefined' ) {
return;
}
// It seems that a device may report for another, linked one => we filter out those events
if ( typeof event.device_id != undefined && event.device_id != this.source.deviceInfo.device_id ) {
return;
}
// OK, let's handle this volume change
log.info("<<< Received a volume update : %j", event);
for ( var t=0 ; t<this.targets.length ; t++ ) {
log.info(">>> Setting volume of %s to %s", this.targets[t].ip, event.main.volume);
this.targets[t].setVolumeTo(event.main.volume);
}
}
}