forked from SignalK/signalk-to-nmea0183
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (102 loc) · 3.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const Bacon = require('baconjs')
const {
toSentence,
computeChecksum,
toHexString,
radsToDeg,
padd,
toNmeaDegrees
} = require('./nmea')
const path = require('path')
const fs = require('fs')
module.exports = function (app) {
var plugin = {
unsubscribes: []
}
plugin.id = 'sk-to-nmea0183'
plugin.name = 'Convert Signal K to NMEA0183'
plugin.description = 'Plugin to convert Signal K to NMEA0183'
plugin.schema = {
type: 'object',
title: 'Conversions to NMEA0183',
description:
'If there is SK data for the conversion generate the following NMEA0183 sentences from Signal K data. For converting NMEA2000 AIS to NMEA 0183 use the signalk-n2kais-to-nmea0183 plugin.',
properties: {}
}
plugin.start = function (options) {
const selfContext = 'vessels.' + app.selfId
const selfMatcher = delta => delta.context && delta.context === selfContext
function mapToNmea (encoder, throttle) {
const selfStreams = encoder.keys.map((key, index) => {
let stream = app.streambundle.getSelfStream(key)
if (encoder.defaults && typeof encoder.defaults[index] != 'undefined') {
stream = stream.merge(Bacon.once(encoder.defaults[index]))
}
return stream
}, app.streambundle)
const sentenceEvent = encoder.sentence ? `g${encoder.sentence}` : undefined
let stream = Bacon.combineWith(function () {
try {
return encoder.f.apply(this, arguments)
} catch (e) {
console.error(e.message)
}
}, selfStreams)
.filter(v => typeof v !== 'undefined')
.changes()
.debounceImmediate(20)
if (throttle) {
stream = stream.throttle(throttle)
}
plugin.unsubscribes.push(
stream
.onValue(nmeaString => {
app.emit('nmea0183out', nmeaString)
if (sentenceEvent) {
app.emit(sentenceEvent, nmeaString)
}
app.debug(nmeaString)
})
)
}
Object.keys(plugin.sentences).forEach(name => {
if (options[name]) {
mapToNmea(plugin.sentences[name], options[getThrottlePropname(name)])
}
})
}
plugin.stop = function () {
plugin.unsubscribes.forEach(f => f())
}
plugin.sentences = loadSentences(app, plugin)
buildSchemaFromSentences(plugin)
return plugin
}
function buildSchemaFromSentences (plugin) {
Object.keys(plugin.sentences).forEach(key => {
var sentence = plugin.sentences[key]
const throttlePropname = getThrottlePropname(key)
plugin.schema.properties[key] = {
title: sentence['title'],
type: 'boolean',
default: false
}
plugin.schema.properties[throttlePropname] = {
title: `${key} throttle ms`,
type: 'number',
default: 0
}
})
}
function loadSentences (app, plugin) {
const fpath = path.join(__dirname, 'sentences')
return fs
.readdirSync(fpath)
.filter(filename => filename.endsWith('.js'))
.reduce((acc, fname) => {
let sentence = path.basename(fname, '.js')
acc[sentence] = require(path.join(fpath, sentence))(app, plugin)
return acc
}, {})
}
const getThrottlePropname = (key) => `${key}_throttle`