chenyc
2026-04-21 8632fbd73fdb15f22fae9cd36b9ed3e0635360f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const assert = require('assert');
const { JhmDecoder, additiveChecksum } = require('../decoder');
 
function buildJhmFrame(command, payload) {
  const cy = (command + payload[0] + payload[1] + payload[2] + payload[3]) & 0xFF;
  return Buffer.from([0xEE, 0x55, command, ...payload, cy]);
}
 
function buildBloodPressureFrame({ systolic, diastolic, pulse, timeBytes }) {
  const frameWithoutChecksum = Buffer.from([
    0xAA,
    0x55,
    0x0E,
    0xBA,
    (systolic >> 8) & 0xFF,
    systolic & 0xFF,
    diastolic & 0xFF,
    pulse & 0xFF,
    ...timeBytes,
  ]);
 
  return Buffer.concat([
    frameWithoutChecksum,
    Buffer.from([additiveChecksum(frameWithoutChecksum)]),
  ]);
}
 
describe('decoder', () => {
  it('parses blood pressure frame and publishes N/O/P', () => {
    const decoder = new JhmDecoder({ alModelPath: './alModel.json' });
    const frame = buildBloodPressureFrame({
      systolic: 120,
      diastolic: 80,
      pulse: 89,
      timeBytes: [0x1A, 0x04, 0x0F, 0x09, 0x1E],
    });
 
    const results = decoder.push(frame);
 
    assert.strictEqual(results.length, 1);
    assert.strictEqual(results[0].publish, true);
    assert.deepStrictEqual(results[0].metric, {
      N: 120,
      O: 80,
      P: 89,
    });
  });
 
  it('keeps legacy JHM protocol parsing intact', () => {
    const decoder = new JhmDecoder({ alModelPath: './alModel.json' });
    const frame = buildJhmFrame(0x01, [0x00, 0x00, 0x01, 0x72]);
 
    const results = decoder.push(frame);
 
    assert.strictEqual(results.length, 1);
    assert.strictEqual(results[0].publish, true);
    assert.deepStrictEqual(results[0].metric, { F: 37 });
  });
 
  it('can parse mixed protocol frames from one stream', () => {
    const decoder = new JhmDecoder({ alModelPath: './alModel.json' });
    const jhmFrame = buildJhmFrame(0x0E, [0x00, 0x00, 0x00, 0x8C]);
    const bpFrame = buildBloodPressureFrame({
      systolic: 118,
      diastolic: 76,
      pulse: 72,
      timeBytes: [0x1A, 0x04, 0x0F, 0x0A, 0x05],
    });
 
    const results = decoder.push(Buffer.concat([jhmFrame, bpFrame]));
 
    assert.strictEqual(results.length, 2);
    assert.deepStrictEqual(results[0].metric, { Na: 140 });
    assert.deepStrictEqual(results[1].metric, {
      N: 118,
      O: 76,
      P: 72,
    });
  });
 
  it('can disable blood pressure time publishing', () => {
    const decoder = new JhmDecoder({
      alModelPath: './alModel.json',
      publishBloodPressureTime: false,
    });
    const frame = buildBloodPressureFrame({
      systolic: 120,
      diastolic: 80,
      pulse: 89,
      timeBytes: [0x1A, 0x04, 0x0F, 0x09, 0x1E],
    });
 
    const results = decoder.push(frame);
 
    assert.strictEqual(results.length, 1);
    assert.strictEqual(results[0].publish, true);
    assert.deepStrictEqual(results[0].metric, {
      N: 120,
      O: 80,
      P: 89,
    });
  });
});