From 18f6af6472e3582b23cc69eb851cf70c0a2db904 Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Sun, 12 May 2024 01:13:29 -0400 Subject: [PATCH 1/2] feat: add method to return Zniffer capture as Buffer --- docs/api/zniffer.md | 6 ++++++ packages/zwave-js/src/lib/zniffer/Zniffer.ts | 16 ++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/docs/api/zniffer.md b/docs/api/zniffer.md index 5aaf29c17820..5331b727b141 100644 --- a/docs/api/zniffer.md +++ b/docs/api/zniffer.md @@ -88,6 +88,12 @@ await zniffer.saveCaptureToFile("/path/to/file.zlf"); This will overwrite the file if it already exists. Starting a new capture will discard all previously captured frames. +Captured frames can also be returned as a `Buffer` in the `.zlf` format using the `getCapture` method: + +```ts +await zniffer.getCapture(); +``` + ## Frequency selection The configured frequency of the Zniffer has to match the frequency of the Z-Wave network it is capturing. Zniffers based on 700/800 series firmware support frequencies that match the `ZnifferRegion` enum: diff --git a/packages/zwave-js/src/lib/zniffer/Zniffer.ts b/packages/zwave-js/src/lib/zniffer/Zniffer.ts index a068d07311b3..c87e584ffb60 100644 --- a/packages/zwave-js/src/lib/zniffer/Zniffer.ts +++ b/packages/zwave-js/src/lib/zniffer/Zniffer.ts @@ -788,17 +788,21 @@ export class Zniffer extends TypedEventEmitter { return ret; } - /** Saves the captured frames in a `.zlf` file that can be read by the official Zniffer application. */ - public async saveCaptureToFile(filePath: string): Promise { + /** Get the captured frames in the official Zniffer application format. */ + public getCapture(): Buffer { // Mimics the current Zniffer software, without using features like sessions and comments const header = Buffer.alloc(2048, 0); header[0] = 0x68; // zniffer version header.writeUInt16BE(0x2312, 0x07fe); // checksum + return Buffer.concat([ + header, + ...this.capturedDataFrames.map(captureToZLFEntry), + ]); + } - await fs.writeFile(filePath, header); - for (const frame of this.capturedDataFrames) { - await fs.appendFile(filePath, captureToZLFEntry(frame)); - } + /** Saves the captured frames in a `.zlf` file that can be read by the official Zniffer application. */ + public async saveCaptureToFile(filePath: string): Promise { + await fs.writeFile(filePath, this.getCapture()); } } From d1d060816b716dfe4a616547cbcc78cf8e514ca7 Mon Sep 17 00:00:00 2001 From: Dominic Griesel Date: Wed, 15 May 2024 09:22:47 +0200 Subject: [PATCH 2/2] refactor: rename method --- docs/api/zniffer.md | 4 ++-- packages/zwave-js/src/lib/zniffer/Zniffer.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/api/zniffer.md b/docs/api/zniffer.md index 5331b727b141..d10b000f562d 100644 --- a/docs/api/zniffer.md +++ b/docs/api/zniffer.md @@ -88,10 +88,10 @@ await zniffer.saveCaptureToFile("/path/to/file.zlf"); This will overwrite the file if it already exists. Starting a new capture will discard all previously captured frames. -Captured frames can also be returned as a `Buffer` in the `.zlf` format using the `getCapture` method: +Captured frames can also be returned as a `Buffer` in the `.zlf` format using the `getCaptureAsZLFBuffer` method: ```ts -await zniffer.getCapture(); +await zniffer.getCaptureAsZLFBuffer(); ``` ## Frequency selection diff --git a/packages/zwave-js/src/lib/zniffer/Zniffer.ts b/packages/zwave-js/src/lib/zniffer/Zniffer.ts index c87e584ffb60..3acc6c8ac85e 100644 --- a/packages/zwave-js/src/lib/zniffer/Zniffer.ts +++ b/packages/zwave-js/src/lib/zniffer/Zniffer.ts @@ -789,7 +789,7 @@ export class Zniffer extends TypedEventEmitter { } /** Get the captured frames in the official Zniffer application format. */ - public getCapture(): Buffer { + public getCaptureAsZLFBuffer(): Buffer { // Mimics the current Zniffer software, without using features like sessions and comments const header = Buffer.alloc(2048, 0); header[0] = 0x68; // zniffer version @@ -802,7 +802,7 @@ export class Zniffer extends TypedEventEmitter { /** Saves the captured frames in a `.zlf` file that can be read by the official Zniffer application. */ public async saveCaptureToFile(filePath: string): Promise { - await fs.writeFile(filePath, this.getCapture()); + await fs.writeFile(filePath, this.getCaptureAsZLFBuffer()); } }