From 5863913636f542451e07a030aece6741797233b9 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Tue, 21 May 2024 17:57:20 +0200 Subject: [PATCH] feat: get frames from zniffer and clear --- api/app.ts | 6 ++ api/lib/ZnifferManager.ts | 114 +++++++++++++++++++++++++------------- src/views/Zniffer.vue | 49 +++++++++++++--- 3 files changed, 120 insertions(+), 49 deletions(-) diff --git a/api/app.ts b/api/app.ts index 90985ce247f..eb56c53c126 100644 --- a/api/app.ts +++ b/api/app.ts @@ -760,6 +760,12 @@ function setupSocket(server: HttpServer) { case 'stop': res = await zniffer.stop() break + case 'clear': + res = zniffer.clear() + break + case 'getFrames': + res = zniffer.getFrames() + break case 'setFrequency': res = await zniffer.setFrequency(data.frequency) break diff --git a/api/lib/ZnifferManager.ts b/api/lib/ZnifferManager.ts index f5909ccb1d0..15e9b9dceae 100644 --- a/api/lib/ZnifferManager.ts +++ b/api/lib/ZnifferManager.ts @@ -48,6 +48,7 @@ export type SocketFrame = (Frame | CorruptedFrame) & { parsedPayload?: Record corrupted: boolean timestamp: number + raw: string } export interface FrameCCLogEntry { @@ -113,34 +114,14 @@ export default class ZnifferManager extends TypedEventEmitter { - const socketFrame: SocketFrame = { - ...frame, - corrupted: false, - payload: '' as any, - timestamp: Date.now(), - } - - if ('payload' in frame) { - if (frame.payload instanceof CommandClass) { - socketFrame.parsedPayload = this.ccToLogRecord( - frame.payload, - ) - } else { - socketFrame.payload = buffer2hex(frame.payload) - } - } + this.zniffer.on('frame', (frame, rawData) => { + const socketFrame = this.parseFrame(frame, rawData) this.socket.emit(socketEvents.znifferFrame, socketFrame) }) - this.zniffer.on('corrupted frame', (frame) => { - const socketFrame: SocketFrame = { - ...frame, - corrupted: true, - payload: buffer2hex(frame.payload) as any, - timestamp: Date.now(), - } + this.zniffer.on('corrupted frame', (frame, rawData) => { + const socketFrame = this.parseFrame(frame, rawData) this.socket.emit(socketEvents.znifferFrame, socketFrame) }) @@ -165,6 +146,30 @@ export default class ZnifferManager extends TypedEventEmitter { + return this.parseFrame( + frame.parsedFrame, + frame.frameData, + frame.timestamp.getTime(), + ) + }) + } + public async setFrequency(frequency: number) { this.checkReady() @@ -201,21 +218,29 @@ export default class ZnifferManager extends TypedEventEmitter { - const parsed: Record = commandClass.toLogEntry( - this.zniffer as any, - ) - - if (isEncapsulatingCommandClass(commandClass)) { - parsed.encapsulated = [ - this.ccToLogRecord(commandClass.encapsulated), - ] - } else if (isMultiEncapsulatingCommandClass(commandClass)) { - parsed.encapsulated = [ - commandClass.encapsulated.map((cc) => this.ccToLogRecord(cc)), - ] + try { + const parsed: Record = commandClass.toLogEntry( + this.zniffer as any, + ) + + if (isEncapsulatingCommandClass(commandClass)) { + parsed.encapsulated = [ + this.ccToLogRecord(commandClass.encapsulated), + ] + } else if (isMultiEncapsulatingCommandClass(commandClass)) { + parsed.encapsulated = [ + commandClass.encapsulated.map((cc) => + this.ccToLogRecord(cc), + ), + ] + } + return parsed + } catch (error) { + logger.error('Error parsing command class:', error) + return { + error: error.message, + } } - - return parsed } public async close() { @@ -241,7 +266,7 @@ export default class ZnifferManager extends TypedEventEmitter { - data.id = uuid() - const lastFrame = this.frames[this.frames.length - 1] - data.delta = lastFrame ? data.timestamp - lastFrame.timestamp : 0 - this.framesQueue.push(data) - }) + this.socket.on(socketEvents.znifferFrame, this.addFrame) this.onWindowResize = () => { const oneThird = window.innerHeight / 3 this.topPaneHeight = oneThird * 2 } + this.socket.on('connect', this.onConnnect) + + this.getFrames() + window.addEventListener('resize', this.onWindowResize) this.onWindowResize() @@ -397,6 +396,7 @@ export default { if (this.socket) { // unbind events this.socket.off(socketEvents.znifferFrame) + this.socket.off('connect', this.onConnnect) } if (this.timeoutScroll) { @@ -466,6 +466,15 @@ export default { this.frames.push(...this.framesQueue) this.framesQueue = [] }, + onConnnect() { + this.getFrames() + }, + addFrame(data) { + data.id = uuid() + const lastFrame = this.frames[this.frames.length - 1] + data.delta = lastFrame ? data.timestamp - lastFrame.timestamp : 0 + this.framesQueue.push(data) + }, async clearFrequency() { // needed to handle the clear event on select await this.$nextTick() @@ -690,6 +699,21 @@ export default { } }) }, + async getFrames() { + const response = await this.sendAction( + { + apiName: 'getFrames', + }, + { + hideInfo: true, + }, + ) + + if (response.success) { + this.frames = [] + response.result.forEach(this.addFrame) + } + }, async setFrequency() { const response = await this.sendAction( { @@ -721,9 +745,16 @@ export default { this.showSnackbar(`Zniffer stopped`, 'success') } }, - clearFrames() { - this.frames = [] - this.framesFiltered = [] + async clearFrames() { + const response = await this.sendAction({ + apiName: 'clear', + }) + + if (response.success) { + this.showSnackbar(`Zniffer cleared`, 'success') + this.frames = [] + this.framesFiltered = [] + } }, async createCapture() { const response = await this.sendAction({