obd-raw-data-parser
Version:
A React Native hook library to manage Bluetooth Low Energy connections and communication with ELM327 OBD-II adapters.
2 lines • 2.42 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:true});exports.decodeDTC=decodeDTC;exports.dtcToString=dtcToString;exports.handleFrameSequence=handleFrameSequence;exports.isValidDTCComponents=isValidDTCComponents;exports.isValidDTCFrame=isValidDTCFrame;exports.normalizeResponse=normalizeResponse;var _utils=require("../../utils");function decodeDTC(byte1,byte2){try{var b1=(0,_utils.parseHexInt)(byte1);var b2=(0,_utils.parseHexInt)(byte2);if(isNaN(b1)||isNaN(b2)||b1===0&&b2===0){return null;}var type=b1>>6&0x03;var digit2=b1>>4&0x03;var digit3=b1&0x0f;var digits45=b2;if(isValidDTCComponents(type,digit2,digit3,digits45)){return{type:type,digit2:digit2,digit3:digit3,digits45:digits45};}var swappedType=b2>>6&0x03;var swappedDigit2=b2>>4&0x03;var swappedDigit3=b2&0x0f;var swappedDigits45=b1;if(isValidDTCComponents(swappedType,swappedDigit2,swappedDigit3,swappedDigits45)){return{type:swappedType,digit2:swappedDigit2,digit3:swappedDigit3,digits45:swappedDigits45};}return null;}catch(_unused){return null;}}function dtcToString(dtc){try{if(!dtc||typeof dtc!=='object')return null;var typeIndex=dtc.type,digit2=dtc.digit2,digit3=dtc.digit3,digits45=dtc.digits45;if(!isValidDTCComponents(typeIndex,digit2,digit3,digits45)){return null;}var types=['P','C','B','U'];var typeChar=types[typeIndex];var digit3Hex=(0,_utils.toHexString)(digit3,1).toUpperCase();var digits45Hex=(0,_utils.toHexString)(digits45,2).toUpperCase();return`${typeChar}${digit2}${digit3Hex}${digits45Hex}`;}catch(_unused2){return null;}}function isValidDTCComponents(type,digit2,digit3,digits45){var validations=[{value:type,max:3},{value:digit2,max:3},{value:digit3,max:15},{value:digits45,max:255}];return validations.every(function(_ref){var value=_ref.value,max=_ref.max;return value>=0&&value<=max;});}function normalizeResponse(bytes){return bytes.filter(function(b){return![13,10,62].includes(b);});}function isValidDTCFrame(frame){if(!frame||frame.length<2)return false;var validModeResponses=[0x43,0x47,0x4a];return validModeResponses.includes(frame[0]);}function handleFrameSequence(frames){if(!frames||frames.length===0)return frames;return frames.filter(function(frame){return frame&&frame.length>0;}).map(function(frame){return normalizeResponse(frame);}).filter(function(frame){var isValid=isValidDTCFrame(frame)||frame.some(function(byte){return byte>=0x30&&byte<=0x39;});return isValid;});}
//# sourceMappingURL=dtcDecoder.js.map