diff --git a/docs/api/zniffer.md b/docs/api/zniffer.md index b6b9b00803e7..9609a0045551 100644 --- a/docs/api/zniffer.md +++ b/docs/api/zniffer.md @@ -104,6 +104,12 @@ When done, make sure to destroy the Zniffer instance to release the serial port await zniffer.destroy(); ``` +Captured frames can also be returned as a `Buffer` in the `.zlf` format using the `getCaptureAsZLFBuffer` method: + +```ts +await zniffer.getCaptureAsZLFBuffer(); +``` + ## 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 f5deb95c26a0..112ce48e3ece 100644 --- a/packages/zwave-js/src/lib/zniffer/Zniffer.ts +++ b/packages/zwave-js/src/lib/zniffer/Zniffer.ts @@ -851,17 +851,21 @@ export class Zniffer extends TypedEventEmitter { this._capturedFrames = []; } - /** 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 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 header.writeUInt16BE(0x2312, 0x07fe); // checksum + return Buffer.concat([ + header, + ...this._capturedFrames.map(captureToZLFEntry), + ]); + } - await fs.writeFile(filePath, header); - for (const frame of this._capturedFrames) { - 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.getCaptureAsZLFBuffer()); } /**