Skip to content

Commit

Permalink
Merge pull request #1 from SignalK/plugin-providers
Browse files Browse the repository at this point in the history
Fresh start
  • Loading branch information
Fabian Tollenaar committed Sep 10, 2014
2 parents 9afb0e1 + 9d6f435 commit 726806d
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 82 deletions.
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

// start server
https.listen(app.config.port, function() {
// Require bonjour/zeroconf helper
require('./lib/bonjour');
/* Zeroconf stuff. Unused atm
require('./lib/bonjour');
app.on('remote-server::up', function(SignalKServer) {
console.log('remote-server::up', SignalKServer.id);
Expand All @@ -52,6 +52,7 @@
app.on('remote-server::down', function(id) {
console.log('remote-server::down', id);
});
// */

// Initiate the Multiplexer...
app.multiplexer = new Multiplexer();
Expand Down
61 changes: 17 additions & 44 deletions lib/lib/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
*/


var _ = require('lodash')
, colors = require('colors')
var _ = require('lodash')
, colors = require('colors')
, uuid = require('node-uuid')
, fork = require('child_process').fork
, path = require('path')
Expand All @@ -30,13 +30,14 @@ var _ = require('lodash')
function Provider(provider, streamFn, debug) {
// Provider information
this.debug = !!debug;
//this.debug = true;
this.name = '';
this.id = '';
this.identity = null;
this.id = '';
this.identity = null;
this.fork = null;
this._to = null;
this._to = null;
this.sleep = 1000;
this.streamFn = streamFn;
this.streamFn = streamFn;

this.init(provider);
}
Expand All @@ -48,19 +49,19 @@ Provider.prototype.init = function(provider) {
};

Provider.prototype.start = function() {
this.log('Starting new fork');
this.log('Starting Provider', this.name);

var self = this;

// CREATE FORK
this.fork = fork(path.join(__dirname, '../../providers/', this.name));
// SETUP DIFFERENT LISTENERS
this.fork.on('message', function() { self.handleMessage.apply(self, arguments); });
this.fork.on('error', function() { self.handleError.apply(self, arguments); });
this.fork.on('close', function() { self.handleClose.apply(self, arguments); });
this.fork.on('disconnect', function() { self.handleDisconnect.apply(self, arguments); });
this.fork.on('exit', function() { self.handleExit.apply(self, arguments); });
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);
} catch(err) {
this.log("Provider doesn't exist or has no init method.", err);
}
};

Provider.prototype.handleMessage = function(data) {
Expand All @@ -78,34 +79,6 @@ Provider.prototype.handleMessage = function(data) {
}
};

Provider.prototype.handleError = function() {
if(this.debug) this.log('Provider send an error', arguments);
var self = this;
clearTimeout(this._to);
this._to = setTimeout(function() { self.start(); }, this.sleep);
};

Provider.prototype.handleClose = function() {
if(this.debug) this.log('Provider send close', arguments);
var self = this;
clearTimeout(this._to);
this._to = setTimeout(function() { self.start(); }, this.sleep);
};

Provider.prototype.handleDisconnect = function() {
if(this.debug) this.log('Provider send disconnect', arguments);
var self = this;
clearTimeout(this._to);
this._to = setTimeout(function() { self.start(); }, this.sleep);
};

Provider.prototype.handleExit = function() {
if(this.debug) this.log('Provider send exit', arguments);
var self = this;
clearTimeout(this._to);
this._to = setTimeout(function() { self.start(); }, this.sleep);
};

Provider.prototype.log = function() {
var date = new Date();
var z = function(n) { if(parseInt(n) > 9) { return "" + n; } else { return "0" + n; } };
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "saildata-server",
"version": "0.1.11",
"version": "0.1.12",
"description": "An implementation of a [Signal K](http://signalk.github.io) central server for boats.",
"main": "index.js",
"scripts": {
Expand Down
70 changes: 70 additions & 0 deletions providers/execute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
*
* prototype-server: An implementation of a Signal K server for boats.
* Copyright (C) 2014 Fabian Tollenaar <[email protected]>,
* Teppo Kurki <[email protected]> *et al*.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

exports.init = function(settings, send, _debug) {
var debug = !!_debug;
var spawn = require('child_process').spawn;

/* SEND IDENTITY ON FORK START */
send({
messageType: 'identity', // in order to find out if the message contains actual data or the Provider's identity
payload: {
name: 'execute', // name of Provider
version: '0.0.1', // version of Provider
author: 'Fabian Tollenaar <[email protected]>', // author of Provider
provides: 'Execute any (Signal K producing) command set by user.', // description of what the Provider provides
capabilities: ['*'], // capabilities/tags
protocol: 'Signal K', // protocol of the actual data. Should be signal K

device: { // device information
protocol: '-', // original protocol
type: 'command', // device type
location: '-', // device location
manufacturer: '-', // device manufacturer
product: '-', // product identifier
serial: '-' // serial number
}
}
});

// 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.stdout.on('data', function(data) {
try {
send(JSON.parse(data.toString()));
} catch(err) {
if(debug === true) console.log('[provider-execute] error', err.toString());
}
});

command.stderr.on('data', function(err) {
if(debug === true) console.log('[provider-execute] error', err.toString());
});

command.on('exit', function(code) {
if(debug === true) console.log('[provider-execute] exit', code);
});
});
}

66 changes: 32 additions & 34 deletions providers/filerunner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,20 @@
*
*/


if(typeof process.send !== 'function') {
return process.exit();
}

var Parser, fs, reader, filestream, parser, mw, colors, counter, Throttle, throttled, last, path, vessel, FILE;

// FILE = 'gofree-merrimac.log';
// FILE = 'gps.log';
FILE = 'plaka.log';
FILE = 'plaka.log';

path = require('path');
path = require('path');
vessel = require(path.normalize(__dirname + '/../../settings.json')).vessel;
colors = require('colors');
fs = require('fs');
fs = require('fs');
Parser = require('nmea0183-signalk').Parser;
Throttle = require('stream-throttle').Throttle;

/* SEND IDENTITY ON FORK START */
process.send({
messageType: 'identity', // in order to find out if the message contains actual data or the Provider's identity
payload: {
name: 'filerunner', // name of Provider
version: '0.0.1', // version of Provider
author: 'Fabian Tollenaar <[email protected]>', // author of Provider
provides: 'Loops through a log file of nmea0183 sentences', // description of what the Provider provides
capabilities: ['nmea0183'], // capabilities/tags
protocol: 'Signal K', // protocol of the actual data. Should be signal K

device: { // device information
protocol: 'nmea0183', // original protocol
type: 'file', // device type
location: './nmea.log', // device location
manufacturer: '-', // device manufacturer
product: '-', // product identifier
serial: '-' // serial number
}
}
});

function reader() {
function reader(send) {
filestream = fs.createReadStream(__dirname + '/logs/' + FILE);

parser = new Parser({
Expand All @@ -75,7 +48,7 @@ function reader() {
throttled.pipe(parser);

parser.on('sentence', function(data) {
process.send(data);
send(data);
});

filestream.on('end', function() {
Expand All @@ -85,11 +58,36 @@ function reader() {
setTimeout(function() {
// console.log(((Date.now() - counter) / 1000) + "s. elapsed. Restarting reader.");
// counter = Date.now();
reader();
reader(send);
}, 200);
});
}

reader();
exports.init = function(settings, send, _debug) {
var debug = !!_debug;

/* SEND IDENTITY ON FORK START */
send({
messageType: 'identity', // in order to find out if the message contains actual data or the Provider's identity
payload: {
name: 'filerunner', // name of Provider
version: '0.0.2', // version of Provider
author: 'Fabian Tollenaar <[email protected]>', // author of Provider
provides: 'Loops through a log file of nmea0183 sentences', // description of what the Provider provides
capabilities: ['nmea0183'], // capabilities/tags
protocol: 'Signal K', // protocol of the actual data. Should be signal K

device: { // device information
protocol: 'nmea0183', // original protocol
type: 'file', // device type
location: './nmea.log', // device location
manufacturer: '-', // device manufacturer
product: '-', // product identifier
serial: '-' // serial number
}
}
});

reader(send);
}

2 changes: 1 addition & 1 deletion settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"key": "saildata"
},

"providers": ["provider-filerunner"]
"providers": ["provider-execute", "provider-filerunner"]

}

0 comments on commit 726806d

Please sign in to comment.