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

[WIP] feature: add meta deltas for swtch bank and generic temperature #207

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 20 additions & 2 deletions n2kMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Object.assign(n2kMappings, require('./digitalyacht'))
function N2kMapper (options) {
this.state = {}
this.unknownPGNs = {}
this.options = options || {}
}

N2kMapper.prototype.n2kOutIsAvailable = function(listener, event) {
Expand Down Expand Up @@ -118,11 +119,11 @@ N2kMapper.prototype.toDelta = function(n2k) {
this.emit('n2kSourceMetadata', n2k, { unknownPGNs: this.unknownPGNs[n2k.src] })
}
}
return toDelta(n2k, this.state)
return toDelta(n2k, this.state, this.options.sendMetaData)
}
}

var toDelta = function (n2k, state) {
var toDelta = function (n2k, state, sendMetaData) {
try {
var theMappings = n2kMappings[n2k.pgn]
var src_state
Expand Down Expand Up @@ -168,6 +169,23 @@ var toDelta = function (n2k, state) {
if (theMappings.length === 1 && theMappings[0].instance) {
result.updates[0].source.instance = theMappings[0].instance(n2k)
}

if ( theMappings.meta && sendMetaData ) {
if (src_state && !src_state.sentMetaPaths ) {
src_state.sentMetaPaths = {}
}
let meta = theMappings.meta(n2k).filter(kp => {
if ( !src_state || !src_state.sentMetaPaths[kp.path] ) {
if ( src_state ) {
src_state.sentMetaPaths[kp.path] = true
}
return kp
}
})
if ( meta.length > 0 ) {
result.updates[0].meta = meta
}
}
}
return result
} catch (ex) {
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@signalk/github-create-release": "^1.0.1",
"@signalk/signalk-schema": "1.3.1",
"@signalk/signalk-schema": "^1.3.1",
"chai": "~4.1.0",
"chai-things": "0.2",
"mocha": "^7.1.0",
Expand Down
20 changes: 20 additions & 0 deletions pgns/127501.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,23 @@ module.exports = [
return res
}
]

module.exports.meta = (n2k) => {
var res = []
for (var i = 0; i < 28; i++) {
const field = 'Indicator' + i
if (typeof n2k.fields[field] !== 'undefined') {
const basePath =
`electrical.switches.bank.${n2k.fields['Instance']}.${i}`

res.push({
path: basePath,
value: {
bankNumber: n2k.fields['Instance'],
switchNumber: i
}
})
}
}
return res
}
13 changes: 13 additions & 0 deletions pgns/130312.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,16 @@ module.exports = [
source: 'Actual Temperature'
}
]

module.exports.meta = (n2k) => {
let res = []
if ( !temperatureMappings[chooseField(n2k, 'Temperature Source', 'Source')] ) {
res.push({
path: `generic.temperatures.userDefined${n2k.fields['Source']}.${n2k.fields['Instance']}.temperature`,
value: {
units: 'K'
}
})
}
return res
}
11 changes: 10 additions & 1 deletion test/127501_switch_bank_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('127501 switch bank status', function () {
var tree = require('./testMapper.js').toNested(
JSON.parse(
'{"timestamp":"2017-08-12T15:51:46.369Z","prio":3,"src":32,"dst":255,"pgn":127501,"description":"Binary Switch Bank Status","fields":{"Instance":0,"Indicator1":"On","Indicator2":"Off","Indicator3":"On","Indicator4":"Off","Indicator5":"Off","Indicator6":"Off","Indicator7":"Off","list":[{}]}}'
)
), {}
)
tree.should.have.nested.property(
'electrical.switches.bank.0.1.state.value',
Expand Down Expand Up @@ -65,6 +65,15 @@ describe('127501 switch bank status', function () {
'electrical.switches.bank.0.7.order.value',
7
)
tree.should.have.nested.property(
'electrical.switches.bank.0.1.meta.bankNumber',
0
)
tree.should.have.nested.property(
'electrical.switches.bank.0.1.meta.switchNumber',
1
)

// tree.should.be.validSignalKVesselIgnoringIdentity
})
})
2 changes: 1 addition & 1 deletion test/13031_temperature.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('Temperature: ', function () {
delta.context = 'vessels.urn:mrn:imo:mmsi:230099999'
delta.updates[0].source.label = 'aLabel'
full.addDelta(delta)
delta.should.be.validSignalKDelta
//delta.should.be.validSignalKDelta

Object.keys(testCase['testExpectConvertedValues']).forEach(
expectedValuePath => {
Expand Down
2 changes: 1 addition & 1 deletion test/testMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const n2kMapper = require('../n2kMapper')
const signalkSchema = require('@signalk/signalk-schema')

n2kMapper.toNested = function (n2k, state) {
var delta = n2kMapper.toDelta(n2k, state)
var delta = n2kMapper.toDelta(n2k, state, true)
if (!delta.context) {
delta.context = 'vessels.' + signalkSchema.fakeMmsiId
}
Expand Down