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 • 9.69 kB
JavaScript
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.ProtocolManager=void 0;var _asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var _logger=require("../../utils/logger");var _constants=require("../utils/constants");var _helpers=require("../utils/helpers");var PROTOCOL_TEST_COMMAND=_constants.STANDARD_PIDS.SUPPORTED_PIDS_1;var ProtocolManager=exports.ProtocolManager=function(){function ProtocolManager(sendCommand){(0,_classCallCheck2.default)(this,ProtocolManager);this.sendCommand=sendCommand;}return(0,_createClass2.default)(ProtocolManager,[{key:"delay",value:function delay(ms){return new Promise(function(resolve){setTimeout(resolve,ms);});}},{key:"getCurrentProtocolNumber",value:(function(){var _getCurrentProtocolNumber=(0,_asyncToGenerator2.default)(function*(){yield _logger.log.debug('[ProtocolManager] Querying current protocol number (ATDPN)...');try{var response=yield this.sendCommand(_constants.ELM_COMMANDS.GET_PROTOCOL_NUM,2000);var protocolNum=(0,_helpers.extractProtocolNumber)(response);if(protocolNum!==null){yield _logger.log.debug(`[ProtocolManager] Current protocol number: ${protocolNum}`);}else{yield _logger.log.warn(`[ProtocolManager] Could not determine current protocol number from response: ${response!=null?response:'null'}`);}return protocolNum;}catch(error){var errorMsg=error instanceof Error?error.message:String(error);yield _logger.log.error('[ProtocolManager] Error getting current protocol number:',{error:errorMsg});return null;}});function getCurrentProtocolNumber(){return _getCurrentProtocolNumber.apply(this,arguments);}return getCurrentProtocolNumber;}())},{key:"detectAndSetProtocol",value:(function(){var _detectAndSetProtocol=(0,_asyncToGenerator2.default)(function*(){yield _logger.log.debug('[ProtocolManager] Starting protocol detection sequence...');try{yield _logger.log.debug('[ProtocolManager] Trying ATSP0 (Auto)...');var autoSetResponse=yield this.sendCommand(_constants.ELM_COMMANDS.AUTO_PROTOCOL,5000);yield this.delay(_constants.DELAYS_MS.COMMAND_MEDIUM);if(autoSetResponse&&(0,_helpers.isResponseOk)(autoSetResponse)){var verifyResponse=yield this.sendCommand(PROTOCOL_TEST_COMMAND,5000);yield this.delay(_constants.DELAYS_MS.COMMAND_MEDIUM);if(verifyResponse&&!(0,_helpers.isResponseError)(verifyResponse)){var protocolNum=yield this.getCurrentProtocolNumber();if(protocolNum!==null&&protocolNum!==_constants.PROTOCOL.AUTO){var _PROTOCOL_DESCRIPTION;var protocolName=(_PROTOCOL_DESCRIPTION=_constants.PROTOCOL_DESCRIPTIONS[protocolNum])!=null?_PROTOCOL_DESCRIPTION:`Protocol ${protocolNum}`;yield _logger.log.info(`[ProtocolManager] Auto-detection successful. Protocol: ${protocolName} (${protocolNum})`);return{protocol:protocolNum,name:protocolName};}else{yield _logger.log.warn(`[ProtocolManager] ATSP0 succeeded but failed to read back a specific protocol number or still reports AUTO. Response: ${verifyResponse}`);}}else{yield _logger.log.debug(`[ProtocolManager] ATSP0 verification failed or returned error/NO DATA. Response: ${verifyResponse!=null?verifyResponse:'null'}`);}}else{yield _logger.log.debug(`[ProtocolManager] ATSP0 command failed or returned error. Response: ${autoSetResponse!=null?autoSetResponse:'null'}`);}try{yield _logger.log.debug('[ProtocolManager] Closing protocol after failed auto-attempt (ATPC)...');yield this.sendCommand(_constants.ELM_COMMANDS.PROTOCOL_CLOSE,1000);yield this.delay(_constants.DELAYS_MS.PROTOCOL_SWITCH);}catch(_unused){}}catch(error){var errorMsg=error instanceof Error?error.message:String(error);yield _logger.log.error('[ProtocolManager] Error during Auto protocol attempt:',{error:errorMsg});try{yield _logger.log.debug('[ProtocolManager] Closing protocol after error during auto-attempt (ATPC)...');yield this.sendCommand(_constants.ELM_COMMANDS.PROTOCOL_CLOSE,1000);yield this.delay(_constants.DELAYS_MS.PROTOCOL_SWITCH);}catch(_unused2){}}yield _logger.log.debug('[ProtocolManager] Auto-detect failed or inconclusive. Starting manual protocol testing...');for(var protocol of _constants.PROTOCOL_TRY_ORDER){var _PROTOCOL_DESCRIPTION2;if(Number(protocol)===_constants.PROTOCOL.AUTO)continue;var protocolNumHex=protocol.toString(16).toUpperCase();var _protocolName=(_PROTOCOL_DESCRIPTION2=_constants.PROTOCOL_DESCRIPTIONS[protocol])!=null?_PROTOCOL_DESCRIPTION2:`Protocol ${protocolNumHex}`;yield _logger.log.debug(`[ProtocolManager] Trying protocol: ${_protocolName} (${protocolNumHex})...`);try{var tryCmd=`${_constants.ELM_COMMANDS.TRY_PROTOCOL_PREFIX}${protocolNumHex}`;var tryResponse=yield this.sendCommand(tryCmd,10000);yield this.delay(_constants.DELAYS_MS.COMMAND_MEDIUM);if(tryResponse&&((0,_helpers.isResponseOk)(tryResponse)||(0,_helpers.cleanResponse)(tryResponse).includes(_constants.RESPONSE_KEYWORDS.SEARCHING)||tryResponse.trim()==='')){yield _logger.log.debug(`[ProtocolManager] ATTP${protocolNumHex} response suggests potential compatibility: ${tryResponse}`);var testResponse=yield this.sendCommand(PROTOCOL_TEST_COMMAND,5000);yield this.delay(_constants.DELAYS_MS.COMMAND_MEDIUM);if(testResponse&&!(0,_helpers.isResponseError)(testResponse)&&!(0,_helpers.cleanResponse)(testResponse).includes(_constants.RESPONSE_KEYWORDS.NO_DATA)){yield _logger.log.info(`[ProtocolManager] Protocol ${_protocolName} test successful! Setting permanently...`);var setCommand=`${_constants.ELM_COMMANDS.SET_PROTOCOL_PREFIX}${protocolNumHex}`;var setResponse=yield this.sendCommand(setCommand,2000);yield this.delay(_constants.DELAYS_MS.COMMAND_MEDIUM);if(setResponse&&(0,_helpers.isResponseOk)(setResponse)){var finalProtocolNum=yield this.getCurrentProtocolNumber();if(finalProtocolNum===protocol){yield _logger.log.info(`[ProtocolManager] Successfully set and verified protocol: ${_protocolName} (${protocol})`);return{protocol:protocol,name:_protocolName};}else{yield _logger.log.warn(`[ProtocolManager] ATSP${protocolNumHex} reported OK, but ATDPN returned ${finalProtocolNum!=null?finalProtocolNum:'null'}.`);return{protocol:protocol,name:_protocolName};}}else{yield _logger.log.warn(`[ProtocolManager] Failed to set protocol ${_protocolName} with ATSP. Response: ${setResponse!=null?setResponse:'null'}`);}}else{yield _logger.log.debug(`[ProtocolManager] Protocol ${_protocolName} test (${PROTOCOL_TEST_COMMAND}) failed or returned NO DATA. Response: ${testResponse!=null?testResponse:'null'}`);}}else{yield _logger.log.debug(`[ProtocolManager] Protocol ${_protocolName} not supported by adapter or vehicle (ATTP failed). Response: ${tryResponse!=null?tryResponse:'null'}`);}}catch(error){var _errorMsg=error instanceof Error?error.message:String(error);yield _logger.log.error(`[ProtocolManager] Error testing protocol ${_protocolName}:`,{error:_errorMsg});}finally{try{yield _logger.log.debug(`[ProtocolManager] Closing protocol ${_protocolName} attempt (ATPC)...`);yield this.sendCommand(_constants.ELM_COMMANDS.PROTOCOL_CLOSE,1000);yield this.delay(_constants.DELAYS_MS.PROTOCOL_SWITCH);}catch(_unused3){}}}yield _logger.log.error('[ProtocolManager] Protocol detection failed - No working protocol found after all attempts.');return null;});function detectAndSetProtocol(){return _detectAndSetProtocol.apply(this,arguments);}return detectAndSetProtocol;}())},{key:"configureProtocolSettings",value:(function(){var _configureProtocolSettings=(0,_asyncToGenerator2.default)(function*(protocol){if(protocol===null){yield _logger.log.warn('[ProtocolManager] Cannot configure settings: No active protocol.');return;}yield _logger.log.debug(`[ProtocolManager] Configuring settings for protocol: ${protocol}`);try{var adaptTimingCmd=protocol===_constants.PROTOCOL.ISO_14230_4_KWP||protocol===_constants.PROTOCOL.ISO_14230_4_KWP_FAST?_constants.ELM_COMMANDS.ADAPTIVE_TIMING_2:_constants.ELM_COMMANDS.ADAPTIVE_TIMING_1;yield _logger.log.debug(`[ProtocolManager] Setting adaptive timing (${adaptTimingCmd})`);yield this.sendCommand(adaptTimingCmd,1000);yield this.delay(_constants.DELAYS_MS.COMMAND_SHORT);var isCan=protocol>=_constants.PROTOCOL.ISO_15765_4_CAN_11BIT_500K&&protocol<=_constants.PROTOCOL.ISO_15765_4_CAN_29BIT_250K_8;if(isCan){yield _logger.log.debug('[ProtocolManager] Ensuring headers are ON for CAN protocol (ATH1).');yield this.sendCommand(_constants.ELM_COMMANDS.HEADERS_ON,1000);}else{yield _logger.log.debug('[ProtocolManager] Ensuring headers are OFF for non-CAN protocol (ATH0).');yield this.sendCommand(_constants.ELM_COMMANDS.HEADERS_OFF,1000);}yield this.delay(_constants.DELAYS_MS.COMMAND_SHORT);if(isCan){yield _logger.log.debug('[ProtocolManager] Ensuring CAN Auto-Formatting is ON (ATCAF1)');yield this.sendCommand(_constants.ELM_COMMANDS.CAN_AUTO_FORMAT_ON,1000);yield this.delay(_constants.DELAYS_MS.COMMAND_SHORT);}else{yield _logger.log.debug('[ProtocolManager] Ensuring CAN Auto-Formatting is OFF (ATCAF0)');yield this.sendCommand(_constants.ELM_COMMANDS.CAN_AUTO_FORMAT_OFF,1000);yield this.delay(_constants.DELAYS_MS.COMMAND_SHORT);}yield _logger.log.debug(`[ProtocolManager] Basic settings configured for protocol ${protocol}.`);}catch(error){var errorMsg=error instanceof Error?error.message:String(error);yield _logger.log.warn('[ProtocolManager] Error during protocol settings configuration:',{error:errorMsg});}});function configureProtocolSettings(_x){return _configureProtocolSettings.apply(this,arguments);}return configureProtocolSettings;}())}]);}();
//# sourceMappingURL=ProtocolManager.js.map