Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

osc-bridge: allow specifying config.json on command line #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 55 additions & 22 deletions packages/osc-bridge/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,35 @@
*
* Usage:
*
* $ node cli.js [ -- --auto-ping ]
* $ node cli.js [--auto-ping] [config.json]
*
* Configuration:
*
* Following NPM environment variables should be defined in the `config`
* section of the `package.json` using this package as a dependency:
* The following settings can be defined in `config.json`:
*
* {
* "udp-server": {
* "host": "0.0.0.0",
* "port": "7400"
* },
* "udp-client": {
* "host": "192.168.3.4",
* "port": "7500"
* },
* "ws-server": {
* "host": "0.0.0.0",
* "port": "8080"
* }
* }
*
* Alternatively, the config object can be placed in the `config.osc-bridge`
* section of `package.json` when using this package as a dependency:
*
* {
* …,
* "config": {
* "osc-bridge": {
* "udp-server": {
* "host": "0.0.0.0",
* "port": "7400"
* },
* "udp-client": {
* "host": "192.168.xxx.yyy",
* "port": "7500"
* },
* "ws-server": {
* "host": "0.0.0.0",
* "port": "8080"
* }
* "udp-server": …
* …
* }
* },
* "devDependencies": {
Expand All @@ -42,6 +48,9 @@
* …
* }
*
* All values are optional. Values from `config.json`, if specified, override
* corresponding values from `package.json`.
*
* Options:
*
* * `ws-server` defines the host and port to which the bridge binds
Expand All @@ -67,6 +76,7 @@
* Upon start, this bridge server will list the IP addresses of
* all local network interfaces of its host, to ease setup.
*/
const fs = require( "fs");
const OSC = require( "osc-js");

const AUTO_PING_INTERVAL = 5000; // in milliseconds
Expand Down Expand Up @@ -98,6 +108,21 @@ const oscBridgeConfig = {
}
}

function loadConfig( config) {
if( config['udp-server']) {
oscBridgeConfig.udpServer.host = config['udp-server'].host || oscBridgeConfig.udpServer.host;
oscBridgeConfig.udpServer.port = config['udp-server'].port || oscBridgeConfig.udpServer.port;
}
if( config['udp-client']) {
oscBridgeConfig.udpClient.host = config['udp-client'].host || oscBridgeConfig.udpClient.host;
oscBridgeConfig.udpClient.port = config['udp-client'].port || oscBridgeConfig.udpClient.port;
}
if( config['ws-server']) {
oscBridgeConfig.wsServer.host = config['ws-server'].host || oscBridgeConfig.wsServer.host;
oscBridgeConfig.wsServer.port = config['ws-server'].port || oscBridgeConfig.wsServer.port;
}
}

function getLocalHostIPAddresses() {
const os = require( "os"),
interfaces = os.networkInterfaces(),
Expand Down Expand Up @@ -154,17 +179,25 @@ function autoSendPing( osc, interval) {

// Start the OSC bridge server
console.log( "OSC Websocket <-> UDP bridge server");
const osc = start();

// Enable auto pinging the WS if `--auto-ping` argument was given
const [ , , ...args ] = process.argv;
if( args.indexOf( "--auto-ping") > -1) {
autoSendPing( osc, AUTO_PING_INTERVAL); // interval in milliseconds
const args = process.argv.slice(2);
for( let i = 0; i < args.length; i++ ) {
// Enable auto pinging the WS if `--auto-ping` argument was given
if( args[i] === "--auto-ping") {
autoSendPing( osc, AUTO_PING_INTERVAL); // interval in milliseconds
continue;
}

console.info( `Loading config from '${args[i]}'`);
const config = JSON.parse( fs.readFileSync( args[i]));
loadConfig(config);
}

const osc = start();

// Stop the OSC bridge server on CTRL-C keypress in the terminal
process.on( "SIGINT", function() {
console.info( "Received CTRL-C, stopping OSC bridge.")
osc.close();
process.exit();
});
});