-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
343 lines (292 loc) · 9.77 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const {InfluxDB, Point, HttpError} = require('@influxdata/influxdb-client')
module.exports = function (app) {
let unsubscribes = []
let metricArray = []
let bufferArray = []
let influxUploadTimer
let vesselname = app.getSelfPath('name')
let vesselfleet = app.getSelfPath('fleet')
let modifyPath = function(path,values,signalkTimestamp,options) {
if (path == "navigation.position") {
const pathLatitude = "navigation.position.latitude"
const valueLatitude = values['latitude']
const timestamp = signalkTimestamp
const pathLongitude = "navigation.position.longitude"
const valueLongitude = values['longitude']
const latitude = {path: pathLatitude, value: valueLatitude, timestamp: timestamp}
const longitude = {path: pathLongitude, value: valueLongitude, timestamp: timestamp}
return [latitude,longitude]
//influxFormat(pathLatitude,valueLatitude,timestamp,options)
//influxFormat(pathLongitude,valueLongitude,timestamp,options)
}
if (path == "navigation.attitude") {
const pathRoll = "navigation.attitude.roll"
const valueRoll = values['roll']
const timestamp = signalkTimestamp
const pathPitch = "navigation.attitude.pitch"
const valuePitch = values['pitch']
const pathYaw = "navigation.attitude.yaw"
const valueYaw = values['yaw']
const roll = {path: pathRoll, value: valueRoll, timestamp: timestamp}
const pitch = {path: pathPitch, value: valuePitch, timestamp: timestamp}
const yaw = {path: pathYaw, value: valueYaw, timestamp: timestamp}
return [roll,pitch,yaw]
//influxFormat(pathRoll,valueRoll,timestamp,options)
//influxFormat(pathPitch,valuePitch,timestamp,options)
//influxFormat(pathYaw,valueYaw,timestamp,options)
}
}
let signalkPathCheck = function(path) {
if (path == "navigation.position") {
return true
}
if (path == "navigation.attitude") {
return true
}
}
let isfloatField = function(n) {
return Number(n) === n;
}
let influxFormat = function(path,values,signalkTimestamp,options) {
const measurement = path
const fields = {"value":values}
const timestamp = Date.parse(signalkTimestamp)
const point = new Point(measurement)
.floatField('value',values)
.timestamp(Date.parse(signalkTimestamp))
app.debug(point)
return point
//metricArray.push(metric)
}
let _localSubscription = function(options) {
const subscribeArray = []
options.pathArray.forEach(path => {
const subscribe = {}
subscribe.path = path.path
subscribe.policy = "instant"
subscribe.minPeriod = path.interval
subscribeArray.push(subscribe)
})
app.debug(subscribeArray)
return (localSubscription = {
"context" : "vessels.self",
"subscribe" : subscribeArray
})
}
let _start = function(options) {
app.debug(`${plugin.name} Started...`)
//Set Variables from plugin options
const url = options["influxHost"]
const token = options["influxToken"]
const org = options["influxOrg"]
const bucket = options["influxBucket"]
const writeOptions = options["writeOptions"]
const defaultTags = {}
options.defaultTags.forEach(tag => {
defaultTags[tag["tagName"]]=tag["tagValue"]
app.debug(defaultTags)
})
//Create InfluxDB
const writeApi = new InfluxDB({
url,
token})
.getWriteApi(
org,
bucket,
'ms',
writeOptions)
writeApi.useDefaultTags(defaultTags)
app.subscriptionmanager.subscribe(
_localSubscription(options),
unsubscribes,
subscriptionError => {
app.error('Error:' + subscriptionError);
},
delta => {
delta.updates.forEach(u => {
//if no u.values then return as there is no values to display
if (!u.values) {
return
}
const path = u.values[0].path
const values = u.values[0].value
const timestamp = u.timestamp
if (signalkPathCheck(path) == true) {
const pathArray = modifyPath(path,values,timestamp,options)
pathArray.forEach(seperatePath => {
app.debug(seperatePath)
if (isNaN(seperatePath["value"])) {
return
}
else {
writeApi.writePoint(influxFormat(seperatePath.path,seperatePath.value,seperatePath.timestamp,options))
}
})
}
else {
if (isNaN(values) || !isfloatField(values) || !isFinite(values)) {
app.debug(`Skipping path '${path}' because values is invalid, '${values}'`)
return
}
else {
writeApi.writePoint(influxFormat(path,values,timestamp,options))
}
}
});
}
);
}
let _stop = function(options) {
app.debug(`${plugin.name} Stopped...`)
unsubscribes.forEach(f => f());
unsubscribes = [];
//if (influxUploadTimer) {
//clearInterval(influxUploadTimer);
//}
//// clean up the state
//influxUploadTimer = undefined;
//}
}
const plugin = {
"id":"signalk-to-influxdb-v2-buffer",
"name":"Signalk To Influxdbv2.0",
"description":"Plugin that saves data to an influxdbv2 database - buffers data without internet connection",
"schema":{
"type":"object",
"required":[
"influxHost",
"influxToken",
"influxOrg",
"influxBucket",
"uploadFrequency"
],
"properties":{
"influxHost":{
"type":"string",
"title":"Influxdb2.0 Host URL",
"description": "the url to your cloud hosted influxb2.0"
},
"influxToken":{
"type":"string",
"title":"Influxdb2.0 Token",
"description": "the token for your cloud hosted influxb2.0 bucket"
},
"influxOrg":{
"type":"string",
"title":"Influxdb2.0 Organisation",
"description": "your influxdb2.0 organistion"
},
"influxBucket":{
"type":"string",
"title":"Influxdb2.0 Bucket",
"description": "which bucket you are storing the metrics in"
},
"writeOptions":{
"type":"object",
"title": "Influx Write Options",
"required":[
"batchSize",
"flushInterval",
"maxBufferLines",
"maxRetries",
"maxRetryDelay",
"minRetryDelay",
"retryJitter"
],
"properties":{
"batchSize":{
"type":"number",
"title":"Batch Size",
"description": "the maximum points/line to send in a single batch to InfluxDB server",
"default": 1000
},
"flushInterval":{
"type":"number",
"title":"Flush Interval",
"description": "maximum time in millis to keep points in an unflushed batch, 0 means don't periodically flush",
"default": 30000
},
"maxBufferLines":{
"type":"number",
"title":"Maximum Buffer Lines",
"description": "maximum size of the retry buffer - it contains items that could not be sent for the first time",
"default": 32000
},
"maxRetries":{
"type":"number",
"title":"Maximum Retries",
"description": "maximum delay between retries in milliseconds",
"default": 3
},
"maxRetryDelay":{
"type":"number",
"title":"Maximum Retry Delay",
"description": "maximum delay between retries in milliseconds",
"default": 5000
},
"minRetryDelay":{
"type":"number",
"title":"Minimum Retry Delay",
"description": "minimum delay between retries in milliseconds",
"default": 180000
},
"retryJitter":{
"type":"number",
"title":"Retry Jitter",
"description": "a random value of up to retryJitter is added when scheduling next retry",
"default": 200
}
}
},
"defaultTags":{
"type":"array",
"title": "Default Tags",
"items": {
"type": "object",
"required":[
"tagName",
"tagValue"
],
"properties":{
"tagName":{
"type":"string",
"title":"Tag Name"
},
"tagValue":{
"type":"string",
"title":"Tag Value"
}
}
}
},
"pathArray":{
"type":"array",
"title":"Paths",
"default":[
],
"items":{
"type":"object",
"required":[
"path",
"interval"
],
"properties":{
"path":{
"type":"string",
"title":"Signal K path to record"
},
"interval":{
"type":"number",
"title":"Record Interval",
"default":1000
}
}
}
}
}
},
start: _start,
stop: _stop
}
return plugin
}