From 8383005da8036a8d3e911b06940e0d3992ff24b0 Mon Sep 17 00:00:00 2001 From: WEBER Logan Date: Sun, 29 Jul 2018 12:24:01 +0200 Subject: [PATCH] fix(frame): rssi byte must not be allocated during a default frame creation --- src/frame.ts | 10 ++++++---- test/frame.spec.ts | 4 ++-- test/zigate.spec.ts | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/frame.ts b/src/frame.ts index f10d5f0..315851c 100644 --- a/src/frame.ts +++ b/src/frame.ts @@ -80,7 +80,7 @@ export class ZGFrame { msgLengthBytes: Buffer = Buffer.alloc(ZGFrameChunkSize.UInt16) checksumBytes: Buffer = Buffer.alloc(ZGFrameChunkSize.UInt8) msgPayloadBytes: Buffer = Buffer.alloc(0) - rssiBytes: Buffer = Buffer.alloc(ZGFrameChunkSize.UInt8) + rssiBytes: Buffer = Buffer.alloc(0) msgLengthOffset = 0 @@ -111,7 +111,7 @@ export class ZGFrame { this.msgLengthBytes = getFrameChunk(frame, 3, this.msgLengthBytes.length) this.checksumBytes = getFrameChunk(frame, 5, this.checksumBytes.length) this.msgPayloadBytes = getFrameChunk(frame, 6, this.readMsgLength()) - this.rssiBytes = getFrameChunk(frame, 6 + this.readMsgLength(), this.rssiBytes.length) + this.rssiBytes = getFrameChunk(frame, 6 + this.readMsgLength(), ZGFrameChunkSize.UInt8) debug('frame')( `msg code: %d\nmsg length: %d\nchecksum: %d\nmsg payload: %o\nrssi: %d`, @@ -124,6 +124,8 @@ export class ZGFrame { } toBuffer(): Buffer { + const length = 7 + this.rssiBytes.length + this.readMsgLength() + return Buffer.concat( [ Uint8Array.from([ZGFrame.START_BYTE]), @@ -134,7 +136,7 @@ export class ZGFrame { this.rssiBytes, Uint8Array.from([ZGFrame.STOP_BYTE]) ], - 8 + this.readMsgLength() + length ) } @@ -173,7 +175,7 @@ export class ZGFrame { } writeRSSI(rssi: number): ZGFrame { - writeBytes(this.rssiBytes, rssi) + this.rssiBytes = Buffer.from([rssi]) this.writeChecksum() return this } diff --git a/test/frame.spec.ts b/test/frame.spec.ts index 28245d3..c10a733 100644 --- a/test/frame.spec.ts +++ b/test/frame.spec.ts @@ -1,7 +1,7 @@ import { ZGFrame } from '../src/frame' describe('ZGFrame', () => { - it('should create an empty frame when no buffer is specified', () => { + it('should create an empty frame when no specified buffer', () => { /*** GIVEN ***/ /*** WHEN ***/ @@ -12,7 +12,7 @@ describe('ZGFrame', () => { expect(zgFrame.msgLengthBytes).toEqual(Buffer.from([0, 0])) expect(zgFrame.checksumBytes).toEqual(Buffer.from([0])) expect(zgFrame.msgPayloadBytes).toEqual(Buffer.from([])) - expect(zgFrame.rssiBytes).toEqual(Buffer.from([0])) + expect(zgFrame.rssiBytes).toEqual(Buffer.from([])) }) it('should create a frame with a specified buffer', () => { diff --git a/test/zigate.spec.ts b/test/zigate.spec.ts index 86b7b84..d34a115 100644 --- a/test/zigate.spec.ts +++ b/test/zigate.spec.ts @@ -53,7 +53,7 @@ describe('ZiGate', () => { /*** THEN ***/ expect(zigate.serialPort.write).toHaveBeenCalledWith( - Buffer.from([0x1, 0x0, 0x49, 0x0, 0x4, 0x50, 0xff, 0xfc, 0x1e, 0x0, 0x0, 0x3]) + Buffer.from([0x1, 0x0, 0x49, 0x0, 0x4, 0x50, 0xff, 0xfc, 0x1e, 0x0, 0x3]) ) })