UNPKG

ucan_json_parser

Version:

Parse JSON data to uCAN compatible byte streams

146 lines (122 loc) 4.06 kB
// onchange '*.js' 'test/*.js' -- npm test const relay_id = 1; const relay_status_id = 2; const line_motor_id = 3; const line_motor_status_id = 4; const stepper_motor_id = 5; const stepper_motor_status_id = 6; const hall_sensor_status = 7; var assert = require('assert'); var ucan_json_parser = require('../ucan_json_parser') let cmd_1 = { "type": "relay", "id": 6, "signals": { "relay_open": true, } } let cmd_2 = { "type": "line_motor", "id": 3, "signals": { "direction": 1, "speed": 123, } } let cmd_3 = { "type": "unkonwn", "id": 0x2, "signals": { "direction": 1, "speed": 123, } } let cmd_4 = { "type": "stepper_motor", "id": 0x15, "signals": { "direction": 1, "step_size": 2, "steps_number": 12 } } let cmd_assign_nad = { "type": "diag_request", "id": 0x3c, "signals": { "nad": 0x7F, "pci": 0x06, "sid": 0xB0, "d0": 0x12, "d1": 0x34, "d2": 0x56, "d3": 0x78, "d4": 0x00, } } describe('ucan_json_parser', function() { describe('#JSONCommandToByteStream', function() { it('should parse relay command', function() { let f = ucan_json_parser.JSONCommandToByteStream(JSON.stringify(cmd_1)); assert.deepEqual(f, {id: 6, data:Buffer.from([0x01])}); }); it('should parse line_motor command', function() { let f = ucan_json_parser.JSONCommandToByteStream(JSON.stringify(cmd_2)); assert.deepEqual(f, {id: 3, data:Buffer.from([0x10,0x7b, 0x00])}); }); it('should not produce frame if unknown data_type', function() { let f = ucan_json_parser.JSONCommandToByteStream(JSON.stringify(cmd_3)); assert.deepEqual(f, undefined); }); it('should parse stepper_motor command', function() { let f = ucan_json_parser.JSONCommandToByteStream(JSON.stringify(cmd_4)); assert.deepEqual(f, {id: 0x15, data:Buffer.from([0x01,0x02, 12])}); }); it('should parse diagnost request', function() { let f = ucan_json_parser.JSONCommandToByteStream(JSON.stringify(cmd_assign_nad)); assert.deepEqual(f, {id: 0x3C, data:Buffer.from([0x7F, 0x06,0xB0,0x12,0x34,0x56,0x78,0x00])}); }); }); let scf1 = {data : Buffer.from([0x10, 0x7b, 00]), id : 3}; let scf2 = {data : Buffer.from([0x01]), id : 6}; let scf3 = {data : Buffer.from([0x01]), id : 0xFF}; let scf4 = {data : Buffer.from([0x10]), id : 2}; let scf5 = {data : Buffer.from([0,0x04,0x7b]), id : 3}; let scf6 = {data : Buffer.from([15,0x04,0x7b]), id : 15}; describe('#ByteStreamToJSONData', function() { it('should parse stream motor status', function() { let f = ucan_json_parser.ByteStreamToJSONData(scf1, line_motor_status_id); assert.deepEqual(f, { id: 3, control_type: 0, direction: 1, speed: 123, position: 0 }); }); it('should parse stream relay status', function() { let f = ucan_json_parser.ByteStreamToJSONData(scf2, relay_status_id); assert.deepEqual(f, { id: 6, relay_status: 1, relay_status_save: 0 }); }); it('should not crash if unkonw id', function() { let f = ucan_json_parser.ByteStreamToJSONData(scf3, 0xFF); assert.deepEqual(f, undefined); }); it('should not crash if frame is to short', function() { let f = ucan_json_parser.ByteStreamToJSONData(scf4, line_motor_id); assert.deepEqual(f, undefined); }); it('should parse stream stepper status', function() { let f = ucan_json_parser.ByteStreamToJSONData(scf5, stepper_motor_status_id); assert.deepEqual(f, { id: 3, direction: 0, step_size: 0x04, steps_number: 0x7b }); }); it('should parse hall sensor status', function() { let f = ucan_json_parser.ByteStreamToJSONData(scf6, hall_sensor_status); assert.deepEqual(f, { id: 15, x: 15, y: 0x04, z: 0x7b }); }); }); });