Skip to content

Commit

Permalink
feature: limit the transmission rates of pgns per the n2k spec
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 committed Jun 5, 2019
1 parent 23cac86 commit 209e739
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 56 deletions.
5 changes: 3 additions & 2 deletions conversions/ais.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = (app, plugin) => {
sourceType: 'onDelta',
//outputType: 'buffer',
optionKey: 'AISv2',
pgns: [ static_pgn, position_pgn, aton_pgn ],
callback: (delta) => {
var selfContext = 'vessels.' + app.selfId

Expand Down Expand Up @@ -191,11 +192,11 @@ function generatePosition(vessel, mmsi, delta) {
status = 0
}

if ( cog > Math.PI*2 ) {
if ( cog >= Math.PI*2 ) {
cog = undefined
}

if ( heading > Math.PI*2 ) {
if ( heading >= Math.PI*2 ) {
heading = undefined
}

Expand Down
1 change: 1 addition & 0 deletions conversions/battery.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = (app, plugin) => {
return {
title: 'Battery (127506 & 127508)',
optionKey: 'BATTERYv2',
pgns: [127506, 127508],
context: 'vessels.self',
properties: {
batteries: {
Expand Down
1 change: 1 addition & 0 deletions conversions/cogSOG.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = (app, plugin) => {
return {
title: 'COG & SOG (129026)',
optionKey: 'COG_SOGv2',
pgns: [ 129026 ],
keys: ["navigation.courseOverGroundTrue", "navigation.speedOverGround"],
callback: (course, speed) => {
try {
Expand Down
1 change: 1 addition & 0 deletions conversions/depth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = (app, plugin) => {
return {
title: 'Depth (128267)',
optionKey: 'DEPTHv2',
pgns: [ 128267 ],
keys: ["environment.depth.belowTransducer"],
callback: (belowTransducer) => {
var surfaceToTransducer = _.get(app.signalk.self,
Expand Down
2 changes: 2 additions & 0 deletions conversions/engineParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = (app, plugin) => {
return [{
title: 'Temperature, exhaust (130312)',
optionKey: 'EXHAUST_TEMPERATURE',
pgns: [ 130312 ],
context: 'vessels.self',
properties: {
engines: {
Expand Down Expand Up @@ -74,6 +75,7 @@ module.exports = (app, plugin) => {
{
title: 'Engine Parameters (127489,127488)',
optionKey: 'ENGINE_PARAMETERS',
pgns: [ 127489, 127488 ],
context: 'vessels.self',
properties: {
engines: {
Expand Down
58 changes: 25 additions & 33 deletions conversions/gps.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const _ = require('lodash')

module.exports = (app, plugin) => {
var lastUpdate = null

return {
title: 'Location (129025,129029)',
optionKey: 'GPS_LOCATIONv2',
pgns: [ 129025, 129029 ],
keys: ["navigation.position"],
callback: (position) => {
//app.debug(`position: ${JSON.stringify(position)}`)
Expand All @@ -17,38 +16,31 @@ module.exports = (app, plugin) => {
}
]


if (
lastUpdate == null ||
(new Date().getTime() - lastUpdate.getTime()) > 1000
) {
lastUpdate = new Date()

var dateObj = new Date();
var date = Math.trunc(dateObj.getTime() / 86400 / 1000);
var time =
dateObj.getUTCHours() * (60 * 60) +
dateObj.getUTCMinutes() * 60 +
dateObj.getUTCSeconds();

res.push({
pgn: 129029,
Date: date,
Time: time,
Latitude: position.latitude,
Longitude: position.longitude,
'GNSS type': 'GPS+SBAS/WAAS',
Method: 'DGNSS fix',
Integrity: 'No integrity checking',
'Number of SVs': 16,
HDOP:0.64,
'Geoidal Separation': -0.01,
'Reference Stations': 1,
'Reference Station Type': 'GPS+SBAS/WAAS',
'Reference Station ID': 7
var dateObj = new Date();
var date = Math.trunc(dateObj.getTime() / 86400 / 1000);
var time =
dateObj.getUTCHours() * (60 * 60) +
dateObj.getUTCMinutes() * 60 +
dateObj.getUTCSeconds();

res.push({
pgn: 129029,
Date: date,
Time: time,
Latitude: position.latitude,
Longitude: position.longitude,
'GNSS type': 'GPS+SBAS/WAAS',
Method: 'DGNSS fix',
Integrity: 'No integrity checking',
'Number of SVs': 16,
HDOP:0.64,
'Geoidal Separation': -0.01,
'Reference Stations': 1,
'Reference Station Type': 'GPS+SBAS/WAAS',
'Reference Station ID': 7
//'Age of DGNSS Corrections':
})
}
})

return res
}
}
Expand Down
1 change: 1 addition & 0 deletions conversions/heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = (app, plugin) => {
pgn: 127250,
title: 'Heading (127250)',
optionKey: 'HEADINGv2',
pgns: [ 127250 ],
keys: [
"navigation.headingMagnetic",
'navigation.magneticVariation'
Expand Down
1 change: 1 addition & 0 deletions conversions/speed.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = (app, plugin) => {
pgns: [ 128259 ],
title: 'Speed (128259)',
optionKey: 'SPEED',
pgns: 128259,
keys: ["navigation.speedThroughWater"],

callback: (speed) => {
Expand Down
1 change: 1 addition & 0 deletions conversions/systemTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = (app, plugin) => {
sourceType: 'timer',
interval: 1000,
optionKey: 'SYSTEM_TIMEv2',
pgns: [ 126992 ],
callback: (app) => {
var dateObj = new Date();
var date = Math.trunc(dateObj.getTime() / 86400 / 1000);
Expand Down
1 change: 1 addition & 0 deletions conversions/tanks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = (app, plugin) => {
return {
title: 'Tank Levels (127505)',
optionKey: 'TANKS',
pgns: [ 127505 ],
context: 'vessels.self',
properties: () => {
var tanks = _.get(app.signalk.self, 'tanks')
Expand Down
4 changes: 4 additions & 0 deletions conversions/temperature.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = (app, plugin) => {
pgn: 130312,
title: 'Outside Temperature (130312)',
optionKey: 'TEMPERATURE_OUTSIDE',
pgns: [ 130312 ],
keys: [
"environment.outside.temperature"
],
Expand All @@ -26,6 +27,7 @@ module.exports = (app, plugin) => {
pgn: 130312,
title: 'Inside Temperature (130312)',
optionKey: 'TEMPERATURE_INSIDE',
pgns: 130312,
keys: [
"environment.inside.temperature"
],
Expand All @@ -37,6 +39,7 @@ module.exports = (app, plugin) => {
pgn: 130312,
title: 'Engine Room Temperature (130312)',
optionKey: 'TEMPERATURE_ENGINEROOM',
pgns: [ 130312 ],
keys: [
"environment.inside.engineRoom.temperature"
],
Expand All @@ -48,6 +51,7 @@ module.exports = (app, plugin) => {
pgn: 130312,
title: 'Refridgerator Temperature (130312)',
optionKey: 'TEMPERATURE_refridgerator',
pgns: [ 130312 ],
keys: [
"environment.inside.refrigerator.temperature"
],
Expand Down
1 change: 1 addition & 0 deletions conversions/wind.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = (app, plugin) => {
return {
title: 'Wind (130306)',
optionKey: 'WINDv2',
pgns: [ 130306 ],
keys: ["environment.wind.angleApparent", "environment.wind.speedApparent"],
callback: (angle, speed) => {
try {
Expand Down
55 changes: 42 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const path = require('path')
const fs = require('fs')

module.exports = function(app) {
var plugin = {};
var plugin = {
lastUpdates: {}
};
var unsubscribes = [];
var timers = []
var conversions = load_conversions(app, plugin)
Expand Down Expand Up @@ -104,6 +106,7 @@ module.exports = function(app) {
if ( subConversions != null ) {
subConversions.forEach(subConversion => {
var type = _.isUndefined(subConversion.sourceType) ? 'onValueChange' : subConversion.sourceType

var mapper = sourceTypes[type]
if ( _.isUndefined(mapper) ) {
console.error(`Unknown conversion type: ${type}`)
Expand Down Expand Up @@ -156,13 +159,22 @@ module.exports = function(app) {
function processToN2K(pgns) {
if ( pgns ) {
pgns.filter(pgn => pgn != null).forEach(pgn => {
try {
app.debug("emit %j", pgn)
app.emit("nmea2000JsonOut", pgn);
}
catch ( err ) {
console.error(`error writing pgn ${JSON.stringify(pgn)}`)
console.error(err.stack)

const key = `${pgn.pgn}.${pgn.Instance}`
const lastUpdate = plugin.lastUpdates[key]
const rate = updateRates[pgn.pgn]
if ( !_.isUndefined(rate) && !_.isUndefined(lastUpdate) && Date.now() - lastUpdate < rate ) {
return
} else {
plugin.lastUpdates[key] = Date.now()
try {
app.debug("emit %j", pgn)
app.emit("nmea2000JsonOut", pgn);
}
catch ( err ) {
console.error(`error writing pgn ${JSON.stringify(pgn)}`)
console.error(err.stack)
}
}
})
}
Expand All @@ -172,7 +184,9 @@ module.exports = function(app) {
outputTypes[conversion.outputType](output)
}

function mapBaconjs(conversion) {
function mapBaconjs(conversion, options) {
let lastUpdate

unsubscribes.push(
timeoutingArrayStream(
conversion.keys,
Expand All @@ -187,7 +201,7 @@ module.exports = function(app) {
);
}

function mapOnDelta(conversion) {
function mapOnDelta(conversion, options) {
app.signalk.on('delta', (delta) => {
try {
processOutput(conversion, conversion.callback(delta))
Expand Down Expand Up @@ -271,13 +285,28 @@ module.exports = function(app) {
})
const result = combinedBus.debounce(10)
if (app.debug.enabled) {
unsubscribes.push(result.onValue(x => app.debug(`${keys}:${x}`)))
unsubscribes.push(result.onValue(x => app.debug(`${keys}:${JSON.stringify(x)}`)))
}
return result
}

};
}


const notDefined = x => typeof x === 'undefined'
const isDefined = x => typeof x !== 'undefined'

const updateRates = {
127506: 1500,
127508: 1500,
129026: 250,
128267: 1000,
130312: 2000,
127489: 500,
129025: 100,
129029: 1000,
127250: 100,
126992: 1000,
127505: 2500,
130306: 100
}

16 changes: 8 additions & 8 deletions package-lock.json

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

0 comments on commit 209e739

Please sign in to comment.