Skip to content

Commit

Permalink
added parsing of CLI arguments and forwarding to Providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Tollenaar committed Sep 14, 2014
1 parent ef3b332 commit bf21b68
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 116 deletions.
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
*
*/


module.exports = require('./lib');
9 changes: 2 additions & 7 deletions lib/config/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,21 @@


(function() {

"use strict";

var express = require('express');

var app = this.app;
var app = this.app;
var config = this.app.config;

if(app.get('env') == 'development') {

config.environment = 'development';
config.debug = true;
config.environment = 'development';

app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));

app.all('/', require('../controllers/config'));

}

}).call(global);
13 changes: 6 additions & 7 deletions lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@

"use strict";

var colors = require('colors');
var fs = require("fs");
var express = require("express");
var colors = require('colors');
var fs = require("fs");
var express = require("express");
var EventEmitter = require("events").EventEmitter;

var app = this.app;
var config = this.app.config = {};
var env = this.app.env = process.env;
var app = this.app;
var config = this.app.config = {};
var env = this.app.env = process.env;

app.event = new EventEmitter();

Expand Down Expand Up @@ -66,5 +66,4 @@

require('./development');
require('./production');

}).call(global);
7 changes: 2 additions & 5 deletions lib/config/production.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
*/


(function() {

(function() {
"use strict";

var express = require('express');
Expand All @@ -32,12 +31,10 @@
if(app.get('env') == 'production') {

config.environment = 'production';
config.debug = false;
config.debug = false;

app.use(express.errorHandler());

app.all('/', require('../controllers/wifi'));

}

}).call(global);
8 changes: 3 additions & 5 deletions lib/config/staging.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
*/


(function() {

(function() {
"use strict";

var app = this.app;
var config = this.app.config;
var app = this.app;
var config = this.app.config;

if(app.get('env') === 'staging') {
config.environment = 'staging';
}

}).call(global);
20 changes: 5 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,15 @@
, io = this.app.io = require('socket.io')(https)
;

// config
require('./config');
// config & options
require('./config');
require('./lib/cli');

// export
module.exports = this.app;

// start server
https.listen(app.config.port, function() {
/* Zeroconf stuff. Unused atm
require('./lib/bonjour');
app.on('remote-server::up', function(SignalKServer) {
console.log('remote-server::up', SignalKServer.id);
});
app.on('remote-server::down', function(id) {
console.log('remote-server::down', id);
});
// */

// Initiate the Multiplexer...
app.multiplexer = new Multiplexer();

Expand All @@ -69,10 +58,11 @@

app.event.emit('server ready', { address: '127.0.0.1', port: app.config.port });
app.log('Server ready and listening on 127.0.0.1:' + app.config.port);
if(app.config.debug === true) app.log('App is in debug mode'.inverse.red);

process.nextTick(function() {
require('./providers');
});
});

}).call(global);
}).call(global);
25 changes: 11 additions & 14 deletions lib/lib/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ var _ = require('lodash')
, path = require('path')
;

function Provider(provider, streamFn, debug) {
function Provider(app, provider, callback) {
// Provider information
this.debug = !!debug;
//this.debug = true;
this.name = '';
this.id = '';
this.identity = null;
this.fork = null;
this._to = null;
this.sleep = 1000;
this.streamFn = streamFn;
this.app = app;
this.debug = !!(this.app.config.debug);
this.name = '';
this.id = '';
this.identity = null;
this.fork = null;
this._to = null;
this.sleep = 1000;
this.streamFn = callback;

this.init(provider);
}
Expand All @@ -54,11 +54,8 @@ Provider.prototype.start = function() {
var self = this;

try {
// LOAD PROVIDER
this.provider = require(path.normalize(__dirname + '/../../providers/' + this.name));
// INIT PROVIDER
// TODO {} = placeholder for link to app/settings/file server (?) class.
this.provider.init({}, function(data) { self.handleMessage.call(self, data) }, this.debug);
this.provider.init(this.app, function(data) { self.handleMessage.call(self, data) }, this.debug);
} catch(err) {
this.log("Provider doesn't exist or has no init method.", err);
}
Expand Down
11 changes: 11 additions & 0 deletions lib/lib/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(function() {
// Attach all argument to a "hidden" instance variable
this.app.__argv = process.argv.slice(2);
// Parse __argv and attach to instance variable argv
this.app.argv = require('minimist')(this.app.__argv);

if(typeof this.app.argv['D'] === 'boolean' || typeof this.app.argv['debug'] === 'boolean') {
if(this.app.argv['D'] === true) this.app.config.debug = true;
if(this.app.argv['debug'] === true) this.app.config.debug = true;
}
}).call(global);
28 changes: 11 additions & 17 deletions lib/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,22 @@

(function() {

var _ = require('lodash');
var _ = require('lodash');
var Provider = require('./lib/Provider');

var app = this.app;
var config = this.app.config;
var providers = this.app.providers = {};
var app = this.app;
var config = this.app.config;
var providers = this.app.providers = {};

var streamFn = function() {
// console.log('streamFn -', arguments);
app.multiplexer.add.apply(app.multiplexer, arguments);
};

_.each(config.settings.providers, function(provider_name) {

// var debug = config.debug;
var debug = false;
var provider = new Provider(provider_name, streamFn, debug);

app.log('Registering provider', provider_name);

providers[provider.id] = provider;


var provider = new Provider(app, provider_name, function() {
// This function is run by Provider instance whenever new data is available
app.multiplexer.add.apply(app.multiplexer, arguments);
});

providers[provider.id] = provider;
});

}).call(global);
89 changes: 50 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
{
"name": "saildata-server",
"version": "0.1.12",
"description": "An implementation of a [Signal K](http://signalk.github.io) central server for boats.",
"main": "index.js",
"scripts": {
"start": "node ./index"
},
"keywords": [
"signalk",
"kjson",
"nmea",
"seatalk",
"gps",
"sailing",
"boat",
"marine",
"nautic"
],
"author": "Fabian Tollenaar",
"license": "GPLv3",
"devDependencies": {
"tape": "^2.12.1"
},
"engine": {
"node": "v0.10.x"
},
"dependencies": {
"colors": "^0.6.2",
"ejson": "^1.0.1",
"express": "3.x",
"lodash": "^2.4.1",
"nmea0183-signalk": "^0.1.12",
"node-uuid": "^1.4.1",
"socket.io": "^1.0.6",
"stream-throttle": "^0.1.3",
"memwatch": "*",
"mdns": "^2.2.0"
}
}
"name": "signalk-server-prototype",
"version": "0.1.13",
"description": "An implementation of a [Signal K](http://signalk.github.io) server for boats.",
"main": "index.js",
"scripts": {
"start": "node ./index"
},
"keywords": [
"signalk",
"kjson",
"nmea",
"seatalk",
"gps",
"sailing",
"boat",
"marine",
"nautic"
],
"license": "GPLv3",
"author": "Fabian Tollenaar <[email protected]>",
"contributors": [
{
"name": "Teppo Kurki",
"email": "[email protected]"
}
],
"repository": {
"type": "git",
"url": "https://github.com/SignalK/server-prototype"
},
"bugs": {
"url": "https://github.com/SignalK/server-prototype/issues"
},
"engine": {
"node": "v0.10.x"
},
"dependencies": {
"colors": "^0.6.2",
"ejson": "^1.0.1",
"express": "3.x",
"lodash": "^2.4.1",
"mdns": "^2.2.0",
"memwatch": "*",
"minimist": "^1.1.0",
"nmea0183-signalk": "^0.1.12",
"node-uuid": "^1.4.1",
"socket.io": "^1.0.6",
"stream-throttle": "^0.1.3"
}
}
15 changes: 9 additions & 6 deletions providers/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
*
*/

exports.init = function(settings, send, _debug) {
var debug = !!_debug;
var spawn = require('child_process').spawn;
exports.init = function(app, send, _debug) {
var command = typeof app.argv['execute'] === 'string' ? app.argv['execute'] : false;

if(!command) return false;

var debug = !!_debug;
var spawn = require('child_process').spawn;

/* SEND IDENTITY ON FORK START */
send({
Expand All @@ -45,10 +49,9 @@ exports.init = function(settings, send, _debug) {
}
});

// TODO retrieve command from settings and add a panel to settings to set the command.

process.nextTick(function() {
var command = spawn('echo', ['{"self":"a34af45a","vessels":{"a34af45a":{"source":{"type":"NMEA0183","label":"signalk/nmea-signalk"},"timestamp":"2014-05-06T15:51:30.241Z","uuid":"a34af45a","navigation":{"courseOverGroundTrue":{"source":{"type":"NMEA0183","sentence":"RMC","device":"nmea-signalk"},"timestamp":"2014-05-03T09:14:11.000Z","value":28.17},"location":{"latitude":52.371901666666666,"longitude":4.90974,"source":{"type":"NMEA0183","sentence":"RMC","device":"nmea-signalk"},"timestamp":"2014-05-03T09:14:11.000Z"},"magneticVariaton":{"source":{"type":"NMEA0183","sentence":"RMC","device":"nmea-signalk"},"timestamp":"2014-05-03T09:14:11.000Z","value":0},"speedOverGround":{"source":{"type":"NMEA0183","sentence":"RMC","device":"nmea-signalk"},"timestamp":"2014-05-03T09:14:11.000Z","value":0.18},"gnss":{"source":{"type":"NMEA0183","sentence":"GGA","device":"nmea-signalk"},"timestamp":"2014-06-06T09:14:00.000Z","quality":1,"satellites":8,"antennaAltitude":1,"horizontalDilution":0,"geoidalSeparation":47,"differentialAge":0,"differentialReference":0},"position":{"source":{"type":"NMEA0183","sentence":"GGA","device":"nmea-signalk"},"timestamp":"2014-06-06T09:14:00.000Z","longitude":52.371903333333336,"latitude":4.909741666666667}}}},"version":"2.0","timestamp":"2014-05-06T15:51:30.123Z","source":{"type":"NMEA0183","label":"signalk/nmea-signalk"}}']);
command = String(command).split(' ');
command = spawn(command[0], command.slice(1));

command.stdout.on('data', function(data) {
try {
Expand Down

1 comment on commit bf21b68

@tkurki
Copy link
Member

@tkurki tkurki commented on bf21b68 Sep 15, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for the moment. This way providers can not register cli arguments, but since they can access the raw args can process all args as they see fit and report whatever they want.

In addition to cli args passed to providers I would like a way to specify the settings file to be used, but go ahead and merge this plz.

Please sign in to comment.