Skip to content

Commit 6e536b8

Browse files
committed
Added Gets/Sets
1 parent 13d6f53 commit 6e536b8

File tree

2 files changed

+290
-22
lines changed

2 files changed

+290
-22
lines changed

demo/index.html

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@
8888
document.addEventListener("DOMContentLoaded", function() {
8989

9090
// Initialize nmea
91-
const example = new Nmea();
92-
91+
const example = new Nmea("$GPGGA,092751.000,5321.6802,N,00630.3371,W,1,8,1.03,61.7,M,55.3,M,,*75");
92+
93+
9394
document.getElementById("lms_console").insertAdjacentHTML("beforeend", "<p class='pre_nmea'><b style='color:#ededed'>Time:</b> "+example.getInfo("time", "$GPRMC,001225,A,2832.1834,N,08101.0536,W,12,25,251211,1.2,E,A*03")+"</p>");
9495
document.getElementById("lms_console").insertAdjacentHTML("beforeend", "<p class='pre_nmea'><b style='color:#ededed'>positionStatus:</b> "+example.getInfo("positionStatus", "$GPRMC,001225,A,2832.1834,N,08101.0536,W,12,25,251211,1.2,E,A*03")+"</p>");
9596
document.getElementById("lms_console").insertAdjacentHTML("beforeend", "<p class='pre_nmea'><b style='color:#ededed'>coordinates:</b> "+example.getInfo("coordinates", "$GPRMC,001225,A,2832.1834,N,08101.0536,W,12,25,251211,1.2,E,A*03")+"</p>");
@@ -99,8 +100,42 @@
99100
document.getElementById("lms_console").insertAdjacentHTML("beforeend", "<p class='pre_nmea'><b style='color:#ededed'>magneticVariation:</b> "+example.getInfo("magneticVariation", "$GPRMC,001225,A,2832.1834,N,08101.0536,W,12,25,251211,1.2,E,A*03")+"</p>");
100101
document.getElementById("lms_console").insertAdjacentHTML("beforeend", "<p class='pre_nmea'><b style='color:#ededed'>magneticVariationDirection:</b> "+example.getInfo("magneticVariationDirection", "$GPRMC,001225,A,2832.1834,N,08101.0536,W,12,25,251211,1.2,E,A*03")+"</p>");
101102
document.getElementById("lms_console").insertAdjacentHTML("beforeend", "<p class='pre_nmea'><b style='color:#ededed'>checksum:</b> "+example.getInfo("checksum", "$GPRMC,001225,A,2832.1834,N,08101.0536,W,12,25,251211,1.2,E,A*03")+"</p>");
102-
console.log(example.getInfo("date", "$GPRMC,001225,A,2832.1834,N,08101.0536,W,12,25,251211,1.2,E,A*03"));
103+
103104

105+
console.log(example.altitude);
106+
console.log(example.altitudeUnits);
107+
console.log(example.checksum);
108+
console.log(example.coordinates);
109+
console.log(example.fixType);
110+
console.log(example.hdop);
111+
console.log(example.satellites);
112+
console.log(example.sentenceType);
113+
console.log(example.time);
114+
console.log(example.date);
115+
console.log(example.heading);
116+
console.log(example.magneticVariation);
117+
console.log(example.magneticVariationDirection);
118+
console.log(example.positionStatus);
119+
console.log(example.speed);
120+
121+
console.log("----------------");
122+
example.set = "$GPRMC,001225,A,2832.1834,N,08101.0536,W,12,25,251211,1.2,E,A*03";
123+
124+
console.log(example.altitude);
125+
console.log(example.altitudeUnits);
126+
console.log(example.checksum);
127+
console.log(example.coordinates);
128+
console.log(example.fixType);
129+
console.log(example.hdop);
130+
console.log(example.satellites);
131+
console.log(example.sentenceType);
132+
console.log(example.time);
133+
console.log(example.date);
134+
console.log(example.heading);
135+
console.log(example.magneticVariation);
136+
console.log(example.magneticVariationDirection);
137+
console.log(example.positionStatus);
138+
console.log(example.speed);
104139
});
105140
</script>
106141
</body>

js/nmea.js

Lines changed: 252 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,52 @@ class Nmea {
1111
// Global
1212
let _this = this;
1313
this.option = option;
14+
this.type = null;
1415

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
1529
this.checkSource = function(sentence)
1630
{
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+
}
1835
}
36+
37+
// Check sentence type
38+
this.validateSentence = function(sentence)
39+
{
40+
const splitSentence = sentence.split(',');
41+
if (sentence.startsWith('$') && splitSentence.length >= 2) {
1942

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
2060
this.nmea = {
2161
gpgga: {
2262
sentenceType: function(sentence) { return "GPGGA"; },
@@ -229,34 +269,227 @@ class Nmea {
229269

230270
}
231271

232-
getInfo(property, sentence) {
272+
/*
273+
** Methods
274+
*/
233275

234-
console.log(this.nmea);
276+
getInfo(property, sentence) {
235277

236278
// Check if source if from constructor or parameter
237-
sentence = this.checkSource(sentence);
238-
279+
this.checkSource(sentence);
280+
239281
// Check if sentence is provided
240-
if(sentence)
282+
if(sentence && this.validateSentence(sentence))
241283
{
242284
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;
251288

252-
} else {
253-
return "Invalid NMEA Sentence"; // Invalid NMEA sentence
254-
}
255289
} 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";
257291
}
292+
}
258293

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+
}
260493
}
261494
}
262495

0 commit comments

Comments
 (0)