Skip to content

Commit

Permalink
feat(wirepas): record local timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Feb 16, 2024
1 parent 984de0d commit c0c7049
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
9 changes: 7 additions & 2 deletions wirepas-5g-mesh-gateway/decodePayload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import assert from 'node:assert/strict'

void describe('decodePayload()', () => {
void it('should decode a regular payload', () => {
const now = Date.now()

const payload = Buffer.from(
[
// [0x01: COUNTER] [0x04] [size_t counter]
Expand Down Expand Up @@ -52,11 +54,14 @@ void describe('decodePayload()', () => {
'hex',
)

const decoded = decodePayload(payload)
const decoded = decodePayload(payload, undefined, () => now)

assert.deepEqual(decoded, {
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
temp: 24.479999542236328,
temp: {
v: 24.479999542236328,
ts: now,
},
})
})

Expand Down
14 changes: 12 additions & 2 deletions wirepas-5g-mesh-gateway/decodePayload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ScannableArray } from './ScannableArray.js'
export type Wirepas5GMeshNodePayload = {
temp?: number
temp?: {
v: number
// The local timestamp
ts: number
}
btn?: {
// The ID of the pressed button
v: number
Expand Down Expand Up @@ -166,7 +170,13 @@ export const decodePayload = (
skip()
continue
case MessageType.TEMPERATURE:
message = { ...message, temp: readFloat(msg, len) }
message = {
...message,
temp: {
v: readFloat(msg, len),
ts: now(),
},
}
continue
default:
onUnknown?.(type, msg.pos() - 1)
Expand Down

0 comments on commit c0c7049

Please sign in to comment.