forked from raffmont/signalk-to-stalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (115 loc) · 3.4 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// From the stalk.js file, import: toDatagram, computeChecksum, toHexString, a>
const {
toDatagram,
computeChecksum,
toHexString,
padd
} = require('./stalk')
// Import path module
const path = require('path')
// Import file system module
const fs = require('fs')
// Load the handlers from the handlers folder
function loadHandlers (app, plugin) {
// Define where the handlers are located
const fpath = path.join(__dirname, 'handlers')
// Return the handlers dictionary:
// handler name as a key, handler definition as value
return fs
// Read the directory synchronously
.readdirSync(fpath)
// Filter only the .js files
.filter(filename => filename.endsWith('.js'))
// For each file in the directory
.reduce((acc, fname) => {
// Import the module defining the handler
handler = require(path.join(fpath, fname))(app, plugin)
// Add the enable/disable handler entry in the configuration schema
plugin.schema.properties[handler.path] = {
title: handler['title'],
type: 'boolean',
default: false
}
acc[handler.path] = handler
// Return the handler definition
return acc
}, {})
}
module.exports = function (app) {
var plugin = {
id: 'signalk-to-stalk',
name: 'Convert Signal K to STALK',
description: 'Signal K server plugin to convert Signal K to STALK',
schema: {
type: 'object',
title: 'Conversions to STALK',
description: 'Convert the following data to STALK',
properties: {
"event_identifier": {
type: 'string',
title: "Event identifier",
default: "seatalkOut"
}
}
},
unsubscribes: []
};
plugin.start = function (options, restartPlugin) {
// Here we put our plugin logic
app.debug('Plugin started');
// Load the datagrams from the datagrams directory
plugin.handlers = loadHandlers(app, plugin)
var subscribes = []
Object.keys(plugin.handlers).forEach( key => {
if (options[key]) {
app.debug("Added: " + key)
subscribe = {
path: key
}
if ("period" in plugin.handlers[key]) {
subscribe.period = plugin.handlers[key].period
}
if ("policy" in plugin.handlers[key]) {
subscribe.policy = plugin.handlers[key].policy
}
if ("minPeriod" in plugin.handlers[key]) {
subscribe.minPeriod = plugin.handlers[key].minPeriod
}
subscribes.push( subscribe )
}
})
let localSubscription = {
context: 'vessels.' + app.selfId, // Get data for all contexts
subscribe: subscribes
};
app.subscriptionmanager.subscribe(
localSubscription,
plugin.unsubscribes,
subscriptionError => {
app.error('Error:' + subscriptionError);
},
delta => {
delta.updates.forEach(u => {
u.values.forEach(v => {
app.debug("v.path: " + v.path)
if (v.path in plugin.handlers) {
let datagrams = plugin.handlers[v.path].handler(v.value)
datagrams.forEach( datagram => {
app.debug(datagram)
// Emit the event on the associated ST2USB device
app.emit(options["event_identifier"], datagram)
})
}
});
});
}
);
};
plugin.stop = function () {
// Here we put logic we need when the plugin stops
app.debug('Plugin stopped');
plugin.unsubscribes.forEach(f => f());
plugin.unsubscribes = [];
};
return plugin;
};