-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (112 loc) · 3.29 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
117
118
119
120
121
const PLUGIN_ID = 'RegExp-JEXL-reader';
const pkgData = require('./package.json')
const jexl = require("jexl");
module.exports = function (app) {
let onStop = []
return {
start: options => {
app.debug(options)
if (options.mappings) {
options.mappings.forEach(mapping => {
mapping.regex = new RegExp(mapping.pattern)
if (mapping.calculations) {
mapping.calculations.forEach(calculation => {
calculation.jexl = jexl.compile(calculation.expression)
});
}
});
const send = message => {
options.mappings.forEach(mapping => {
let match = message.match(mapping.regex)
if (match) {
mapping.calculations.forEach(calculation => {
app.debug(`Found match in ${match[0]} and applying ${calculation.expression}`)
let value = calculation.jexl.evalSync({m: match})
app.debug(`Calculated new value ${value} for path ${calculation.path}`)
let delta = {
values: [{
path: calculation.path,
value: value
}],
$source: mapping.pattern
}
app.handleMessage(PLUGIN_ID, {updates: [delta]});
});
}
});
}
if (typeof options.nmea0183 === 'undefined' || options.nmea0183) {
app.signalk.on('nmea0183', send)
onStop.push(() => {
app.signalk.removeListener('nmea0183', send)
})
}
if (typeof options.nmea0183out === 'undefined' || options.nmea0183out) {
app.on('nmea0183out', send)
onStop.push(() => {
app.removeListener('nmea0183out', send)
})
}
app.setProviderStatus('Running')
} else {
app.setProviderStatus('No mappings defined')
}
},
stop: () => {
onStop.forEach(f => f())
onStop = []
},
schema,
id: PLUGIN_ID,
name: pkgData.description
}
}
function schema () {
return {
type: 'object',
properties: {
nmea0183: {
type: 'boolean',
title: 'Use server event nmea0183',
default: true
},
nmea0183out: {
type: 'boolean',
title: 'Use server event nmea0183out',
default: true
},
mappings: {
type: 'array',
title: 'Mappings:',
items: {
type: 'object',
title: 'Mapping',
properties: {
pattern: {
type: 'string',
title: "Regular expression pattern"
},
calculations: {
type: 'array',
title: 'Calculations',
items: {
type: 'object',
title: 'Calculation',
properties: {
expression: {
type: 'string',
title: 'Expression'
},
path: {
type: 'string',
title: 'SignalK path'
}
}
}
}
}
}
}
}
}
}