/
index.js
559 lines (520 loc) · 15 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
const http = require('http');
const https = require('https');
const request = require('request');
const SHIPSIO_HOST = 'shipsio.com';
const SHIPSIO_PORT = '443';
const SHIPSIO_HTTP = https;
const SIGNALK_HOST = 'localhost';
const SIGNALK_PORT = '3000';
module.exports = function (app) {
const showError =
app.showerror || app.error ||
(err => {
console.error(err)
})
const logError =
app.error ||
(err => {
console.error(err)
})
const debug =
app.debug ||
(msg => {
console.log(msg)
})
const plugin = {
unsubscribes: []
}
plugin.id = 'shipsio-signalk-plugin';
plugin.name = 'ShipsIO AIS Network Exchanger';
plugin.description = 'Exchange AIS messages with ShipsIO open AIS Network. Enabling this will automatically return close by ships and integrate them into your AIS stream.';
plugin.schema = () => ({
title: 'Exchange AIS messages with ShipsIO',
type: 'object',
properties: {
interval: {
type: 'number',
title: 'Interval between AIS reports (600 seconds is default and 120 seconds is minimum)',
default: 600 //Every 10 mins
},
key: {
type: 'string',
title: 'AIS key. Get it from https://shipsio.com. The key is free, subscription is NOT required. Go to Accounts page to copy the key and paste it here.',
default: ''
},
integrate: {
type: 'boolean',
title: 'Get ships around me and integrate them into my AIS SignalK feed',
default: false
}
}
})
plugin.downloadJSON = function (src) {
return new Promise((resolve, reject) => {
try {
const maxFileSize = 1024 * 1024 * 1; // 1 MB
let contentBuffer = [];
let totalBytesInBuffer = 0;
debug('Fetching local AIS vessels: ' + src)
const req = request(src, {
method: "GET",
headers: {
'Accept': 'application/json'
}
}, function (err, headRes) {
if (err) {
reject(err);
} else if (headRes.statusCode !== 200) {
logError(headRes.statusMessage + ': ' + src);
} else {
// console.log(headRes.statusMessage + ': ' + src);
}
});
req.on('data', function (data) {
contentBuffer.push(data);
totalBytesInBuffer += data.length;
// Look to see if the file size is too large.
if (totalBytesInBuffer > maxFileSize) {
req.pause();
req.header('Connection', 'close');
req.status(413).json({error: `The file size exceeded the limit of ${maxFileSize} bytes`});
req.connection.destroy();
}
});
// Could happen if the client cancels the upload.
req.on('aborted', function () {
// Nothing to do with buffering, garbage collection will clean everything up.
reject(new Error('aborted'));
});
req.on('end', function () {
contentBuffer = Buffer.concat(contentBuffer, totalBytesInBuffer).toString();
if (req.response && (req.response.statusCode === 200)) {
const json = JSON.parse(contentBuffer);
resolve(json);
}
else {
reject(contentBuffer);
}
});
req.on('error', function (error) {
reject(error);
});
} catch (error) {
logError(error);
}
})
}
plugin.vesselDict = {};
plugin.isValidKey = true; //Assume valid until otherwise proven
plugin.fetchVessels = function (url) {
plugin.downloadJSON(url)
.then(async result => {
let vessels = [];
let resultCount = 0;
for (const property in result) {
try {
if (result.hasOwnProperty(property)) {
// debug(property);
resultCount++;
let vesselRaw = result[property];
let vessel = {};
if (vesselRaw.mmsi) {
vessel.MMSI = Number.parseInt(vesselRaw.mmsi);
}
vessel.Name = vesselRaw.name;
if (vesselRaw.navigation && vesselRaw.navigation.position) {
vessel.Lat = vesselRaw.navigation.position.value.latitude;
vessel.Lon = vesselRaw.navigation.position.value.longitude;
if (vesselRaw.navigation.speedOverGround) {
vessel.Speed = vesselRaw.navigation.speedOverGround.value;
}
if (vesselRaw.navigation.courseOverGroundTrue) {
vessel.Course = vesselRaw.navigation.courseOverGroundTrue.value;
}
if (vesselRaw.navigation.headingTrue) {
vessel.Heading = vesselRaw.navigation.headingTrue.value;
}
}
if (vesselRaw.communication && vesselRaw.communication.callsignVhf) {
if (typeof vesselRaw.communication.callsignVhf === 'string' ) {
vessel.Callsign = vesselRaw.communication.callsignVhf;
}
else {
console.warn('Unknown callsignVhf type:' + JSON.stringify(vesselRaw.communication.callsignVhf))
}
}
if (vesselRaw.design) {
if (vesselRaw.design.length) {
vessel.Length = vesselRaw.design.length.value.overall;
}
if (vesselRaw.design.beam) {
vessel.Beam = vesselRaw.design.beam.value;
}
if (vesselRaw.design.draft && vesselRaw.design.draft.value) {
if (vesselRaw.design.draft && vesselRaw.design.draft.value.maximum) {
vessel.Draft = vesselRaw.design.draft.value.maximum;
} else if (vesselRaw.design.draft && vesselRaw.design.draft.value.current) {
vessel.Draft = vesselRaw.design.draft.value.current;
}
}
if (vesselRaw.design.aisShipType) {
vessel.AISType = vesselRaw.design.aisShipType.value.id;
vessel.AISTypeDescription = vesselRaw.design.aisShipType.value.name;
}
}
if (vesselRaw.sensors && vesselRaw.sensors.ais && vesselRaw.sensors.ais.class) {
vessel.AISClass = vesselRaw.sensors.ais.class.value;
}
if (vesselRaw.registrations && vesselRaw.registrations.imo && vesselRaw.registrations.imo.startsWith('IMO ')) {
vessel.IMO = vesselRaw.registrations.imo.replace('IMO ', '');
}
if (vesselRaw.Name) {
vessel.Name = vesselRaw.Name;
}
if (vesselRaw.Lat) {
vessel.Lat = vesselRaw.Lat;
}
if (vesselRaw.Lon) {
vessel.Lon = vesselRaw.Lon;
}
if (vesselRaw.Speed) {
vessel.Speed = vesselRaw.Speed;
}
if (vesselRaw.Course) {
vessel.Course = vesselRaw.Course;
}
if (vesselRaw.Heading) {
vessel.Heading = vesselRaw.Heading;
}
if (vesselRaw.callsign) {
vessel.Callsign = vesselRaw.callsign;
}
if (vesselRaw.Modified) {
vessel.Modified = vesselRaw.Modified;
}
// debug(vessel);
if (vessel.MMSI) {
//Before posting, check against vesselDict
if (plugin.vesselDict[vessel.MMSI]) {
//We've posted this before, let's strip any data that hasn't changed
const previuslyPostedVessel = plugin.vesselDict[vessel.MMSI];
// debug(previuslyPostedVessel);
for (const property in previuslyPostedVessel) {
if (property !== 'MMSI') {
if ((vessel[property] === undefined) || vessel[property] === previuslyPostedVessel[property]) {
//Delete data that is same as previously sent or is undefined, to minimize data transfer
delete vessel[property];
}
}
}
}
for (const property in vessel) {
if (property !== 'MMSI') {
if (vessel[property] === undefined) {
//Delete undefined data
delete vessel[property];
}
}
}
if (!vessel.IsNetAIS) {
vessels.push(vessel);
}
else {
console.debug('Skipped NetAIS vessel: ' + vessel.MMSI);
}
}
}
}
catch (e) {
logError(e);
}
}
debug(resultCount + " local AIS vessels fetched");
if (vessels.length > 0) {
//Post all vessels along with a authorization key (optional) and position (optional)
const data = {
Key: plugin.options.key,
Integrate: plugin.options.integrate,
Lat: vessels[0].Lat, //Assume first vessel is self
Lon: vessels[0].Lon,
Vessels: vessels
}
debug("Posting "+vessels.length+" vessels to ShipsIO");
const result = await plugin.postToShipsIO(data);
if (result) {
debug('Vessels posted to ShipsIO: ' + result.Posted);
//Make sure all posted vessels are in vessel dictionary
for (const index in vessels) {
const postedVessel = vessels[index];
if (!plugin.vesselDict[postedVessel.MMSI]) {
plugin.vesselDict[postedVessel.MMSI] = postedVessel;
}
}
if (result.vessels) {
debug('Vessels returned from ShipsIO: ' + result.vessels.length);
let missingVessels = [];
for (let i=0; i<result.vessels.length; i++) {
let newVessel = result.vessels[i];
if (!plugin.vesselDict[newVessel.MMSI]) {
//New vessel, not posted to ShipsIO
missingVessels.push(newVessel);
}
}
debug("New net AIS vessels: " + missingVessels.length);
for(const index in missingVessels) {
plugin.handleNewVessel(missingVessels[index]);
}
}
else {
debug('Vessels returned from ShipsIO: 0');
}
}
}
})
.catch(e => {
logError(e);
})
}
plugin.prepareValues = function (vessel) {
let values = [];
if (vessel.MMSI) {
values.push({
path: '',
value: {mmsi: vessel.MMSI}
});
}
if (vessel.Name) {
values.push({
path: '',
value: {name: vessel.Name}
});
}
if (vessel.IMO) {
values.push({
path: 'registrations',
value:{imo: vessel.IMO}
});
}
if (vessel.Callsign) {
values.push({
path: 'communication.callsignVhf',
value: vessel.Callsign
});
}
if (true) {
values.push({
path: 'communication.netAIS',
value: true
});
}
if (vessel.Lon && vessel.Lat) {
values.push({
path: 'navigation.position',
value: {longitude: vessel.Lon, latitude: vessel.Lat}
});
}
if (vessel.Course) {
values.push({
path: 'navigation.courseOverGroundTrue',
value: vessel.Course
});
}
if (vessel.Speed) {
values.push({
path: 'navigation.speedOverGround',
value: vessel.Speed
});
}
if (vessel.Heading) {
values.push({
path: 'navigation.headingTrue',
value: vessel.Heading
});
}
if (vessel.Modified) {
values.push({
path: 'navigation.datetime',
value: vessel.Modified
});
}
if (vessel.AISType && vessel.AISTypeDescription) {
values.push({
path: 'design.aisShipType',
value: {id: vessel.AISType,name : vessel.AISTypeDescription} //
});
}
if (vessel.Length) {
values.push({
path: 'design.length',
value:{"overall": vessel.Length}
});
}
if (vessel.Beam) {
values.push({
path: 'design.beam',
value: vessel.Beam
});
}
if (vessel.Draft) {
values.push({
path: 'design.draft',
value:{"current": vessel.Draft,"maximum":vessel.Draft}
});
}
return values;
}
plugin.handleNewVessel = function (vessel) {
if (vessel.MMSI && vessel.Modified) {
debug('Handling NET AIS vessel:' + vessel.Name);
let values = plugin.prepareValues(vessel);
app.handleMessage(plugin.id, {
context: 'vessels.urn:mrn:imo:mmsi:' + vessel.MMSI,
updates: [
{
values: values,
source: {label: plugin.id},
timestamp: vessel.Modified,
}
]
});
}
}
plugin.start = function (options) {
try {
debug('Starting ShipsIO plugin');
if (!options.interval || (options.interval < 120)) {
options.interval = 120;
}
debug('Interval: ' + options.interval);
debug('Key: ' + options.key);
plugin.options = options;
let url = `http://${SIGNALK_HOST}:${SIGNALK_PORT}/signalk/v1/api/vessels`;
debug('Url: ' + url);
if (!options.key || (options.key.length < 1)) {
logError('Missing setting: key');
}
else {
//Setup an interval to perform fetchVessels (does not fire right away)
plugin.intervalId = setInterval(() => {
try {
plugin.fetchVessels(url);
} catch (e) {
logError(e);
}
}, options.interval * 1000);
//Defer first fetchVessels, to let NMEA get some data first
plugin.intervalId = setTimeout(() => {
try {
plugin.fetchVessels(url);
} catch (e) {
logError(e);
}
}, 30000)
}
}
catch (e) {
logError(e);
}
}
plugin.postData = function (url = "", data = {}) {
return new Promise((resolve, reject) => {
try {
const post_data = JSON.stringify(data);
const datalength = Buffer.byteLength(post_data);
const maxFileSize = 1024 * 1024 * 1; // 1 MB
let contentBuffer = '';
let totalBytesInBuffer = 0;
debug('Size to post: ' + datalength);
// An object of options to indicate where to post to
const post_options = {
host: SHIPSIO_HOST,
port: SHIPSIO_PORT,
path: url,
method: 'POST',
headers: {
"Content-Type": "application/json",
'Content-Length': datalength
}
};
// Set up the request
const post_req = SHIPSIO_HTTP.request(post_options, function (res) {
// debug(`STATUS: ${res.statusCode}`);
// debug(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', function (data) {
contentBuffer += data;
totalBytesInBuffer += data.length;
// Look to see if the file size is too large.
if (totalBytesInBuffer > maxFileSize) {
res.pause();
res.header('Connection', 'close');
res.status(413).json({error: `The file size exceeded the limit of ${maxFileSize} bytes`});
res.connection.destroy();
}
});
res.on('end', function () {
try {
debug('Bytes returned: ' + totalBytesInBuffer);
// contentBuffer = Buffer.concat(contentBuffer, totalBytesInBuffer).toString();
if (contentBuffer.startsWith('{"Posted":')) {
debug("Successfully posted to ShipsIO:");
const json = JSON.parse(contentBuffer);
resolve(json);
} else {
if (contentBuffer === "Invalid key") {
//See if this is first time
if (plugin.isValidKey) {
//It's first time, set to false to only send this once
plugin.isValidKey = false;
showError(new Error('Invalid ShipsIO Key'));
}
}
reject(new Error(contentBuffer));
}
} catch (e) {
reject(e);
}
});
res.on('error', function (e) {
logError(e);
reject(e);
})
// Could happen if the client cancels the upload.
res.on('aborted', function () {
// Nothing to do with buffering, garbage collection will clean everything up.
logError(e);
reject(new Error('aborted'));
});
});
post_req.on('error', (e) => {
logError(`Problem with request: ${e.message}`);
reject(e);
});
// post the data
post_req.write(post_data);
post_req.end();
} catch (e) {
logError(e);
reject(e);
}
});
}
plugin.postToShipsIO = async (data) => {
try {
let result = await plugin.postData("/public/ais/signalk", data);
return result;
}
catch (e) {
logError(e);
return null;
}
}
plugin.stop = function () {
debug('Stopping ShipsIO plugin');
if (plugin.intervalId) {
clearInterval(plugin.intervalId);
}
plugin.unsubscribes.forEach(f => f())
}
return plugin;
};