Skip to content

Commit

Permalink
feat(wirepas): add timestamp to button press
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Feb 13, 2024
1 parent 96ffab2 commit 5a29b01
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 12 additions & 4 deletions wirepas-5g-mesh-gateway/decodePayload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,18 @@ void describe('decodePayload()', () => {
})
})

void it('should decode a button press', () =>
assert.deepEqual(decodePayload(Buffer.from('010002', 'hex')), {
btn: 2,
}))
void it('should decode a button press', () => {
const now = Date.now()
assert.deepEqual(
decodePayload(Buffer.from('010002', 'hex'), undefined, () => now),
{
btn: {
v: 2,
ts: now,
},
},
)
})

void it('should decode a LED state change', () =>
assert.deepEqual(decodePayload(Buffer.from('030101', 'hex')), {
Expand Down
15 changes: 13 additions & 2 deletions wirepas-5g-mesh-gateway/decodePayload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ScannableArray } from './ScannableArray.js'
export type Wirepas5GMeshNodePayload = {
temp?: number
btn?: number
btn?: {
// The ID of the pressed button
v: number
// The local timestamp
ts: number
}
led?: {
r?: boolean
g?: boolean
Expand Down Expand Up @@ -70,6 +75,7 @@ Concerning the message starting with BF, it does not come from the Thingy: these
export const decodePayload = (
payload: Uint8Array,
onUnknown?: (type: number, pos: number) => void,
now = () => Date.now(),
): Wirepas5GMeshNodePayload => {
const msg = new ScannableArray(payload)

Expand All @@ -84,7 +90,12 @@ export const decodePayload = (
if (payload.length === 3 && msg.peek() === 1) {
msg.next() // skip type
msg.next() // skip len
return { btn: readUint(msg, 1) }
return {
btn: {
v: readUint(msg, 1),
ts: now(),
},
}
}

// LED special case
Expand Down

0 comments on commit 5a29b01

Please sign in to comment.