diff --git a/lambda/parseSinkMessages.ts b/lambda/parseSinkMessages.ts index a6d9ba3..c1aa5a3 100644 --- a/lambda/parseSinkMessages.ts +++ b/lambda/parseSinkMessages.ts @@ -1,7 +1,8 @@ import type { KinesisStreamEvent } from 'aws-lambda' import { parser } from '../nrplus/messageStreamParser.js' +import { PCCLines, PDCLines } from '../nrplus/messages.js' -const parserInstance = parser() +const parserInstance = parser([PCCLines, PDCLines]) parserInstance.onMessage((deviceId, message) => { console.log(JSON.stringify({ deviceId, message })) }) diff --git a/nrplus/messageStreamParser.spec.ts b/nrplus/messageStreamParser.spec.ts index 381908b..f41794c 100644 --- a/nrplus/messageStreamParser.spec.ts +++ b/nrplus/messageStreamParser.spec.ts @@ -1,7 +1,8 @@ -import { describe, it, mock } from 'node:test' +import { describe, it } from 'node:test' import assert from 'node:assert/strict' import { readFile } from 'node:fs/promises' import { parser } from './messageStreamParser.js' +import { PCCLines, PDCLines } from './messages.js' void describe('messageStreamParser', () => { void it('should parse sink messages', async () => { @@ -9,15 +10,17 @@ void describe('messageStreamParser', () => { '\n', ) - const onMessageCallback = mock.fn(() => undefined) - const p = parser() - p.onMessage(onMessageCallback) + const messages: Record[]> = {} + const p = parser([PDCLines, PCCLines]) + p.onMessage( + (deviceId, message) => + (messages[deviceId] = [...(messages[deviceId] ?? []), message]), + ) for (const line of lines) { p.addLine('nrplus-gw-jagu', line) } - assert.deepEqual(onMessageCallback.mock.calls[0]?.arguments, [ - 'nrplus-gw-jagu', + assert.deepEqual(messages['nrplus-gw-jagu'], [ { time: '358881336898', snr: '88', @@ -34,10 +37,6 @@ void describe('messageStreamParser', () => { sduData: '{"data":"Yes, hello","modem_temp":"33"}', ieType: 'none', }, - ]) - - assert.deepEqual(onMessageCallback.mock.calls[1]?.arguments, [ - 'nrplus-gw-jagu', { time: '359572555405', status: 'valid - PDC can be received', @@ -49,10 +48,6 @@ void describe('messageStreamParser', () => { mcs: '0', txPowerDBm: '-12', }, - ]) - - assert.deepEqual(onMessageCallback.mock.calls[2]?.arguments, [ - 'nrplus-gw-jagu', { time: '364412319378', snr: '96', @@ -69,10 +64,6 @@ void describe('messageStreamParser', () => { sduData: '{"data":"Yes, hello","modem_temp":"33"}', ieType: 'none', }, - ]) - - assert.deepEqual(onMessageCallback.mock.calls[3]?.arguments, [ - 'nrplus-gw-jagu', { time: '365111832209', status: 'valid - PDC can be received', diff --git a/nrplus/messageStreamParser.ts b/nrplus/messageStreamParser.ts index afcfd9a..62d3834 100644 --- a/nrplus/messageStreamParser.ts +++ b/nrplus/messageStreamParser.ts @@ -1,132 +1,60 @@ -/* -PCC: Physical Control Channel -PDC: Physical Data Channel -MCS: Modulation and Coding Scheme -SDU: Service Data Unit +type MessageListener = ( + deviceId: string, + message: Record, +) => void -PCC comes before each PDC. -PDC contains the actual MAC PDU/SDU. -The content of the PDUs in here are just fMAC specific. -*/ +const parseMessages = ( + lines: string[], + messageDef: Readonly>, +): Record | null => { + const result: RegExpExecArray['groups'][] = [] -type PDCInfo = { - time: string // e.g. '358881336898' - snr: string // e.g. '88' - RSSI: string // e.g. '-60' - len: string // e.g. '83' - type: string // e.g. 'Data SDU' - expectedRXRSSI: string // e.g. '-60' - seqNbr: string // e.g. '366' - networkId: string // e.g. '22' - transmitterId: string // e.g. '40' - receiverId: string // e.g. '38' - sduLastSeenSeqNbr: string // e.g. '1' - sduDataLength: string // e.g. '40' - sduData: string // e.g. '{"data":"Yes, hello","modem_temp":"33"}' - ieType: string // e.g. 'none' -} - -const PDCLines = [ - /^PDC received \(time (?