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
104
105
106
107
108
109
110
111
112
113
114
const assert = require('assert');
const { TcpService, formatReceivedTimestamp } = require('../tcp-service');
const { additiveChecksum } = require('../decoder');
 
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('tcp-service', () => {
  it('formats blood pressure receive time as yyyy-mm-dd HH:mm:ss', () => {
    const text = formatReceivedTimestamp(new Date('2026-04-15T10:20:30'));
 
    assert.strictEqual(text, '2026-04-15 10:20:30');
  });
 
  it('uses receive time for blood pressure metric M', () => {
    let receivedMetric = null;
    const service = new TcpService({
      tcpConfig: { maxBufferBytes: 8192 },
      devices: [],
      alModelPath: './alModel.json',
      publishBloodPressureTime: true,
      logger: { info() {}, warn() {}, error() {} },
      onMetric(_device, metric) {
        receivedMetric = metric;
      },
    });
 
    service.getReceivedMetricTime = () => '2026-04-15 10:20:30';
 
    const fakeSocket = {};
    service.sessions.set(fakeSocket, {
      device: { deviceId: 'JH-001', ip: '127.0.0.1' },
      decoder: {
        push() {
          return [{
            ok: true,
            publish: true,
            protocol: 'blood-pressure',
            metric: { N: 120, O: 80, P: 89 },
            rawHex: '0xAA',
          }];
        },
      },
    });
 
    service.handleData(fakeSocket, buildBloodPressureFrame({
      systolic: 120,
      diastolic: 80,
      pulse: 89,
      timeBytes: [0x1A, 0x04, 0x0F, 0x09, 0x1E],
    }));
 
    assert.deepStrictEqual(receivedMetric, {
      N: 120,
      O: 80,
      P: 89,
      M: '2026-04-15 10:20:30',
    });
  });
 
  it('can disable blood pressure receive time publishing', () => {
    let receivedMetric = null;
    const service = new TcpService({
      tcpConfig: { maxBufferBytes: 8192 },
      devices: [],
      alModelPath: './alModel.json',
      publishBloodPressureTime: false,
      logger: { info() {}, warn() {}, error() {} },
      onMetric(_device, metric) {
        receivedMetric = metric;
      },
    });
 
    const fakeSocket = {};
    service.sessions.set(fakeSocket, {
      device: { deviceId: 'JH-001', ip: '127.0.0.1' },
      decoder: {
        push() {
          return [{
            ok: true,
            publish: true,
            protocol: 'blood-pressure',
            metric: { N: 120, O: 80, P: 89 },
            rawHex: '0xAA',
          }];
        },
      },
    });
 
    service.handleData(fakeSocket, Buffer.from([0xAA]));
 
    assert.deepStrictEqual(receivedMetric, {
      N: 120,
      O: 80,
      P: 89,
    });
  });
});