-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (103 loc) · 2.87 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
const id = 'signalk-hypermarket';
const nats = require('nats');
module.exports = function(app) {
var plugin = {
unsubscribes: [],
};
plugin.id = id;
plugin.name = 'SignalK - Hypermarket NATS';
plugin.description =
'Plugin utilizado para enviar a telemetria para o eMarine';
plugin.schema = {
title: 'SignalK - Hypermarket NATS',
type: 'object',
required: ['port'],
properties: {
sendToRemote: {
type: 'boolean',
title: 'Envia dados para o Hypermarket',
default: false,
},
remoteHost: {
type: 'string',
title: 'Hypermarket Cluster URL, default: nats://<localhost>:4222',
description:
'Hypermarket Cluster URL',
default: 'nats://sa-01.hypermarket.io',
},
username: {
type: "string",
title: "Hypermarket Username"
},
password: {
type: "string",
title: "Hypermarket Password"
},
mmsi: {
type: "string",
title: "mmsi vessel",
description: "mmsi vessel",
},
paths: {
type: 'array',
title: 'SignalK self paths para envio',
default: [{ path: 'navigation.position', interval: 60 }],
items: {
type: 'object',
properties: {
path: {
type: 'string',
title: 'Path',
},
interval: {
type: 'number',
title:
'Intervalo minimo para envio',
},
},
},
},
},
};
plugin.onStop = [];
plugin.start = async function(options) {
plugin.onStop = [];
if (options.sendToRemote) {
const nc = await nats.connect({servers: options.remoteHost, user: options.username, pass: options.password});
startSending(options, nc, plugin.onStop);
plugin.onStop.push(_ => nc.drain());
}
};
plugin.stop = function() {
plugin.onStop.forEach(f => f());
};
function startSending(options, nc, onStop) {
options.paths.forEach(pathInterval => {
onStop.push(
app.streambundle
.getSelfBus(pathInterval.path)
.debounceImmediate(pathInterval.interval * 1000)
.onValue(normalizedPathValue =>
nc.publish('signalk.' + options.mmsi + '.' + 'telemetria' + '.' + pathInterval.path,
JSON.stringify({
context: 'vessels.' + app.selfId,
updates: [
{
timestamp: normalizedPathValue.timestamp,
$source: normalizedPathValue.$source,
values: [
{
path: pathInterval.path,
value: normalizedPathValue.value,
},
],
},
],
})
)
)
);
});
}
return plugin;
};