Skip to content

Commit

Permalink
chore: update to use the latest plugin api (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbender9 authored Jul 11, 2018
1 parent 473b707 commit 36c9457
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 62 deletions.
1 change: 0 additions & 1 deletion conversions/ais.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const _ = require('lodash')
const Concentrate2 = require("concentrate2");
const debug = require("debug")("signalk:signalk-to-nmea2000");

const static_keys = [
"name",
Expand Down
2 changes: 0 additions & 2 deletions conversions/cogSOG.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const _ = require('lodash')
const Concentrate2 = require("concentrate2");
const debug = require("debug")("signalk:signalk-to-nmea2000");

module.exports = (app, plugin) => {
var lastUpdate = null
Expand Down
4 changes: 1 addition & 3 deletions conversions/gps.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const _ = require('lodash')
const Concentrate2 = require("concentrate2");
const debug = require("debug")("signalk:signalk-to-nmea2000");

module.exports = (app, plugin) => {
var lastUpdate = null
Expand All @@ -10,7 +8,7 @@ module.exports = (app, plugin) => {
optionKey: 'GPS_LOCATIONv2',
keys: ["navigation.position"],
callback: (position) => {
//debug(`position: ${JSON.stringify(position)}`)
//app.debug(`position: ${JSON.stringify(position)}`)
var res = [
{
pgn: 129025,
Expand Down
99 changes: 47 additions & 52 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const Bacon = require("baconjs");
const debug = require("debug")("signalk:signalk-to-nmea2000");
const util = require("util");
const { toPgn, toActisenseSerialFormat } = require("canboatjs");
const _ = require('lodash')
Expand Down Expand Up @@ -88,15 +87,13 @@ module.exports = function(app) {
}

plugin.start = function(options) {
debug("start");

conversions.forEach(conversion => {
if ( !_.isArray(conversion) ) {
conversion = [ conversion ]
}
conversion.forEach(conversion => {
if ( options[conversion.optionKey] && options[conversion.optionKey].enabled ) {
debug(`${conversion.title} is enabled`)
app.debug(`${conversion.title} is enabled`)

var subConversions = conversion.conversions
if ( _.isUndefined(subConversions) ) {
Expand Down Expand Up @@ -146,7 +143,7 @@ module.exports = function(app) {
pgns.filter(pgn => pgn != null).forEach(pgn => {
try {
const msg = toActisenseSerialFormat(pgn.pgn, pgn.buffer);
debug("emit " + msg);
app.debug("emit " + msg);
app.emit("nmea2000out", msg);
} catch ( err ) {
console.error(`error writing pgn ${JSON.stringify(pgn)}`)
Expand All @@ -160,12 +157,8 @@ module.exports = function(app) {
if ( pgns ) {
pgns.filter(pgn => pgn != null).forEach(pgn => {
try {
const data = toPgn(pgn)
if ( !_.isUndefined(data) ) {
const msg = toActisenseSerialFormat(pgn.pgn, data);
debug("emit " + msg);
app.emit("nmea2000out", msg);
}
app.debug("emit %j", pgn)
app.emit("nmea2000JsonOut", pgn);
}
catch ( err ) {
console.error(`error writing pgn ${JSON.stringify(pgn)}`)
Expand Down Expand Up @@ -199,7 +192,7 @@ module.exports = function(app) {
try {
processOutput(conversion, conversion.callback(delta))
} catch ( err ) {
console.log(err)
app.error(err)
}
})
}
Expand All @@ -212,7 +205,7 @@ module.exports = function(app) {

function subscription_error(err)
{
console.log("error: " + err)
app.error(err.toString())
}

function mapSubscription(mapping, options) {
Expand All @@ -226,7 +219,7 @@ module.exports = function(app) {
subscription.subscribe.push({ path: key})
});

debug("subscription: " + JSON.stringify(subscription))
app.debug("subscription: " + JSON.stringify(subscription))

app.subscriptionmanager.subscribe(
subscription,
Expand All @@ -236,52 +229,54 @@ module.exports = function(app) {
try {
processToPGNs(mapping.callback(delta, options))
} catch ( err ) {
console.log(err)
app.error(err)
}
});
}
};

function timeoutingArrayStream (
keys,
timeouts = [],
streambundle,
unsubscribes
) {
debug(`keys:${keys}`)
debug(`timeouts:${timeouts}`)
const lastValues = keys.reduce((acc, key) => {
acc[key] = {
timestamp: new Date().getTime(),
value: null
}
return acc
}, {})
const combinedBus = new Bacon.Bus()
keys.map(skKey => {
streambundle.getSelfStream(skKey).onValue(value => {
lastValues[skKey] = {
function timeoutingArrayStream (
keys,
timeouts = [],
streambundle,
unsubscribes
) {
app.debug(`keys:${keys}`)
app.debug(`timeouts:${timeouts}`)
const lastValues = keys.reduce((acc, key) => {
acc[key] = {
timestamp: new Date().getTime(),
value
value: null
}
const now = new Date().getTime()

combinedBus.push(
keys.map((key, i) => {
return notDefined(timeouts[i]) ||
lastValues[key].timestamp + timeouts[i] > now
? lastValues[key].value
: null
})
)
return acc
}, {})
const combinedBus = new Bacon.Bus()
keys.map(skKey => {
streambundle.getSelfStream(skKey).onValue(value => {
lastValues[skKey] = {
timestamp: new Date().getTime(),
value
}
const now = new Date().getTime()

combinedBus.push(
keys.map((key, i) => {
return notDefined(timeouts[i]) ||
lastValues[key].timestamp + timeouts[i] > now
? lastValues[key].value
: null
})
)
})
})
})
const result = combinedBus.debounce(10)
if (debug.enabled) {
unsubscribes.push(result.onValue(x => debug(`${keys}:${x}`)))
const result = combinedBus.debounce(10)
if (app.debug.enabled) {
unsubscribes.push(result.onValue(x => app.debug(`${keys}:${x}`)))
}
return result
}
return result
}

};


const notDefined = x => typeof x === 'undefined'
const isDefined = x => typeof x !== 'undefined'
6 changes: 3 additions & 3 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 @@ -23,7 +23,7 @@
"license": "ISC",
"dependencies": {
"baconjs": "^0.7.88",
"canboatjs": "0.0.2",
"canboatjs": "0.0.9",
"concentrate2": "^1.0.0",
"debug": "^3.1.0",
"github-changes": "^1.1.2",
Expand Down

0 comments on commit 36c9457

Please sign in to comment.