@@ -11,12 +11,52 @@ class Nmea {
11
11
// Global
12
12
let _this = this ;
13
13
this . option = option ;
14
+ this . type = null ;
14
15
16
+ // Defines nmea type
17
+ this . nmeaType = function ( sentence )
18
+ {
19
+ const splitSentence = sentence . split ( ',' ) ;
20
+ if ( sentence . startsWith ( '$' ) && splitSentence . length >= 2 ) {
21
+
22
+ const sentenceType = splitSentence [ 0 ] . substring ( 1 ) . toLowerCase ( ) ;
23
+ if ( sentenceType . length >= 5 ) { this . type = sentenceType ; }
24
+
25
+ } else { return false }
26
+ }
27
+
28
+ // Check if it came from the constructor or parameter
15
29
this . checkSource = function ( sentence )
16
30
{
17
- if ( ! this . option ) { return sentence ; } else { return this . option }
31
+ if ( this . nmeaType ( sentence ) )
32
+ {
33
+ if ( ! this . option ) { return sentence ; } else { return this . option }
34
+ }
18
35
}
36
+
37
+ // Check sentence type
38
+ this . validateSentence = function ( sentence )
39
+ {
40
+ const splitSentence = sentence . split ( ',' ) ;
41
+ if ( sentence . startsWith ( '$' ) && splitSentence . length >= 2 ) {
19
42
43
+ const sentenceType = splitSentence [ 0 ] . substring ( 1 ) . toLowerCase ( ) ;
44
+ if ( sentenceType . length >= 5 ) { return true ; }
45
+
46
+ } else { return false }
47
+ }
48
+
49
+ // Check if property exists
50
+ this . checkProperty = function ( property )
51
+ {
52
+ if ( this . nmea [ this . type ] && this . nmea [ this . type ] [ property ] ) {
53
+ return true
54
+ } else {
55
+ return "There is no property for that sentence."
56
+ }
57
+ }
58
+
59
+ // Main object
20
60
this . nmea = {
21
61
gpgga : {
22
62
sentenceType : function ( sentence ) { return "GPGGA" ; } ,
@@ -229,34 +269,227 @@ class Nmea {
229
269
230
270
}
231
271
232
- getInfo ( property , sentence ) {
272
+ /*
273
+ ** Methods
274
+ */
233
275
234
- console . log ( this . nmea ) ;
276
+ getInfo ( property , sentence ) {
235
277
236
278
// Check if source if from constructor or parameter
237
- sentence = this . checkSource ( sentence ) ;
238
-
279
+ this . checkSource ( sentence ) ;
280
+
239
281
// Check if sentence is provided
240
- if ( sentence )
282
+ if ( sentence && this . validateSentence ( sentence ) )
241
283
{
242
284
const splitSentence = sentence . split ( ',' ) ;
243
-
244
- // Check if the sentence starts with "$" and has at least two fields
245
- if ( sentence . startsWith ( '$' ) && splitSentence . length >= 2 ) {
246
-
247
- const sentenceType = splitSentence [ 0 ] . substring ( 1 ) . toLowerCase ( ) ;
248
- const coordinatesProperty = property ;
249
- const result = this . nmea [ sentenceType ] [ coordinatesProperty ] ( splitSentence ) ;
250
- return result ;
285
+ const coordinatesProperty = property ;
286
+ const result = this . nmea [ this . type ] [ coordinatesProperty ] ( splitSentence ) ;
287
+ return result ;
251
288
252
- } else {
253
- return "Invalid NMEA Sentence" ; // Invalid NMEA sentence
254
- }
255
289
} else {
256
- return "NMEA Sentence must be provided in constructor or as parameter" ; // Invalid NMEA sentence
290
+ return "Invalid NMEA Sentence! must be provided in constructor or as parameter" ;
257
291
}
292
+ }
258
293
259
-
294
+ /*
295
+ ** API > Gets
296
+ */
297
+
298
+ get altitude ( ) {
299
+ this . checkSource ( this . option ) ;
300
+ if ( this . checkProperty ( "altitude" ) )
301
+ {
302
+ try {
303
+ const result = this . nmea [ this . type ] [ "altitude" ] ( this . option . split ( ',' ) ) ;
304
+ return result ;
305
+ } catch ( error ) {
306
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
307
+ }
308
+ }
309
+ }
310
+ get altitudeUnits ( ) {
311
+ this . checkSource ( this . option ) ;
312
+ if ( this . checkProperty ( "altitudeUnits" ) )
313
+ {
314
+ try {
315
+ const result = this . nmea [ this . type ] [ "altitudeUnits" ] ( this . option . split ( ',' ) ) ;
316
+ return result ;
317
+ } catch ( error ) {
318
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
319
+ }
320
+ }
321
+ }
322
+ get checksum ( ) {
323
+ this . checkSource ( this . option ) ;
324
+ if ( this . checkProperty ( "checksum" ) )
325
+ {
326
+ try {
327
+ const result = this . nmea [ this . type ] [ "checksum" ] ( this . option . split ( ',' ) ) ;
328
+ return result ;
329
+ } catch ( error ) {
330
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
331
+ }
332
+ }
333
+ }
334
+ get coordinates ( ) {
335
+ this . checkSource ( this . option ) ;
336
+ if ( this . checkProperty ( "coordinates" ) )
337
+ {
338
+ try {
339
+ const result = this . nmea [ this . type ] [ "coordinates" ] ( this . option . split ( ',' ) ) ;
340
+ return result ;
341
+ } catch ( error ) {
342
+ console . erwarnror ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
343
+ }
344
+ }
345
+ }
346
+ get fixType ( ) {
347
+ this . checkSource ( this . option ) ;
348
+ if ( this . checkProperty ( "fixType" ) )
349
+ {
350
+ try {
351
+ const result = this . nmea [ this . type ] [ "fixType" ] ( this . option . split ( ',' ) ) ;
352
+ return result ;
353
+ } catch ( error ) {
354
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
355
+ }
356
+ }
357
+ }
358
+ get hdop ( ) {
359
+ this . checkSource ( this . option ) ;
360
+ if ( this . checkProperty ( "hdop" ) )
361
+ {
362
+ try {
363
+ const result = this . nmea [ this . type ] [ "hdop" ] ( this . option . split ( ',' ) ) ;
364
+ return result ;
365
+ } catch ( error ) {
366
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
367
+ }
368
+ }
369
+ }
370
+ get satellites ( ) {
371
+ this . checkSource ( this . option ) ;
372
+ if ( this . checkProperty ( "satellites" ) )
373
+ {
374
+ try {
375
+ const result = this . nmea [ this . type ] [ "satellites" ] ( this . option . split ( ',' ) ) ;
376
+ return result ;
377
+ } catch ( error ) {
378
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
379
+ }
380
+ }
381
+ }
382
+ get sentenceType ( ) {
383
+ this . checkSource ( this . option ) ;
384
+ if ( this . checkProperty ( "sentenceType" ) )
385
+ {
386
+ try {
387
+ const result = this . nmea [ this . type ] [ "sentenceType" ] ( this . option . split ( ',' ) ) ;
388
+ return result ;
389
+ } catch ( error ) {
390
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
391
+ }
392
+ }
393
+ }
394
+ get time ( ) {
395
+ this . checkSource ( this . option ) ;
396
+ if ( this . checkProperty ( "time" ) )
397
+ {
398
+ try {
399
+ const result = this . nmea [ this . type ] [ "time" ] ( this . option . split ( ',' ) ) ;
400
+ return result ;
401
+ } catch ( error ) {
402
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
403
+ }
404
+ }
405
+ }
406
+ get date ( ) {
407
+ this . checkSource ( this . option ) ;
408
+ if ( this . checkProperty ( "date" ) )
409
+ {
410
+ try {
411
+ const result = this . nmea [ this . type ] [ "date" ] ( this . option . split ( ',' ) ) ;
412
+ return result ;
413
+ } catch ( error ) {
414
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
415
+ }
416
+ }
417
+ }
418
+ get heading ( ) {
419
+ this . checkSource ( this . option ) ;
420
+ if ( this . checkProperty ( "heading" ) )
421
+ {
422
+ try {
423
+ const result = this . nmea [ this . type ] [ "heading" ] ( this . option . split ( ',' ) ) ;
424
+ return result ;
425
+ } catch ( error ) {
426
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
427
+ }
428
+ }
429
+ }
430
+ get magneticVariation ( ) {
431
+ this . checkSource ( this . option ) ;
432
+ if ( this . checkProperty ( "magneticVariation" ) )
433
+ {
434
+ try {
435
+ const result = this . nmea [ this . type ] [ "magneticVariation" ] ( this . option . split ( ',' ) ) ;
436
+ return result ;
437
+ } catch ( error ) {
438
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
439
+ }
440
+ }
441
+ }
442
+ get magneticVariationDirection ( ) {
443
+ this . checkSource ( this . option ) ;
444
+ if ( this . checkProperty ( "magneticVariationDirection" ) )
445
+ {
446
+ try {
447
+ const result = this . nmea [ this . type ] [ "magneticVariationDirection" ] ( this . option . split ( ',' ) ) ;
448
+ return result ;
449
+ } catch ( error ) {
450
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
451
+ }
452
+ }
453
+ }
454
+ get positionStatus ( ) {
455
+ this . checkSource ( this . option ) ;
456
+ if ( this . checkProperty ( "positionStatus" ) )
457
+ {
458
+ try {
459
+ const result = this . nmea [ this . type ] [ "positionStatus" ] ( this . option . split ( ',' ) ) ;
460
+ return result ;
461
+ } catch ( error ) {
462
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
463
+ }
464
+ }
465
+ }
466
+ get speed ( ) {
467
+ this . checkSource ( this . option ) ;
468
+ if ( this . checkProperty ( "speed" ) )
469
+ {
470
+ try {
471
+ const result = this . nmea [ this . type ] [ "speed" ] ( this . option . split ( ',' ) ) ;
472
+ return result ;
473
+ } catch ( error ) {
474
+ console . warn ( "Property 'altitudeUnits' does not exist in '" + this . type + "' sentence" ) ;
475
+ }
476
+ }
477
+ }
478
+
479
+
480
+ /*
481
+ ** API > Sets
482
+ */
483
+ set set ( sentence )
484
+ {
485
+ try {
486
+ if ( sentence && this . validateSentence ( sentence ) )
487
+ {
488
+ this . option = sentence ;
489
+ }
490
+ } catch ( error ) {
491
+ console . error ( "Invalid NMEA Sentence! must be provided in constructor or as parameter" ) ;
492
+ }
260
493
}
261
494
}
262
495
0 commit comments