Skip to content

Commit

Permalink
style: autoformat
Browse files Browse the repository at this point in the history
  • Loading branch information
tkurki committed Jul 21, 2023
1 parent fb10988 commit dc29d00
Show file tree
Hide file tree
Showing 61 changed files with 822 additions and 638 deletions.
11 changes: 4 additions & 7 deletions lowrance/65285.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ module.exports = [
source: 'Actual Temperature',
node: function (n2k) {
var temperatureMapping =
temperatureMappings[n2k.fields['Temperature Source']]
if ( temperatureMappings ) {
temperatureMappings[n2k.fields['Temperature Source']]

if (temperatureMappings) {
if (temperatureMapping.pathWithIndex) {
return temperatureMapping.pathWithIndex.replace(
'<index>',
'default'
)
return temperatureMapping.pathWithIndex.replace('<index>', 'default')
} else if (temperatureMapping.path) {
return temperatureMapping.path
}
Expand Down
2 changes: 1 addition & 1 deletion lowrance/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
65285: require('./65285.js')
}
}
9 changes: 5 additions & 4 deletions mmsi-context.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

module.exports.getMmsiContext = function (n2k) {
return typeof n2k.fields['User ID'] !== 'undefined' ? 'vessels.urn:mrn:imo:mmsi:' + n2k.fields['User ID'] : undefined
return typeof n2k.fields['User ID'] !== 'undefined'
? 'vessels.urn:mrn:imo:mmsi:' + n2k.fields['User ID']
: undefined
}

function padUserID(n2k) {
function padUserID (n2k) {
let id = n2k.fields['User ID']
if ( typeof id !== 'undefined' ) {
if (typeof id !== 'undefined') {
id = id.toString()
return id != '0' ? id.padStart(9, '0') : id
}
Expand Down
150 changes: 85 additions & 65 deletions n2kMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var debug = require('debug')('signalk:n2k-signalk')
const toPgn = require('@canboat/canboatjs').toPgn
const Uint64LE = require('int64-buffer').Uint64LE

require('util').inherits(N2kMapper, EventEmitter);
require('util').inherits(N2kMapper, EventEmitter)

var n2kMappings = {}
Object.assign(n2kMappings, require('./pgns'))
Expand All @@ -16,39 +16,43 @@ Object.assign(n2kMappings, require('./actisense'))
Object.assign(n2kMappings, require('./digitalyacht'))

function N2kMapper (options) {
this.state = { }
this.state = {}
this.unknownPGNs = {}
this.customPgns = {}
this.options = options || {}
if ( this.options.onPropertyValues ) {

if (this.options.onPropertyValues) {
this.options.onPropertyValues('pgn-to-signalk', values => {
values.filter(v => v !== undefined).forEach(pv => {
Object.entries(pv.value).forEach(([pgnNumber, mappings]) => {
if ( n2kMappings[pgnNumber] &&
!this.options.allowCustomPGNOverride ) {
console.error(`pgn ${pgnNumber} can't be overwritten`)
} else {
this.customPgns[pgnNumber] = mappings
debug('registered custom pgn %d by %s', pgnNumber, pv.setter)
}
values
.filter(v => v !== undefined)
.forEach(pv => {
Object.entries(pv.value).forEach(([pgnNumber, mappings]) => {
if (
n2kMappings[pgnNumber] &&
!this.options.allowCustomPGNOverride
) {
console.error(`pgn ${pgnNumber} can't be overwritten`)
} else {
this.customPgns[pgnNumber] = mappings
debug('registered custom pgn %d by %s', pgnNumber, pv.setter)
}
})
})
})
})
}
}

N2kMapper.prototype.n2kOutIsAvailable = function(listener, event) {
N2kMapper.prototype.n2kOutIsAvailable = function (listener, event) {
this.n2kOutEvent = event
this.n2kListener = listener
this.requestAllMeta()
}

N2kMapper.prototype.requestMetaData = function(dst, pgn) {
N2kMapper.prototype.requestMetaData = function (dst, pgn) {
const reqPgn = {
"pgn": 59904,
"dst": dst,
"PGN": pgn
pgn: 59904,
dst: dst,
PGN: pgn
}
debug(`requesting pgn ${pgn} from src ${dst}`)
return new Promise((resolve, reject) => {
Expand All @@ -59,81 +63,92 @@ N2kMapper.prototype.requestMetaData = function(dst, pgn) {
})
}

N2kMapper.prototype.requestMetaPGNs = async function(dst, pgns) {
for ( let i = 0; i < pgns.length; i++ ) {
N2kMapper.prototype.requestMetaPGNs = async function (dst, pgns) {
for (let i = 0; i < pgns.length; i++) {
await this.requestMetaData(dst, pgns[i])
}
}

N2kMapper.prototype.checkSrcMetasAndRetry = function(src) {
if ( src !== "255" ) {
N2kMapper.prototype.checkSrcMetasAndRetry = function (src) {
if (src !== '255') {
const neededPGNs = Object.keys(metaPGNs).filter(pgn => {
return !this.state[src].metaPGNsReceived ||
return (
!this.state[src].metaPGNsReceived ||
!this.state[src].metaPGNsReceived[pgn]
)
})
if ( neededPGNs.length > 0 ) {
if (neededPGNs.length > 0) {
debug('did not get meta pgns %j for src %d', neededPGNs, src)
this.requestMetaPGNs(src, neededPGNs)
.then(() => {
neededPGNs.forEach(pgn => {
if (!this.state[src].metaPGNsReceived ||
!this.state[src].metaPGNsReceived[pgn]) {
debug(`did not get meta pgn ${pgn} for src ${src}`)
this.emit('n2kSourceMetadataTimeout', pgn, src)
}
})
this.requestMetaPGNs(src, neededPGNs).then(() => {
neededPGNs.forEach(pgn => {
if (
!this.state[src].metaPGNsReceived ||
!this.state[src].metaPGNsReceived[pgn]
) {
debug(`did not get meta pgn ${pgn} for src ${src}`)
this.emit('n2kSourceMetadataTimeout', pgn, src)
}
})
})
}
}
}

N2kMapper.prototype.requestAllMeta = function() {
this.requestMetaPGNs(255, Object.keys(metaPGNs))
.then(() => {
Object.keys(this.state).forEach(src => this.checkSrcMetasAndRetry(src))
})
N2kMapper.prototype.requestAllMeta = function () {
this.requestMetaPGNs(255, Object.keys(metaPGNs)).then(() => {
Object.keys(this.state).forEach(src => this.checkSrcMetasAndRetry(src))
})
}

N2kMapper.prototype.toDelta = function(n2k) {
if ( metaPGNs[n2k.pgn] ) {
N2kMapper.prototype.toDelta = function (n2k) {
if (metaPGNs[n2k.pgn]) {
const meta = metaPGNs[n2k.pgn](n2k)
if ( ! this.state[n2k.src] ) {
if (!this.state[n2k.src]) {
this.state[n2k.src] = {}
}

if ( !this.state[n2k.src].metaPGNsReceived ) {
if (!this.state[n2k.src].metaPGNsReceived) {
this.state[n2k.src].metaPGNsReceived = {}
}

if ( n2k.pgn === 60928 ) {
if (n2k.pgn === 60928) {
const canName = new Uint64LE(toPgn(n2k)).toString(16)
if ( ! this.state[n2k.src] ) {
if (!this.state[n2k.src]) {
this.state[n2k.src] = {}
} else if ( this.state[n2k.src].canName && this.state[n2k.src].canName != canName ) {
} else if (
this.state[n2k.src].canName &&
this.state[n2k.src].canName != canName
) {
// clear out any existing state since the src addresses have changed
this.emit('n2kSourceChanged', n2k.src, this.state[n2k.src].canName, canName)
this.emit(
'n2kSourceChanged',
n2k.src,
this.state[n2k.src].canName,
canName
)
this.state[n2k.src] = {}
this.requestMetaData(n2k.src, 126996)
.then(() => {
return this.requestMetaData(n2k.src, 126998)
})
this.requestMetaData(n2k.src, 126996).then(() => {
return this.requestMetaData(n2k.src, 126998)
})
}
this.state[n2k.src].deviceInstance = meta.deviceInstance
meta.canName = canName
this.state[n2k.src].canName = canName
}

this.state[n2k.src].metaPGNsReceived[n2k.pgn] = Date.now()

this.emit('n2kSourceMetadata', n2k, meta)
} else {
if ( !n2kMappings[n2k.pgn] ) {
if ( !this.unknownPGNs[n2k.src] ) {
if (!n2kMappings[n2k.pgn]) {
if (!this.unknownPGNs[n2k.src]) {
this.unknownPGNs[n2k.src] = {}
}
if ( !this.unknownPGNs[n2k.src][n2k.pgn] ) {
if (!this.unknownPGNs[n2k.src][n2k.pgn]) {
this.unknownPGNs[n2k.src][n2k.pgn] = n2k
this.emit('n2kSourceMetadata', n2k, { unknownPGNs: this.unknownPGNs[n2k.src] })
this.emit('n2kSourceMetadata', n2k, {
unknownPGNs: this.unknownPGNs[n2k.src]
})
}
}
return toDelta(n2k, this.state, this.customPgns)
Expand All @@ -144,7 +159,10 @@ var toDelta = function (n2k, state, customPgns = {}) {
try {
var theMappings, customMappings

theMappings = [ ...customPgns[n2k.pgn] || [], ...n2kMappings[n2k.pgn] || []]
theMappings = [
...(customPgns[n2k.pgn] || []),
...(n2kMappings[n2k.pgn] || [])
]

var src_state
if (state) {
Expand All @@ -171,10 +189,10 @@ var toDelta = function (n2k, state, customPgns = {}) {
}
]
}
if ( src_state && src_state.canName ) {
if (src_state && src_state.canName) {
result.updates[0].source.canName = src_state.canName
}
if ( src_state && typeof src_state.deviceInstance !== 'undefined' ) {
if (src_state && typeof src_state.deviceInstance !== 'undefined') {
result.updates[0].source.deviceInstance = src_state.deviceInstance
}
if (
Expand All @@ -186,10 +204,10 @@ var toDelta = function (n2k, state, customPgns = {}) {
result.context = mapping.context(n2k, src_state)
}
})
if ( result.context ) {
if (result.context) {
//filter out invalid mmsi
let last = result.context.lastIndexOf(':')
if ( last != -1 && result.context.slice(last+1).length < 9 ) {
if (last != -1 && result.context.slice(last + 1).length < 9) {
console.error('bad MMSI: ' + result.context)
return
}
Expand Down Expand Up @@ -338,7 +356,7 @@ function addAsNested (pathValue, source, timestamp, result) {
}

const metaPGNs = {
60928: (n2k) => {
60928: n2k => {
return {
uniqueId: n2k.fields['Unique Number'],
manufacturerName: n2k.fields['Manufacturer Code'],
Expand All @@ -347,18 +365,20 @@ const metaPGNs = {
deviceInstanceLower: n2k.fields['Device Instance Lower'],
deviceInstanceUpper: n2k.fields['Device Instance Upper'],
systemInstance: n2k.fields['System Instance'],
deviceInstance: (n2k.fields['Device Instance Upper'] << 3) | n2k.fields['Device Instance Lower']
deviceInstance:
(n2k.fields['Device Instance Upper'] << 3) |
n2k.fields['Device Instance Lower']
}
},
126998: (n2k) => {
126998: n2k => {
return {
installationNote1: n2k.fields['Installation Description #1'],
installationNote2: n2k.fields['Installation Description #2'],
installationNote3: n2k.fields['Installation Description #3'],
manufacturerInfo: n2k.fields['Manufacturer Information']
}
},
126996: (n2k) => {
126996: n2k => {
return {
productName: n2k.fields['Model ID'],
hardwareVersion: n2k.fields['Model Version'],
Expand Down
Loading

0 comments on commit dc29d00

Please sign in to comment.