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 • 3.33 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:true});exports.toHexString=exports.toDecString=exports.parseHexInt=exports.isValidHex=exports.hexToBytes=exports.getValueFromResponse=exports.getUnsignedInt=exports.getPayLoad=exports.fixed=exports.decodeValue=exports.createEmptyBuffer=exports.convertVoltageHexToValue=exports.convertPIDHexToValue=exports.calculateChecksum=exports.bytesToHex=exports.byteArrayToString=void 0;var hexToBytes=exports.hexToBytes=function hexToBytes(hex){var bytes=[];for(var i=0;i<hex.length;i+=2){bytes.push(parseInt(hex.substr(i,2),16));}return bytes;};var bytesToHex=exports.bytesToHex=function bytesToHex(bytes){return bytes.map(function(b){return b.toString(16).padStart(2,'0');}).join('');};var byteArrayToString=exports.byteArrayToString=function byteArrayToString(bytes){try{if(!bytes)return'';if(typeof bytes==='string')return bytes;if(typeof bytes==='number')return decodeValue([bytes]);if(!Array.isArray(bytes))return String(bytes);if(bytes.length===0)return'';var _flatten=function flatten(arr){return arr.reduce(function(flat,item){return flat.concat(Array.isArray(item)?_flatten(item):item);},[]);};var flattened=_flatten(bytes);return decodeValue(flattened);}catch(error){console.error('[ECUDecoder] Error in byteArrayToString:',error);return'';}};var getPayLoad=exports.getPayLoad=function getPayLoad(buffer){if(!buffer||buffer.length<2)return'';return bytesToHex(buffer.slice(2));};var toHexString=exports.toHexString=function toHexString(num){var width=arguments.length>1&&arguments[1]!==undefined?arguments[1]:2;return num.toString(16).toUpperCase().padStart(width,'0');};var createEmptyBuffer=exports.createEmptyBuffer=function createEmptyBuffer(size){var paddingChar=arguments.length>1&&arguments[1]!==undefined?arguments[1]:'0';return paddingChar.repeat(size);};var isValidHex=exports.isValidHex=function isValidHex(hex){return /^[0-9A-Fa-f]+$/.test(hex);};var calculateChecksum=exports.calculateChecksum=function calculateChecksum(data){return data.reduce(function(acc,val){return acc^val;},0);};var parseHexInt=exports.parseHexInt=function parseHexInt(hex){return parseInt(hex,16);};var toDecString=exports.toDecString=function toDecString(num){var width=arguments.length>1&&arguments[1]!==undefined?arguments[1]:0;return num.toString().padStart(width,'0');};var decodeValue=exports.decodeValue=function decodeValue(value){if(!value)return'';try{var textDecoder=new TextDecoder('utf-8');return textDecoder.decode(new Uint8Array(Array.isArray(value)?value:[value]));}catch(error){console.error('[ECUDecoder] Error decoding value:',error);return'';}};var convertPIDHexToValue=exports.convertPIDHexToValue=function convertPIDHexToValue(hex){var value=parseInt(hex,16);return isNaN(value)?0:value;};var convertVoltageHexToValue=exports.convertVoltageHexToValue=function convertVoltageHexToValue(hex){var value=parseInt(hex,16)/1000;return fixed(value);};var getValueFromResponse=exports.getValueFromResponse=function getValueFromResponse(command,response){if(!command||!response)return'';return response.replace(command,'').trim();};var getUnsignedInt=exports.getUnsignedInt=function getUnsignedInt(hex){var value=parseInt(hex,16);return isNaN(value)?0:value>>>0;};var fixed=exports.fixed=function fixed(num){return Math.round(num*100)/100;};
//# sourceMappingURL=utils.js.map