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

feature: add support for multiple NMEA 2000 interfaces #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 36 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,14 @@ module.exports = function(app) {
title: "Conversions to NMEA2000",
description:
"If there is SignalK data for the conversion generate the following NMEA2000 pgns from Signal K data:",
properties: {}
properties: {
defaultOutEvents: {
type: 'string',
title: 'Default NMEA 2000 Json Out Events',
description: 'Comma separated list of event names',
default: 'nmea2000JsonOut'
}
}
};

updateSchema()
Expand All @@ -81,6 +88,11 @@ module.exports = function(app) {
title: 'Resend Duration (seconds)',
description:'The value will be resent for the given #number of seconds',
default: 30
},
outEvents: {
type: 'string',
title: 'NMEA 2000 Json Out Event',
description: 'Comma separated list of event name, leave blank to use the default'
}
}
}
Expand Down Expand Up @@ -108,6 +120,13 @@ module.exports = function(app) {
}

plugin.start = function(options) {
let defaultOutEvents
if ( typeof options.defaultOutEvents === 'undefined' || options.defaultOutEvents === "" ) {
defaultOutEvents = ['nmea2000JsonOut']
} else {
defaultOutEvents = options.defaultOutEvents.split(',')
}

conversions.forEach(conversion => {
if ( !_.isArray(conversion) ) {
conversion = [ conversion ]
Expand All @@ -116,6 +135,14 @@ module.exports = function(app) {
if ( options[conversion.optionKey] && options[conversion.optionKey].enabled ) {
app.debug(`${conversion.title} is enabled`)

let outEventSettings = options[conversion.optionKey].outEvents

if ( typeof outEventSettings === 'undefined' || outEventSettings === '' ) {
conversion.outEvents = defaultOutEvents
} else {
conversion.outEvents = outEventSettings.split(',')
}

var subConversions = conversion.conversions
if ( _.isUndefined(subConversions) ) {
subConversions = [ conversion ]
Expand Down Expand Up @@ -161,7 +188,7 @@ module.exports = function(app) {
}).filter(converter => { return typeof converter !== 'undefined'; });
}

function processBufferOutput(pgns) {
function processBufferOutput(conversion, options, pgns) {
if ( pgns ) {
pgns.filter(pgn => pgn != null).forEach(pgn => {
try {
Expand All @@ -176,12 +203,14 @@ module.exports = function(app) {
}
}

function processToN2K(pgns) {
function processToN2K(conversion, options, pgns) {
if ( pgns ) {
pgns.filter(pgn => pgn != null).forEach(pgn => {
try {
app.debug(`emit nmea2000JsonOut ${JSON.stringify(pgn)}`)
app.emit("nmea2000JsonOut", pgn);
conversion.outEvents.forEach(event => {
app.debug(`emit ${event} ${JSON.stringify(pgn)}`)
app.emit(event, pgn);
})
}
catch ( err ) {
console.error(`error writing pgn ${JSON.stringify(pgn)}`)
Expand All @@ -206,14 +235,14 @@ module.exports = function(app) {
}
const startedAt = Date.now()
conversion.resendTimer = setInterval(() => {
outputTypes[conversion.outputType](output)
outputTypes[conversion.outputType](conversion, options, output)
if ( Date.now() - startedAt > (options.resendTime || 30) * 1000 ) {
clearResendInterval(conversion.resendTimer)
}
}, options.resend * 1000)
timers.push(conversion.resendTimer)
}
outputTypes[conversion.outputType](output)
outputTypes[conversion.outputType](conversion, options, output)
}

function mapBaconjs(conversion, options) {
Expand Down