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, }); }); });