signalk-multiplexer
Advanced tools
Comparing version 0.1.7 to 0.1.10
@@ -42,12 +42,24 @@ /* | ||
function deltaToNested(delta) { | ||
var result = {}; | ||
function valuesToSubTree(delta) { | ||
var valueTree = {}; | ||
var timestamp = delta.updates[0].source.timestamp; | ||
delete delta.updates[0].source.timestamp; | ||
delta.updates[0].values.forEach(function(pathValue) { | ||
addAsNested(pathValue, delta.updates[0].source, timestamp, result); | ||
addAsNested(pathValue, delta.updates[0].source, timestamp, valueTree); | ||
}); | ||
return valueTree; | ||
} | ||
function deltaToNested(delta) { | ||
var result = {}; | ||
var contextPointer = result; | ||
var pathPropertyNames = delta.context.split('.'); | ||
for (var i = 0; i < pathPropertyNames.length - 1; i++) { | ||
contextPointer[pathPropertyNames[i]] = {}; | ||
contextPointer = contextPointer[pathPropertyNames[i]]; | ||
}; | ||
contextPointer[pathPropertyNames[pathPropertyNames.length -1]] = valuesToSubTree(delta); | ||
return result; | ||
} | ||
module.exports = deltaToNested; | ||
module.exports.deltaToNested = deltaToNested; |
@@ -1,7 +0,18 @@ | ||
var events = require("events") | ||
, uuid = require("node-uuid").v4 | ||
, _ = require("lodash") | ||
, delta = require('./delta') | ||
var events = require("events") | ||
, uuid = require("node-uuid").v4 | ||
, _ = require("lodash") | ||
, deltaToNested = require('./delta').deltaToNested | ||
, debug = require('debug')('signalk:multiplexer') | ||
; | ||
/** | ||
* Multiplexer | ||
* | ||
* Multiplexes any Signal K or Signal K delta input into an in-memory Signal K tree. | ||
* Emits change events when the full tree changes and change:delta events for every delta input or composed input. | ||
* | ||
* @param id {String|Number} UUID or MMSI of localboat | ||
* @param type {String} Type of the id (uuid or mmsi) | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
function Multiplexer(id, type) { | ||
@@ -17,2 +28,6 @@ this._data = {}; | ||
this._type = 'uuid'; | ||
debug("No ID provided, generating a new one (" + this._self + ")."); | ||
} else { | ||
debug("Using provided " + this._type.toUpperCase() + ": " + this._self); | ||
} | ||
@@ -33,4 +48,8 @@ | ||
/* | ||
* MODIFIERS | ||
/** | ||
* Multiplexer.prototype.root | ||
* | ||
* Resets the modifier intance variables. | ||
* | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
@@ -46,2 +65,9 @@ Multiplexer.prototype.root = function() { | ||
/** | ||
* Multiplexer.prototype.self | ||
* | ||
* Sets the modifier variables to select localboat. | ||
* | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.self = function() { | ||
@@ -51,2 +77,10 @@ return this.vessel(this._self); | ||
/** | ||
* Multiplexer.prototype.self | ||
* | ||
* Sets the modifier variables to select vessel with a certain id. | ||
* | ||
* @param id {String|Number} UUID or MMSI of a vessel | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.vessel = function(id) { | ||
@@ -58,2 +92,11 @@ this._setPath = 'vessels.' + id; | ||
/** | ||
* Multiplexer.prototype.resource | ||
* | ||
* Sets the modifier variables to select a resource of a certain type with a certain ID. | ||
* | ||
* @param sub {String} Resource type | ||
* @param id {String|Number} ID of a resource | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.resource = function(sub, id) { | ||
@@ -65,2 +108,10 @@ this._setPath = 'resources.' + sub + '.' + id; | ||
/** | ||
* Multiplexer.prototype.group | ||
* | ||
* Sets the modifier variables to select a certain group. | ||
* | ||
* @param key {String} Group name | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.group = function(key) { | ||
@@ -71,2 +122,10 @@ this._setPath += '.' + key; | ||
/** | ||
* Multiplexer.prototype.timestamp | ||
* | ||
* Sets the modifier variable 'timestamp' to a certain timestamp. | ||
* | ||
* @param date {String|Date} Timestamp | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.timestamp = function(date) { | ||
@@ -77,2 +136,10 @@ this._setTimestamp = date; | ||
/** | ||
* Multiplexer.prototype.source | ||
* | ||
* Sets the modifier variable 'source' to a certain source. | ||
* | ||
* @param source {Object} Source | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.source = function(source) { | ||
@@ -83,2 +150,11 @@ this._setSource = source; | ||
/** | ||
* Multiplexer.prototype.set | ||
* | ||
* Set a property 'prop' to value 'val'. | ||
* | ||
* @param prop {String} Property to set value to | ||
* @param val {Mixed} Value to set | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.set = function(prop, val) { | ||
@@ -97,2 +173,11 @@ this._last = _.clone(this._data, true); | ||
/** | ||
* Multiplexer.prototype.value | ||
* | ||
* Set a property 'prop' to value 'val', with a timestamp and source. | ||
* | ||
* @param prop {String} Property to set value to | ||
* @param val {Mixed} Value to set | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.value = function(prop, val) { | ||
@@ -130,2 +215,10 @@ var ts, src; | ||
/** | ||
* Multiplexer.prototype.values | ||
* | ||
* Set an array of properties 'prop' and values 'val', each with a timestamp and source. | ||
* | ||
* @param arr {Array} Array of key:value pairs to set. | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.values = function(arr) { | ||
@@ -168,4 +261,67 @@ var ts, src; | ||
/** | ||
* Multiplexer.prototype.retrieve | ||
* | ||
* Return a clone of the in-memory Signal K tree. | ||
* | ||
* @return {Object} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.retrieve = function() { | ||
return _.clone(this._data, true); | ||
}; | ||
/** | ||
* Multiplexer.prototype.add | ||
* | ||
* Merge a valid Signal K object into the in-memory Signal K tree. | ||
* | ||
* @param input {Object} Delta or full Signal K object. | ||
* @return this {Instance} Instance of Multiplexer | ||
*/ | ||
Multiplexer.prototype.add = function(input) { | ||
var delta = false; | ||
var data = input; | ||
if(this.__isDelta(input)) { | ||
data = deltaToNested(input); | ||
delta = true; | ||
} | ||
if(!this.__isValid(data)) { | ||
throw new Error("Invalid input: " + JSON.stringify(data)); | ||
} | ||
this._last = _.clone(this._data, true); | ||
this._data = _.merge(this._data, data); | ||
if(this.hasChanged()) { | ||
this._lastChange = Date.now(); | ||
this.emit('change', this._data); | ||
if(delta === true) { | ||
this.emit('change:delta', input); | ||
} | ||
} | ||
return this; | ||
}; | ||
/** | ||
* Multiplexer.prototype.hasChanged | ||
* | ||
* Returns true or false depending on wether the in-memory object has changed. | ||
* | ||
* @return {Boolean} true or false - depending on wether the in-memory object has changed. | ||
*/ | ||
Multiplexer.prototype.hasChanged = function() { | ||
if(this._lastChange === -1 || (Date.now() - this._lastChange) > 30000) { | ||
return true; | ||
} | ||
return !(_.isEqual(this._data, this._last)); | ||
}; | ||
/* | ||
* PRIVATE METHODS | ||
* Generate a delta update | ||
*/ | ||
@@ -205,2 +361,5 @@ Multiplexer.prototype.__generateDelta = function(context, path, value, timestamp, source) { | ||
/* | ||
* Select something in this._data by String path. | ||
*/ | ||
Multiplexer.prototype.__select = function(path) { | ||
@@ -223,14 +382,5 @@ var last = this._data; | ||
Multiplexer.prototype.__setModifiers = function(id, primary, secondary) { | ||
this._addToPrimary = primary || 'vessels'; | ||
this._addToSecondary = secondary || ''; | ||
this._addToId = id || this._self; | ||
return this; | ||
}; | ||
Multiplexer.prototype.__parseDelta = function(input) { | ||
return delta(input); | ||
}; | ||
/* | ||
* Check if an input object is a delta object. | ||
*/ | ||
Multiplexer.prototype.__isDelta = function(input) { | ||
@@ -253,2 +403,5 @@ // @TODO: improve this fn. checking is very rudimentary atm | ||
/* | ||
* Check if the input object is valid. | ||
*/ | ||
Multiplexer.prototype.__isValid = function(input) { | ||
@@ -261,56 +414,2 @@ // @TODO | ||
return true; | ||
}; | ||
/* | ||
* LEGACY METHODS | ||
*/ | ||
Multiplexer.prototype.retrieve = function() { | ||
// console.log(JSON.stringify(this._data, null, 2)); | ||
return _.clone(this._data, true); | ||
}; | ||
Multiplexer.prototype.add = function(input) { | ||
// @TODO | ||
var delta = false; | ||
var data = input; | ||
if(this.__isDelta(input)) { | ||
data = this.__parseDelta(input); | ||
delta = true; | ||
} | ||
if(!this.__isValid(data)) { | ||
return; | ||
} | ||
this._last = _.clone(this._data, true); | ||
if(delta === false) { | ||
this._data = _.merge(this._data, data); | ||
} else { | ||
for(var prop in data) { | ||
if(data.hasOwnProperty(prop)) { | ||
this.__select(input.context)[prop] = data[prop]; | ||
} | ||
} | ||
} | ||
if(this.hasChanged()) { | ||
this._lastChange = Date.now(); | ||
this.emit('change', this._data); | ||
if(delta === true) { | ||
this.emit('change:delta', input); | ||
} | ||
// console.log(JSON.stringify(this._data, null, 2)); | ||
} | ||
}; | ||
Multiplexer.prototype.hasChanged = function() { | ||
if(this._lastChange === -1 || (Date.now() - this._lastChange) > 30000) { | ||
return true; | ||
} | ||
return !(_.isEqual(this._data, this._last)); | ||
}; |
{ | ||
"name": "signalk-multiplexer", | ||
"version": "0.1.7", | ||
"version": "0.1.10", | ||
"description": "A multiplexer for Signal K that merges any incoming Signal K (either full or delta) into an in-memory Signal K tree, tracks changes and emits events when appropiate. Optionally takes a settings object with vessel information and/or filters. ", | ||
@@ -25,2 +25,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"debug": "^2.1.0", | ||
"lodash": "^2.4.1", | ||
@@ -27,0 +28,0 @@ "node-uuid": "^1.4.1" |
@@ -96,3 +96,3 @@ (function() { | ||
"device" : "/dev/actisense", | ||
"timestamp": "2014-08-15-16:00:05.538", | ||
"timestamp": "2014-05-03T09:14:11.000Z", | ||
"src": "115" | ||
@@ -109,3 +109,3 @@ }, | ||
"device" : "/dev/actisense", | ||
"timestamp":"2014-08-15-16:00:00.081", | ||
"timestamp":"2014-05-03T09:14:11.000Z", | ||
"src":"115", | ||
@@ -170,3 +170,3 @@ "pgn":"128267" | ||
"test": "suite", | ||
"timestamp": "2012-01-31T23:00:00.000Z" | ||
"timestamp": new Date(2012, 1).toISOString() | ||
}, | ||
@@ -191,3 +191,3 @@ "values": [ | ||
"test": "suite", | ||
"timestamp": "2011-01-31T23:00:00.000Z" | ||
"timestamp": new Date(2011, 1).toISOString() | ||
}, | ||
@@ -218,6 +218,6 @@ | ||
"speedOverGround": { | ||
"timestamp": "2012-01-31T23:00:00.000Z", | ||
"timestamp": new Date(2012, 1).toISOString(), | ||
"source": { | ||
"test": "suite", | ||
"timestamp": "2012-01-31T23:00:00.000Z" | ||
"timestamp": new Date(2012, 1).toISOString() | ||
}, | ||
@@ -232,6 +232,6 @@ "value": 2.3 | ||
"speedOverGround": { | ||
"timestamp": "2011-01-31T23:00:00.000Z", | ||
"timestamp": new Date(2011, 1).toISOString(), | ||
"source": { | ||
"test": "suite", | ||
"timestamp": "2011-01-31T23:00:00.000Z" | ||
"timestamp": new Date(2011, 1).toISOString() | ||
}, | ||
@@ -265,3 +265,3 @@ "value": 3.4 | ||
}, | ||
"timestamp": "2014-08-15-16:00:05.538" | ||
"timestamp": "2014-05-03T09:14:11.000Z" | ||
}, | ||
@@ -275,3 +275,3 @@ "log": { | ||
}, | ||
"timestamp": "2014-08-15-16:00:05.538" | ||
"timestamp": "2014-05-03T09:14:11.000Z" | ||
} | ||
@@ -278,0 +278,0 @@ } |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
20673
8
654
1
6
3
+ Addeddebug@^2.1.0
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)