Skip to content

Commit

Permalink
Merge pull request #16 from angas/master
Browse files Browse the repository at this point in the history
- Added ZDA sentence
  • Loading branch information
101100 authored Apr 6, 2020
2 parents 42cf54f + 2debf2c commit fff279f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ The following sentence types can be parsed by this library:
- `RMC`
- `VHW`
- `VTG`
- `ZDA`

The following sentence types can be encoded by this library:

Expand Down
42 changes: 42 additions & 0 deletions codecs/ZDA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* === ZDA - Time & Date - UTC, day, month, year and local time zone ===
*
* ------------------------------------------------------------------------------
* 1 2 3 4 5 6 7
* | | | | | | |
* $--ZDA,hhmmss.ss,dd,mm,yyyy,zz,zz*hh<CR><LF>
* ------------------------------------------------------------------------------
*
* Field Number:
* 1. UTC time (hours, minutes, seconds, may have fractional subsecond)
* 2. Day, 01 to 31
* 3. Month, 01 to 12
* 4. Year (4 digits)
* 5. Local zone description, 00 to +- 13 hours
* 6. Local zone minutes description, 00 to 59, apply same sign as local hours
* 7. Checksum
*/

import { parseIntSafe, parseTime } from "../helpers";

export const sentenceId: "ZDA" = "ZDA";
export const sentenceName = "UTC, day, month, year, and local time zone";

export interface ZDAPacket {
sentenceId: "ZDA";
sentenceName: string;
talkerId?: string;
datetime: Date;
localZoneHours: number;
localZoneMinutes: number;
}

export function decodeSentence(fields: string[]): ZDAPacket {
return {
sentenceId: sentenceId,
sentenceName: sentenceName,
datetime: parseTime(fields[1], fields.slice(2, 5).join("")),
localZoneHours: parseIntSafe(fields[5]),
localZoneMinutes: parseIntSafe(fields[6])
};
}
10 changes: 5 additions & 5 deletions helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function toHexString(v: number): string {
}


export function padLeft(value: string|number, length: number, paddingCharacter: string): string {
export function padLeft(value: string | number, length: number, paddingCharacter: string): string {
let result = typeof value === "string" ? value : value.toFixed(0);

while (result.length < length) {
Expand Down Expand Up @@ -269,7 +269,7 @@ export function parseIntSafe(i: string): number {
* Parse the given string to a float if possible, returning 0 for an undefined
* value and a string the the given string cannot be parsed.
*/
export function parseNumberOrString(str?: string): number|string {
export function parseNumberOrString(str?: string): number | string {
if (str === undefined) {
return "";
}
Expand Down Expand Up @@ -344,10 +344,10 @@ export function parseLongitude(lon: string, hemi: string): number {
}

/**
* Parses a time in the format "hhmmss" or "hhmmss.ss" and returns a Date
* Parses a UTC time and optionally a date and returns a Date
* object.
* @param {String} time
* @param {String=} date
* @param {String} time Time the format "hhmmss" or "hhmmss.ss"
* @param {String=} date Optional date in format the ddmmyyyy or ddmmyy
* @returns {Date}
*/
export function parseTime(time: string, date?: string): Date {
Expand Down
8 changes: 5 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import { decodeSentence as decodeRDID, RDIDPacket } from "./codecs/RDID";
import { decodeSentence as decodeRMC, RMCPacket } from "./codecs/RMC";
import { decodeSentence as decodeVHW, VHWPacket } from "./codecs/VHW";
import { decodeSentence as decodeVTG, encodePacket as encodeVTG, VTGPacket } from "./codecs/VTG";
import { decodeSentence as decodeZDA, ZDAPacket } from "./codecs/ZDA";

import { validNmeaChecksum } from "./helpers";


export type Packet = APBPacket | BWCPacket | DBTPacket | DTMPacket | GGAPacket | GLLPacket | GNSPacket | GSAPacket | GSTPacket | GSVPacket | HDGPacket | HDMPacket | HDTPacket | MTKPacket | MWVPacket | RDIDPacket | RMCPacket | VHWPacket | VTGPacket;
export { APBPacket, BWCPacket, DBTPacket, DTMPacket, GGAPacket, GLLPacket, GNSPacket, GSAPacket, GSTPacket, GSVPacket, HDGPacket, HDMPacket, HDTPacket, MTKPacket, MWVPacket, RDIDPacket, RMCPacket, VHWPacket, VTGPacket };
export type Packet = APBPacket | BWCPacket | DBTPacket | DTMPacket | GGAPacket | GLLPacket | GNSPacket | GSAPacket | GSTPacket | GSVPacket | HDGPacket | HDMPacket | HDTPacket | MTKPacket | MWVPacket | RDIDPacket | RMCPacket | VHWPacket | VTGPacket | ZDAPacket;
export { APBPacket, BWCPacket, DBTPacket, DTMPacket, GGAPacket, GLLPacket, GNSPacket, GSAPacket, GSTPacket, GSVPacket, HDGPacket, HDMPacket, HDTPacket, MTKPacket, MWVPacket, RDIDPacket, RMCPacket, VHWPacket, VTGPacket, ZDAPacket };


type Decoder = (parts: string[]) => Packet;
Expand All @@ -47,7 +48,8 @@ const decoders: { [sentenceId: string]: Decoder } = {
RDID: decodeRDID,
RMC: decodeRMC,
VHW: decodeVHW,
VTG: decodeVTG
VTG: decodeVTG,
ZDA: decodeZDA
};


Expand Down
14 changes: 14 additions & 0 deletions tests/ZDAtest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "should";

import { parseNmeaSentence } from "../index";

describe("ZDA", (): void => {
it("parser", (): void => {
const packet = parseNmeaSentence("$GPZDA,160012.71,11,03,2004,-1,00*7D");
packet.should.have.property("sentenceId", "ZDA");
packet.should.have.property("sentenceName", "UTC, day, month, year, and local time zone");
packet.should.have.property("datetime", new Date("2004-03-11T16:00:12.710Z"));
packet.should.have.property("localZoneHours", -1);
packet.should.have.property("localZoneMinutes", 0);
});
});

0 comments on commit fff279f

Please sign in to comment.