react-native-bluetooth-obd-manager
Version:
A React Native hook library to manage Bluetooth Low Energy connections and communication with ELM327 OBD-II adapters.
2 lines • 4.51 kB
JavaScript
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.bluetoothReducer=bluetoothReducer;exports.initialState=void 0;var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var initialState=exports.initialState={isInitializing:true,isBluetoothOn:false,hasPermissions:false,error:null,isScanning:false,discoveredDevices:[],isConnecting:false,isDisconnecting:false,connectedDevice:null,activeDeviceConfig:null,isAwaitingResponse:false,isStreaming:false,lastSuccessfulCommandTimestamp:null};function bluetoothReducer(state,action){switch(action.type){case'SET_INITIALIZING':return Object.assign({},state,{isInitializing:action.payload});case'SET_BLUETOOTH_STATE':{if(!action.payload){return Object.assign({},initialState,{isInitializing:false,isBluetoothOn:false,hasPermissions:state.hasPermissions});}return Object.assign({},state,{isBluetoothOn:action.payload});}case'SET_PERMISSIONS_STATUS':return Object.assign({},state,{hasPermissions:action.payload});case'SET_ERROR':{return Object.assign({},state,{error:action.payload,isConnecting:false,isDisconnecting:false,isScanning:false,isAwaitingResponse:false});}case'RESET_STATE':return Object.assign({},initialState);case'SCAN_START':return Object.assign({},state,{isScanning:true,discoveredDevices:[],error:null});case'SCAN_STOP':{var _state$error,_state$error$message;var isTimeoutError=(_state$error=state.error)==null?void 0:(_state$error$message=_state$error.message)==null?void 0:_state$error$message.includes('Scan timed out');return Object.assign({},state,{isScanning:false,error:isTimeoutError?null:state.error});}case'DEVICE_FOUND':{var existingDevices=state.discoveredDevices.filter(function(device){return device.id!==action.payload.id;});var newDevices=[].concat((0,_toConsumableArray2.default)(existingDevices),[action.payload]).sort(function(a,b){return a.id.localeCompare(b.id);});return Object.assign({},state,{discoveredDevices:newDevices});}case'CLEAR_DISCOVERED_DEVICES':return Object.assign({},state,{discoveredDevices:[]});case'CONNECT_START':return Object.assign({},state,{isConnecting:true,error:null});case'CONNECT_SUCCESS':return Object.assign({},state,{isConnecting:false,connectedDevice:action.payload.device,activeDeviceConfig:action.payload.config,error:null});case'CONNECT_FAILURE':return Object.assign({},state,{isConnecting:false,connectedDevice:null,activeDeviceConfig:null,error:action.payload});case'DEVICE_DISCONNECTED':{return Object.assign({},state,{connectedDevice:null,activeDeviceConfig:null,isConnecting:false,isDisconnecting:false,isAwaitingResponse:false,isStreaming:false,lastSuccessfulCommandTimestamp:null});}case'DISCONNECT_START':return Object.assign({},state,{isDisconnecting:true});case'DISCONNECT_SUCCESS':return Object.assign({},state,{isDisconnecting:false,connectedDevice:null,activeDeviceConfig:null});case'DISCONNECT_FAILURE':return Object.assign({},state,{isDisconnecting:false,error:action.payload});case'SEND_COMMAND_START':return Object.assign({},state,{isAwaitingResponse:true,error:null});case'COMMAND_SUCCESS':return Object.assign({},state,{isAwaitingResponse:false,error:null,lastSuccessfulCommandTimestamp:Date.now()});case'COMMAND_FAILURE':return Object.assign({},state,{isAwaitingResponse:false,error:action.payload});case'COMMAND_TIMEOUT':return Object.assign({},state,{isAwaitingResponse:false,error:new Error('Command timed out waiting for response.')});case'DATA_RECEIVED':return state;case'SET_STREAMING_STATUS':{var _state$error2,_state$error2$message;console.info(`[Reducer] Setting isStreaming=${action.payload}`);return Object.assign({},state,{isStreaming:action.payload,lastSuccessfulCommandTimestamp:action.payload?Date.now():null,error:!action.payload&&(_state$error2=state.error)!=null&&(_state$error2$message=_state$error2.message)!=null&&_state$error2$message.includes('Streaming stopped')?null:state.error});}case'UPDATE_LAST_SUCCESS_TIMESTAMP':return Object.assign({},state,{lastSuccessfulCommandTimestamp:Date.now()});case'STREAMING_INACTIVITY_TIMEOUT':console.warn('[Reducer] Streaming inactivity timeout detected.');return Object.assign({},state,{isStreaming:false,lastSuccessfulCommandTimestamp:null,error:new Error('Streaming stopped due to inactivity.')});default:{var unknownAction=action;console.warn(`[BluetoothReducer] Unhandled action type: ${unknownAction.type}`);return state;}}}
//# sourceMappingURL=BluetoothReducer.js.map