diff --git a/index.js b/index.js index c273c0403..6aa00c764 100644 --- a/index.js +++ b/index.js @@ -19,5 +19,4 @@ * */ - module.exports = require('./lib'); \ No newline at end of file diff --git a/lib/config/development.js b/lib/config/development.js index 6d0dcf907..36c75d031 100644 --- a/lib/config/development.js +++ b/lib/config/development.js @@ -21,18 +21,15 @@ (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, @@ -40,7 +37,5 @@ })); app.all('/', require('../controllers/config')); - } - }).call(global); \ No newline at end of file diff --git a/lib/config/index.js b/lib/config/index.js index fbc2f9e11..d16facf8e 100644 --- a/lib/config/index.js +++ b/lib/config/index.js @@ -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(); @@ -66,5 +66,4 @@ require('./development'); require('./production'); - }).call(global); \ No newline at end of file diff --git a/lib/config/production.js b/lib/config/production.js index b624da5af..815139f4c 100644 --- a/lib/config/production.js +++ b/lib/config/production.js @@ -20,8 +20,7 @@ */ - (function() { - +(function() { "use strict"; var express = require('express'); @@ -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); \ No newline at end of file diff --git a/lib/config/staging.js b/lib/config/staging.js index f5f784046..6bd3b0b61 100644 --- a/lib/config/staging.js +++ b/lib/config/staging.js @@ -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); \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index aec66b9cc..f9f5cb6a9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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(); @@ -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); \ No newline at end of file diff --git a/lib/lib/Provider.js b/lib/lib/Provider.js index 7251b992e..579777040 100644 --- a/lib/lib/Provider.js +++ b/lib/lib/Provider.js @@ -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); } @@ -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); } diff --git a/lib/lib/cli.js b/lib/lib/cli.js new file mode 100644 index 000000000..85a995696 --- /dev/null +++ b/lib/lib/cli.js @@ -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); \ No newline at end of file diff --git a/lib/providers.js b/lib/providers.js index f4fbcbd46..de7eea34f 100644 --- a/lib/providers.js +++ b/lib/providers.js @@ -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); \ No newline at end of file diff --git a/package.json b/package.json index ca9bb1294..90ca64b2b 100644 --- a/package.json +++ b/package.json @@ -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" - } -} \ No newline at end of file + "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 ", + "contributors": [ + { + "name": "Teppo Kurki", + "email": "teppo.kurki@iki.fi" + } + ], + "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" + } +} diff --git a/providers/execute.js b/providers/execute.js index 13fa9ba3e..aa9c78cb9 100644 --- a/providers/execute.js +++ b/providers/execute.js @@ -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({ @@ -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 {