Skip to content

Commit

Permalink
fix(frame): rssi byte must not be allocated during a default frame cr…
Browse files Browse the repository at this point in the history
…eation
  • Loading branch information
Neonox31 authored Jul 29, 2018
1 parent 9108044 commit 8383005
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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`,
Expand All @@ -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]),
Expand All @@ -134,7 +136,7 @@ export class ZGFrame {
this.rssiBytes,
Uint8Array.from([ZGFrame.STOP_BYTE])
],
8 + this.readMsgLength()
length
)
}

Expand Down Expand Up @@ -173,7 +175,7 @@ export class ZGFrame {
}

writeRSSI(rssi: number): ZGFrame {
writeBytes(this.rssiBytes, rssi)
this.rssiBytes = Buffer.from([rssi])
this.writeChecksum()
return this
}
Expand Down
4 changes: 2 additions & 2 deletions test/frame.spec.ts
Original file line number Diff line number Diff line change
@@ -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 ***/
Expand All @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/zigate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
)
})

Expand Down

0 comments on commit 8383005

Please sign in to comment.