+ using Winston as the logging framework

~ fixed : error if no scenario-specific option was given
~ doc fixes
This commit is contained in:
nicobo 2021-06-09 07:55:13 +02:00
parent 4e5cd6b975
commit bd5d37601d
No known key found for this signature in database
GPG key ID: 2581E71C5FA5285F
4 changed files with 25 additions and 11 deletions

View file

@ -31,7 +31,7 @@ Currently the following mappings from source to sound program are hard coded
spotify => music with clear_voice disabled
airplay => music with clear_voice disabled
On the command line, use `-s scripts/audio-profile.js` to enable this script and use the `--conf-audio-profile-source` option to set the hostname or IP address of the receiver.
On the command line, use `-s scripts/audio-profile.js` to enable this script and use the `--conf.audio-profile.source` option to set the hostname or IP address of the receiver.
Top-level options (e.g. `--source`) and configuration file are also valid (see instructions below).
@ -45,8 +45,8 @@ If you have a Yamaha MusicCast receiver (like *CRX N470D*) *wirelessly* connecte
This script will solve this by listening to volume updates on a source device and applying any volume change to one or more target devices.
On the command line, use `-s scripts/sync-volume.js` to enable this script and use the following options :
- `--conf-sync-volume-source` sets the hostname or IP address of the *master* receiver
- `--conf-sync-volume-target` lists the *slave* devices that will be updated with the master's volume. You can separate them with a space or pass the option several times.
- `--conf.sync-volume.source` sets the hostname or IP address of the *master* receiver
- `--conf.sync-volume.target` lists the *slave* devices that will be updated with the master's volume. You can separate them with a space or pass the option several times.
Top-level options (e.g. `--source`) and configuration file are also valid (see instructions below).
@ -60,7 +60,7 @@ The scenarios are `.js` scripts which implement the use cases above. More detail
Use the `-s` command line option to specify which script to load :
node . -s ./scripts/sync-volume.js ./scripts/debug.js --source=192.168.1.42 --target=192.168.1.43 --target=192.168.1.44
node . -s ./scripts/sync-volume.js --source=192.168.1.42 --target=192.168.1.43 --target=192.168.1.44
Or in a configuration file (let's say `config.json`) :
@ -80,7 +80,7 @@ Or in a configuration file (let's say `config.json`) :
Then use the `--config` option :
node . -s ./scripts/sync-volume.js ./scripts/debug.js --config config.json
node . -s ./scripts/sync-volume.js --config config.json
You can define generic options at the top level and scenario-specific options under a prefix named after the script's name (its filename without extension).
For instance with `--source 1.2.3.4 --conf.sync-volume.source 5.6.7.8`, `1.2.3.4` will be used as the *source* parameter by default but `5.6.7.8` will be used for the *sync-volume* scenario only.
@ -131,7 +131,13 @@ Deploy on a swarm cluster :
## Logging and debugging
This will log network activity :
There is a special `scripts/debug.js` script that does nothing but printing debug informations. It is simply loaded as a scenario :
node . -s ./scripts/sync-volume.js ./scripts/debug.js --source=192.168.1.42 ...
This will log network activity (Node.js native) :
NODE_DEBUG="net" node index.js ...

View file

@ -1,5 +1,6 @@
const yargs = require('yargs');
const path = require('path');
const log = require('winston');
const udp = require('dgram');
const server = udp.createSocket('udp4');
const http = require('http');
@ -116,12 +117,12 @@ const argv = yargs
type: 'array',
demandOption: true
})
// Configuration as a whole .json file
// --config : configuration as a whole .json file
.config()
.help()
.alias('help', 'h')
.argv;
console.log("argv:", argv);
log.debug("argv:", argv);
// Instanciates the handlers for each scenario
var scenarii = [];
@ -134,7 +135,11 @@ for ( var s=0 ; s<scripts.length ; s++ ) {
var scenarioName = path.basename(scenarioModule, path.extname(scenarioModule));
console.log("Scenario name :", scenarioName);
var conf = Object.assign({}, argv, argv.conf[scenarioName]);
// Merges top options and scenario-specific ones (specific overrides top ones)
var conf = Object.assign({}, argv);
if ( argv.conf !== undefined && argv.conf[scenarioName] !== undefined ) {
conf = Object.assign(conf, argv.conf[scenarioName]);
}
console.log("Scenario conf. :", conf);
scenarii.push({

View file

@ -9,6 +9,7 @@
"node": ">=10"
},
"dependencies": {
"winston": "^3.3.3",
"yamaha-yxc-nodejs": "0.0.13",
"yargs": "^16.2.0"
}

View file

@ -1,10 +1,12 @@
const log = require('winston');
module.exports = class Scenario {
constructor( configuration ) {
console.debug("Debug configuration :",configuration);
log.debug("Debug configuration :",configuration);
}
onEvent( event ) {
console.debug("<<<",event);
log.debug("<<<",event);
}
}