Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
adding more input ways to add protocols in order to help other tools …
Browse files Browse the repository at this point in the history
…like node-red components
  • Loading branch information
crisconru committed Dec 5, 2023
1 parent 4ff9a94 commit f9328ac
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 10 deletions.
24 changes: 18 additions & 6 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { readdirSync } from 'node:fs'
import Path from 'node:path'
import { END_FLAG, END_FLAG_LENGTH, MAX_CHARACTERS, START_FLAG, START_FLAG_LENGTH } from "./constants";
import { BooleanSchema, NMEALikeSchema, NaturalSchema, StringSchema } from "./schemas";
import { Data, FieldType, NMEAKnownSentence, NMEAParser, NMEAPreParsed, NMEASentence, NMEAUknownSentence, ParserSentences, StoredSentence, StoredSentences } from "./types";
import { BooleanSchema, NMEALikeSchema, NaturalSchema, ProtocolsInputSchema, StringSchema } from "./schemas";
import { Data, FieldType, NMEAKnownSentence, NMEAParser, NMEAPreParsed, NMEASentence, NMEAUknownSentence, ParserSentences, ProtocolsFile, ProtocolsInput, StoredSentence, StoredSentences } from "./types";
import { getStoreSentences, readProtocolsFile } from './protocols';
import { getNMEAUnparsedSentence } from './sentences';

Expand Down Expand Up @@ -35,15 +35,27 @@ export class Parser implements NMEAParser {
const files = readdirSync(folder, { encoding: 'utf-8' })
files.forEach(file => {
const absoluteFile = Path.join(folder, file)
this.addProtocols(absoluteFile)
this.addProtocols({ file: absoluteFile} )
})
}

private readProtocols(input: ProtocolsInput): ProtocolsFile {
if (input.file !== undefined) return readProtocolsFile(input.file)
if (input.content !== undefined) return input.content
if (input.protocols !== undefined) return { protocols: input.protocols }
throw new Error('Invalid protocols to add')
}

getProtocols(): string[] { return this._protocols }

addProtocols(file: string): void {
// Get protocols from file
const { protocols } = readProtocolsFile(file)
addProtocols(input: ProtocolsInput): void {
const parsed = ProtocolsInputSchema.safeParse(input)
if (!parsed.success) {
const error = parsed.error
console.error(`Invalid protocols to add\n\tError = ${error}`)
throw error
}
const { protocols } = this.readProtocols(input)
// Add to known protocols
protocols.forEach(protocol => {
const protocolName = protocol.protocol
Expand Down
87 changes: 83 additions & 4 deletions tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { generateSentence } from '../src/sentences'
import { NMEAKnownSentenceSchema, NMEAUknownSentenceSchema } from '../src/schemas'
import { DELIMITER, END_FLAG, END_FLAG_LENGTH, SEPARATOR, START_FLAG_LENGTH } from '../src/constants'
import { getChecksum, numberChecksumToString } from '../src/checksum'
import { readProtocolsFile } from '../src/protocols'
import { Protocol, ProtocolsFile } from '../src/types'

const NORSUB_FILE = path.join(__dirname, 'norsub.yaml')

Expand All @@ -19,9 +21,10 @@ describe('Parser', () => {
expectedSentences.forEach(sentence => expect(Object.keys(parserSentences).includes(sentence)).toBeTruthy())
})

test('Add protocols', () => {
test('Add protocols with file', () => {
const file = NORSUB_FILE
const parser = new Parser()
parser.addProtocols(NORSUB_FILE)
parser.addProtocols({ file })

const parserProtocols = parser.getProtocols()

Expand Down Expand Up @@ -49,16 +52,92 @@ describe('Parser', () => {
expect(result).toBeTruthy()
})
})

test('Add protocols with content', () => {
const content = readProtocolsFile(NORSUB_FILE)
const parser = new Parser()
parser.addProtocols({ content })

const parserProtocols = parser.getProtocols()

const expectedProtocols = [
'NMEA',
'GYROCOMPAS1', 'Tokimek PTVG', 'RDI ADCP', 'SMCA', 'SMCC',
'NORSUB', 'NORSUB2', 'NORSUB6', 'NORSUB7', 'NORSUB7b', 'NORSUB8', 'NORSUB PRDID',
]
expectedProtocols.forEach(protocol => {
const result = parserProtocols.includes(protocol)
if (!result) { console.log(`Protocol ${protocol} is not included`) }
expect(result).toBeTruthy()
})

const parserSentences = parser.getSentences()
const expectedSentences = [
'AAM', 'GGA',
'HEHDT', 'PHTRO', 'PHINF',
'PNORSUB', 'PNORSUB2', 'PNORSUB6', 'PNORSUB7', 'PNORSUB7b', 'PNORSUB8', 'PRDID',
'PTVG', 'PRDID', 'PSMCA', 'PSMCC',
]
expectedSentences.forEach(sentence => {
const result = Object.keys(parserSentences).includes(sentence)
if (!result) { console.log(`Sentence ${sentence} is not included`) }
expect(result).toBeTruthy()
})
})

test('Add protocols with protocols', () => {
const { protocols } = readProtocolsFile(NORSUB_FILE)
const parser = new Parser()
parser.addProtocols({ protocols })

const parserProtocols = parser.getProtocols()

const expectedProtocols = [
'NMEA',
'GYROCOMPAS1', 'Tokimek PTVG', 'RDI ADCP', 'SMCA', 'SMCC',
'NORSUB', 'NORSUB2', 'NORSUB6', 'NORSUB7', 'NORSUB7b', 'NORSUB8', 'NORSUB PRDID',
]
expectedProtocols.forEach(protocol => {
const result = parserProtocols.includes(protocol)
if (!result) { console.log(`Protocol ${protocol} is not included`) }
expect(result).toBeTruthy()
})

const parserSentences = parser.getSentences()
const expectedSentences = [
'AAM', 'GGA',
'HEHDT', 'PHTRO', 'PHINF',
'PNORSUB', 'PNORSUB2', 'PNORSUB6', 'PNORSUB7', 'PNORSUB7b', 'PNORSUB8', 'PRDID',
'PTVG', 'PRDID', 'PSMCA', 'PSMCC',
]
expectedSentences.forEach(sentence => {
const result = Object.keys(parserSentences).includes(sentence)
if (!result) { console.log(`Sentence ${sentence} is not included`) }
expect(result).toBeTruthy()
})
})

test('Add protocols error', () => {
const parser = new Parser()
expect(() => parser.addProtocols({})).toThrow()
expect(() => parser.addProtocols({ file: '' })).toThrow()
expect(() => parser.addProtocols({ content: {} as ProtocolsFile })).toThrow()
expect(() => parser.addProtocols({ protocols: {} as Protocol[] })).toThrow()
})

test('Parsing NMEA + NorSub sentences', () => {
const parser = new Parser()
parser.addProtocols(NORSUB_FILE)
parser.addProtocols({ file: NORSUB_FILE })
const storedSentences = parser.getSentences()
Object.values(storedSentences).forEach(storedSentence => {
const input = generateSentence(storedSentence)
expect(input).toBeTypeOf('string')
const output = parser.parseData(input)[0]
expect(() => NMEAKnownSentenceSchema.parse(output)).not.toThrow()
const parsed = NMEAKnownSentenceSchema.safeParse(output)
if (!parsed.success) {
console.error(parsed.error)
}
expect(parsed.success).toBeTruthy()
})
})

Expand Down

0 comments on commit f9328ac

Please sign in to comment.