Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add method to return Zniffer capture as Buffer #6836

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/api/zniffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 10 additions & 6 deletions packages/zwave-js/src/lib/zniffer/Zniffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,17 +851,21 @@ export class Zniffer extends TypedEventEmitter<ZnifferEventCallbacks> {
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<void> {
/** 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<void> {
await fs.writeFile(filePath, this.getCaptureAsZLFBuffer());
}

/**
Expand Down