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 6.44 kB
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.VINRetrievalExample=void 0;var _asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _react=_interopRequireWildcard(require("react"));var _reactNative=require("react-native");var _useECU2=require("../ecu/hooks/useECU");var _constants=require("../ecu/utils/constants");var _jsxRuntime=require("react/jsx-runtime");var _this=this,_jsxFileName="/Users/rakshitbharat/hardware/react-native-obd-retriver/src/examples/VINRetrievalExample.tsx";function _getRequireWildcardCache(e){if("function"!=typeof WeakMap)return null;var r=new WeakMap(),t=new WeakMap();return(_getRequireWildcardCache=function _getRequireWildcardCache(e){return e?t:r;})(e);}function _interopRequireWildcard(e,r){if(!r&&e&&e.__esModule)return e;if(null===e||"object"!=typeof e&&"function"!=typeof e)return{default:e};var t=_getRequireWildcardCache(r);if(t&&t.has(e))return t.get(e);var n={__proto__:null},a=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var u in e)if("default"!==u&&{}.hasOwnProperty.call(e,u)){var i=a?Object.getOwnPropertyDescriptor(e,u):null;i&&(i.get||i.set)?Object.defineProperty(n,u,i):n[u]=e[u];}return n.default=e,t&&t.set(e,n),n;}var VINRetrievalExample=exports.VINRetrievalExample=function VINRetrievalExample(){var _useECU=(0,_useECU2.useECU)(),state=_useECU.state,connectWithECU=_useECU.connectWithECU,disconnectECU=_useECU.disconnectECU,getVIN=_useECU.getVIN,getECUInformation=_useECU.getECUInformation;var _useState=(0,_react.useState)(null),_useState2=(0,_slicedToArray2.default)(_useState,2),vinResponse=_useState2[0],setVinResponse=_useState2[1];var _useState3=(0,_react.useState)(false),_useState4=(0,_slicedToArray2.default)(_useState3,2),loading=_useState4[0],setLoading=_useState4[1];var _useState5=(0,_react.useState)(null),_useState6=(0,_slicedToArray2.default)(_useState5,2),lastError=_useState6[0],setLastError=_useState6[1];var fetchVIN=(0,_react.useCallback)((0,_asyncToGenerator2.default)(function*(){setLoading(true);setLastError(null);try{var rawVIN=yield getVIN();setVinResponse(rawVIN);}catch(error){setLastError(error instanceof Error?error.message:String(error));}finally{setLoading(false);}}),[getVIN]);var updateECUInfo=(0,_react.useCallback)((0,_asyncToGenerator2.default)(function*(){setLoading(true);setLastError(null);try{yield getECUInformation();}catch(error){setLastError(error instanceof Error?error.message:String(error));}finally{setLoading(false);}}),[getECUInformation]);var isConnected=state.status===_constants.ECUConnectionStatus.CONNECTED;return(0,_jsxRuntime.jsxs)(_reactNative.ScrollView,{style:styles.container,children:[(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.title,children:"VIN Retrieval Example"}),(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.statusContainer,children:[(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.label,children:"Status:"}),(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.value,children:state.status})]}),isConnected&&(0,_jsxRuntime.jsxs)(_jsxRuntime.Fragment,{children:[(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.infoContainer,children:[(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.infoTitle,children:"ECU Information:"}),(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.infoRow,children:[(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.label,children:"Protocol:"}),(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.value,children:state.protocolName||state.activeProtocol||'Unknown'})]}),(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.infoRow,children:[(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.label,children:"Voltage:"}),(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.value,children:state.deviceVoltage||'Unknown'})]}),(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.infoRow,children:[(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.label,children:"ECU Addresses:"}),(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.value,children:state.detectedEcuAddresses&&state.detectedEcuAddresses.length>0?state.detectedEcuAddresses.join(', '):'None detected'})]})]}),(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.vinContainer,children:[(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.infoTitle,children:"VIN Response:"}),loading?(0,_jsxRuntime.jsx)(_reactNative.Text,{children:"Loading..."}):vinResponse?(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.vinValue,children:vinResponse}):(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.noData,children:"No VIN data retrieved"})]})]}),state.lastError&&(0,_jsxRuntime.jsxs)(_reactNative.Text,{style:styles.errorText,children:["Error: ",state.lastError]}),lastError&&(0,_jsxRuntime.jsxs)(_reactNative.Text,{style:styles.errorText,children:["Operation Error: ",lastError]}),(0,_jsxRuntime.jsx)(_reactNative.View,{style:styles.buttonsContainer,children:(0,_jsxRuntime.jsx)(_reactNative.Button,{title:isConnected?'Disconnect ECU':'Connect ECU',onPress:isConnected?disconnectECU:connectWithECU,disabled:state.status===_constants.ECUConnectionStatus.CONNECTING})}),(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.buttonsContainer,children:[(0,_jsxRuntime.jsx)(_reactNative.Button,{title:"Get VIN",onPress:fetchVIN,disabled:!isConnected||loading}),(0,_jsxRuntime.jsx)(_reactNative.Button,{title:"Update ECU Info",onPress:updateECUInfo,disabled:!isConnected||loading})]})]});};var styles=_reactNative.StyleSheet.create({container:{flex:1,padding:16,backgroundColor:'#fff'},title:{fontSize:20,fontWeight:'bold',marginBottom:16},statusContainer:{flexDirection:'row',marginBottom:8,alignItems:'center'},infoContainer:{backgroundColor:'#f5f5f7',borderRadius:8,padding:12,marginVertical:8},infoTitle:{fontSize:16,fontWeight:'bold',marginBottom:8},infoRow:{flexDirection:'row',marginVertical:4},label:{fontWeight:'bold',marginRight:4,minWidth:100},value:{flex:1},vinContainer:{backgroundColor:'#f5f5f7',borderRadius:8,padding:12,marginVertical:8},vinValue:{fontFamily:'monospace',padding:8,backgroundColor:'#e8e8e8',borderRadius:4},noData:{fontStyle:'italic',color:'#666'},errorText:{color:'red',marginVertical:8},buttonsContainer:{flexDirection:'row',justifyContent:'space-between',marginVertical:12}}); //# sourceMappingURL=VINRetrievalExample.js.map