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

fix: provide a null delta when timeRemaining changes to unknown #74

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
32 changes: 20 additions & 12 deletions n2kMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ var through = require('through');
var debug = require('debug')('signalk:n2k-signalk')


var toDelta = function(n2k) {
var toDelta = function(n2k, state) {
try {
var theMappings = n2kMappings[n2k.pgn];
var src_state
if ( state ) {
var n2k_src = n2k.src.toString()
if ( ! state[n2k_src] ) {
state[n2k_src] = {}
}
src_state = state[n2k_src]
}
var result = {
updates: [{
source: {
Expand All @@ -16,13 +24,13 @@ var toDelta = function(n2k) {
src: n2k.src.toString()
},
timestamp: n2k.timestamp.substring(0, 10) + "T" + n2k.timestamp.substring(11, n2k.timestamp.length),
values: toValuesArray(theMappings, n2k)
values: toValuesArray(theMappings, n2k, src_state)
}]
};
if(typeof theMappings != 'undefined') {
theMappings.forEach(function(mapping) {
if(typeof mapping.context === 'function') {
result.context = mapping.context(n2k);
result.context = mapping.context(n2k, src_state);
}
});
if(theMappings.length === 1 && theMappings[0].instance) {
Expand All @@ -36,7 +44,7 @@ var toDelta = function(n2k) {
}
}

function getValue(n2k, theMapping) {
function getValue(n2k, theMapping, state) {
if(typeof theMapping.source != 'undefined') {
var stringValue = n2k.fields[theMapping.source];
if(!stringValue && stringValue != '') {
Expand All @@ -46,27 +54,27 @@ function getValue(n2k, theMapping) {
return isNaN(numberValue) ? stringValue : numberValue;
} else {
if(theMapping.value) {
return theMapping.value(n2k);
return theMapping.value(n2k, state);
}
}
}

var toValuesArray = function(theMappings, n2k) {
var toValuesArray = function(theMappings, n2k, state) {
if(n2k.fields && typeof theMappings != 'undefined') {
return theMappings
.filter(function(theMapping) {
try {
return typeof theMapping.filter === 'undefined' || theMapping.filter(n2k);
return typeof theMapping.filter === 'undefined' || theMapping.filter(n2k, state);
} catch(ex) {
process.stderr.write(ex + ' ' + n2k);
return false;
}
})
.map(function(theMapping) {
try {
var path = typeof theMapping.node === 'function' ? theMapping.node(n2k) : theMapping.node;
var value = typeof theMapping.source === 'function' ? theMapping.source(n2k) : getValue(n2k, theMapping);
if(!(value == null)) { // null or undefined
var path = typeof theMapping.node === 'function' ? theMapping.node(n2k, state) : theMapping.node;
var value = typeof theMapping.source === 'function' ? theMapping.source(n2k, state) : getValue(n2k, theMapping, state);
if(typeof value !== 'undefined') {
return {
path: path,
value: value
Expand Down Expand Up @@ -122,8 +130,8 @@ function addAsNested(pathValue, source, timestamp, result) {


exports.toDelta = toDelta;
exports.toNested = function(n2k) {
var delta = toDelta(n2k);
exports.toNested = function(n2k, state) {
var delta = toDelta(n2k, state);
if(!delta.context) {
delta.context = "vessels." + signalkSchema.fakeMmsiId;
}
Expand Down
16 changes: 15 additions & 1 deletion pgns/127506.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@ module.exports = [
return 'electrical.batteries.' + n2k.fields['DC Instance'] + '.capacity.stateOfHealth'
}
},{
source: 'Time Remaining',
value: function(n2k, state) {
var val = n2k.fields['Time Remaining']
console.log("state: " + state)
Copy link
Member

Choose a reason for hiding this comment

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

console.log

var res
if ( typeof val !== 'undefined' ) {
res = val * 60; //convert to seconds
} else if ( state && typeof state.lastTimeRemaining !== 'undefined' ) {
res = null;
}
if ( state ) {
state.lastTimeRemaining = val;
}
console.log("returning: " + val)
return res;
},
node: function(n2k) {
return 'electrical.batteries.' + n2k.fields['DC Instance'] + '.capacity.timeRemaining'
}
Expand Down
12 changes: 10 additions & 2 deletions test/127506_dc_detailed_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ var chai = require("chai");
chai.Should();
chai.use(require('chai-things'));

var state = {}

describe('127506 dc detailed status', function () {
it('complete sentence converts', function () {
var tree = require("../n2kMapper.js").toNested(
JSON.parse('{"timestamp":"2016-08-22T16:02:55.272Z","prio":6,"src":17,"dst":255,"pgn":127506,"description":"DC Detailed Status","fields":{"DC Instance":1,"State of Charge":60,"State of Health":99,"Time Remaining": 600, "Ripple Voltage": 10.9, "SID":0}}'));
JSON.parse('{"timestamp":"2016-08-22T16:02:55.272Z","prio":6,"src":17,"dst":255,"pgn":127506,"description":"DC Detailed Status","fields":{"DC Instance":1,"State of Charge":60,"State of Health":99,"Time Remaining": 600, "Ripple Voltage": 10.9, "SID":0}}'), state);
tree.should.have.nested.property('electrical.batteries.1.capacity.stateOfCharge.value', .60);
tree.should.have.nested.property('electrical.batteries.1.capacity.stateOfHealth.value', 99);
tree.should.have.nested.property('electrical.batteries.1.capacity.timeRemaining.value', 600);
tree.should.have.nested.property('electrical.batteries.1.capacity.timeRemaining.value', 36000);
//tree.should.have.nested.property('electrical.batteries.1.voltage.ripple.value', 10.9);
tree.should.be.validSignalKVesselIgnoringIdentity;
});

it('null timeRemaining converts', function () {
var delta = require("../n2kMapper.js").toDelta(
JSON.parse('{"timestamp":"2016-08-22T16:02:55.272Z","prio":6,"src":17,"dst":255,"pgn":127506,"description":"DC Detailed Status","fields":{"DC Instance":1,"SID":0}}'), state);
delta.updates[0].values[0].should.have.property('path', 'electrical.batteries.1.capacity.timeRemaining');
delta.updates[0].values[0].should.have.property('value', null);
});
});