phenet-test-library
Build and Publish React NPM Package (including react-native)
- create react-native package
npx create-react-native-library@latest phenet-test-library
- install build tool
npm i -D microbundle
- edit following contents in package.json
{ "name": "phenet-test-library", "version": "0.1.2", "description": "encode/decode obd2-pids", "main": "lib/commonjs/index", "module": "lib/module/index", "types": "lib/typescript/src/index.d", "react-native": "src/index", "source": "src/index.tsx", "unpkg": "lib/index.umd.tsx", "scripts": { "build": "microbundle", "dev": "microbundle watch" } }
- install babel
npm i metro-react-native-babel-preset --save-dev
- edit config babel.config.js:
module.exports = { presets: ['module:metro-react-native-babel-preset'], };
- edit tsconfig.json:
"allowJs": true,
- build pkg
npm run build
publish to npm
npm login
npm whoami
npm publish
Cách sử dụng package
- install package
npm i phenet-test-library
Khởi tạo command
- import
import { ServiceManager } from 'phenet-test-library'; import { ServicesName, PIDsName } from 'phenet-test-library';
- khởi tạo object service
var service = new ServiceManager();
- khai báo command template:
let cmd = JSON.parse('{"time": "null", "data": "", "type": ""}');
- append các pids cần request:
service.appendCommandPID(PIDsName.Engine_speed, cmd); service.appendCommandPID(PIDsName.Vehicle_speed, cmd); service.appendCommandPID(PIDsName.Throttle_position, cmd); service.appendCommandPID(PIDsName.Odometer, cmd); service.appendCommandPID(PIDsName.VIN, cmd); service.appendCommandService(ServicesName.MODE_3_REQ_DTCS, cmd); service.appendCommandService(ServicesName.MODE_4_CLEAR_DTCS, cmd); service.appendCommandService(ServicesName.BAT, cmd); console.log(JSON. stringify(cmd, null, 2));
- kết quả print log:
{ "time": "9:4:24:136", "data": "2015:2:1:12:0:0:0:0:0/2015:2:1:13:0:0:0:0:0/2015:2:1:17:0:0:0:0:0/2015:2:1:166:0:0:0:0:0/2015:2:9:2:0:0:0:0:0/2015:2:3:0:0:0:0:0:0/2015:2:4:0:0:0:0:0:0/2015:2:88:0:0:0:0:0:0", "type": "Engine_speed/Vehicle_speed/Throttle_position/Odometer/VIN/MODE_3_REQ_DTCS/MODE_4_CLEAR_DTCS/BAT" }
Decode command nhận được từ ESP
- decode data nhận từ ESP (đã decrypt)
// fake command let recv_cmd = JSON.parse( `{ "time": "13:28:58:730", "data": "2024:3:65:12:18:52:0:0:0\ /2024:16:20:73:2:1:51:70:65;2024:33:68:80:52:70:74:50:66;2024:34:77:49:49:51:57:49:51;\ /2024:16:20:67:2:1:51:70:65;2024:33:68:80:52:70:74:50:66;2024:34:77:49:49:51:57:49:51;/" }` ); // decode command let results = service.decode(recv_cmd); // print result console.log(JSON.stringify(results, null, 2));
- kết quả print log
{ "data": "1165/3FADP4FJ2BM113913/P0133;C0641;C0450;P3446;C01032;C02413;P3131;P3339;P3133", "type": "Engine_speed/VIN/MODE_3_REQ_DTCS" }
code example
import { ServiceManager } from 'phenet-test-library';
import { ServicesName, PIDsName } from 'phenet-test-library';
var service = new ServiceManager();
// ### Khởi tạo command (encode) ###
let cmd = JSON.parse('{"time": "null", "data": "", "type": ""}');
service.appendCommandPID(PIDsName.Engine_speed, cmd);
service.appendCommandPID(PIDsName.Vehicle_speed, cmd);
service.appendCommandPID(PIDsName.Throttle_position, cmd);
service.appendCommandPID(PIDsName.Odometer, cmd);
service.appendCommandPID(PIDsName.VIN, cmd);
service.appendCommandService(ServicesName.MODE_3_REQ_DTCS, cmd);
service.appendCommandService(ServicesName.MODE_4_CLEAR_DTCS, cmd);
service.appendCommandService(ServicesName.BAT, cmd);
console.log(JSON.stringify(cmd, null, 2));
// ### Decode ###
// fake command
let recv_cmd = JSON.parse(
`{
"time": "13:28:58:730",
"data": "2024:3:65:12:18:52:0:0:0\
/2024:16:20:73:2:1:51:70:65;2024:33:68:80:52:70:74:50:66;2024:34:77:49:49:51:57:49:51;\
/2024:16:20:67:2:1:51:70:65;2024:33:68:80:52:70:74:50:66;2024:34:77:49:49:51:57:49:51;/"
}`
);
// decode command
let results = service.decode(recv_cmd);
// print result
console.log(JSON.stringify(results, null, 2));