UNPKG

react-native-obd-retriver

Version:

A React Native hook library to manage Bluetooth Low Energy connections and communication with ELM327 OBD-II adapters.

2 lines 3.94 kB
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.toHexString=exports.stringToBytes=exports.hexToBytes=exports.bytesToString=exports.bytesToHex=void 0;var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _textEncoding=require("text-encoding");var _logger=require("../../utils/logger");var hexToBytes=exports.hexToBytes=function hexToBytes(hex){var cleanedHex=hex.replace(/[^0-9a-fA-F]/g,'');if(cleanedHex.length%2!==0){void _logger.log.warn(`[ecuUtils] hexToBytes received hex string with odd length: ${hex}`);}var bytes=new Uint8Array(Math.floor(cleanedHex.length/2));for(var i=0;i<bytes.length;i++){var start=i*2;var byteHex=cleanedHex.substring(start,start+2);var byteVal=parseInt(byteHex,16);if(isNaN(byteVal)){void _logger.log.error(`[ecuUtils] Invalid hex byte detected: ${byteHex} in ${hex}`);bytes[i]=0;}else{bytes[i]=byteVal;}}return bytes;};var bytesToHex=exports.bytesToHex=function bytesToHex(bytes){if(!bytes)return'';if(!Array.isArray(bytes)&&!(bytes instanceof Uint8Array)){void _logger.log.warn(`[ecuUtils] bytesToHex received non-array input: ${typeof bytes}`);return'';}return Array.from(bytes).map(function(b){var num=Number(b);if(isNaN(num)){void _logger.log.warn(`[ecuUtils] bytesToHex encountered non-numeric value: ${b}`);return'00';}var validByte=Math.max(0,Math.min(255,Math.floor(num)));return validByte.toString(16).padStart(2,'0');}).join('').toUpperCase();};var bytesToString=exports.bytesToString=function bytesToString(bytes){if(!bytes||bytes.length===0){return'';}try{var _flatten=function flatten(arr){return arr.reduce(function(flat,item){return flat.concat(Array.isArray(item)?_flatten(item):[Number(item)]);},[]);};var numericArray=Array.isArray(bytes)?_flatten(bytes):Array.from(bytes).map(Number);var uint8Array=new Uint8Array(numericArray.filter(function(n){return!isNaN(n);}));var decoderUtf8=new _textEncoding.TextDecoder('utf-8',{fatal:false});var decodedString=decoderUtf8.decode(uint8Array);if(decodedString.includes("\uFFFD")){var decoderLatin1=new _textEncoding.TextDecoder('iso-8859-1');decodedString=decoderLatin1.decode(uint8Array);}return decodedString.replace(/\0/g,'').replace(/[^\x20-\x7E]/g,'').trim();}catch(error){var errorMsg=error instanceof Error?error.message:String(error);void _logger.log.error('[ecuUtils] Error decoding bytes to string:',{error:errorMsg});try{var printableBytes=Array.from(bytes).filter(function(b){return typeof b==='number'&&b>=32&&b<127;});return String.fromCharCode.apply(String,(0,_toConsumableArray2.default)(printableBytes));}catch(fallbackError){var fallbackErrorMsg=fallbackError instanceof Error?fallbackError.message:String(fallbackError);void _logger.log.error('[ecuUtils] Final fallback decoding error:',{error:fallbackErrorMsg});return'';}}};var stringToBytes=exports.stringToBytes=function stringToBytes(str){if(!str){return new Uint8Array(0);}try{var encoder=new _textEncoding.TextEncoder();return encoder.encode(str);}catch(error){var errorMsg=error instanceof Error?error.message:String(error);void _logger.log.error('[ecuUtils] Error encoding string to bytes:',{error:errorMsg});try{var bytes=new Uint8Array(str.length);for(var i=0;i<str.length;i++){bytes[i]=str.charCodeAt(i)&0xff;}return bytes;}catch(fallbackError){var fallbackErrorMsg=fallbackError instanceof Error?fallbackError.message:String(fallbackError);void _logger.log.error('[ecuUtils] Final fallback encoding error:',{error:fallbackErrorMsg});return new Uint8Array(0);}}};var toHexString=exports.toHexString=function toHexString(num){var width=arguments.length>1&&arguments[1]!==undefined?arguments[1]:2;if(typeof num!=='number'||isNaN(num)){return''.padStart(width,'0');}var nonNegativeNum=Math.max(0,num);return Math.floor(nonNegativeNum).toString(16).toUpperCase().padStart(width,'0');}; //# sourceMappingURL=ecuUtils.js.map