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 • 7.81 kB
JavaScript
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.DTCManagerExample=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 _DTCRawDataViewer=require("../components/DTCRawDataViewer");var _useDTCRetriever2=require("../ecu/hooks/useDTCRetriever");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/DTCManagerExample.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 DTCManagerExample=exports.DTCManagerExample=function DTCManagerExample(){var _useECU=(0,_useECU2.useECU)(),state=_useECU.state,connectWithECU=_useECU.connectWithECU,disconnectECU=_useECU.disconnectECU;var _useDTCRetriever=(0,_useDTCRetriever2.useDTCRetriever)(),get03DTCObject=_useDTCRetriever.get03DTCObject,get07DTCObject=_useDTCRetriever.get07DTCObject,get0ADTCObject=_useDTCRetriever.get0ADTCObject;var _useState=(0,_react.useState)(null),_useState2=(0,_slicedToArray2.default)(_useState,2),currentDTCs=_useState2[0],setCurrentDTCs=_useState2[1];var _useState3=(0,_react.useState)(null),_useState4=(0,_slicedToArray2.default)(_useState3,2),pendingDTCs=_useState4[0],setPendingDTCs=_useState4[1];var _useState5=(0,_react.useState)(null),_useState6=(0,_slicedToArray2.default)(_useState5,2),permanentDTCs=_useState6[0],setPermanentDTCs=_useState6[1];var _useState7=(0,_react.useState)(null),_useState8=(0,_slicedToArray2.default)(_useState7,2),loading=_useState8[0],setLoading=_useState8[1];var _useState9=(0,_react.useState)(null),_useState10=(0,_slicedToArray2.default)(_useState9,2),lastError=_useState10[0],setLastError=_useState10[1];var fetchCurrentDTCs=(0,_react.useCallback)((0,_asyncToGenerator2.default)(function*(){setLoading('current');setLastError(null);try{var result=yield get03DTCObject();setCurrentDTCs(result);}catch(error){setLastError(`Current DTCs Error: ${error instanceof Error?error.message:String(error)}`);}finally{setLoading(null);}}),[get03DTCObject]);var fetchPendingDTCs=(0,_react.useCallback)((0,_asyncToGenerator2.default)(function*(){setLoading('pending');setLastError(null);try{var result=yield get07DTCObject();setPendingDTCs(result);}catch(error){setLastError(`Pending DTCs Error: ${error instanceof Error?error.message:String(error)}`);}finally{setLoading(null);}}),[get07DTCObject]);var fetchPermanentDTCs=(0,_react.useCallback)((0,_asyncToGenerator2.default)(function*(){setLoading('permanent');setLastError(null);try{var result=yield get0ADTCObject();setPermanentDTCs(result);}catch(error){setLastError(`Permanent DTCs Error: ${error instanceof Error?error.message:String(error)}`);}finally{setLoading(null);}}),[get0ADTCObject]);var fetchAllDTCs=(0,_react.useCallback)((0,_asyncToGenerator2.default)(function*(){setLoading('all');setLastError(null);try{var _yield$Promise$all=yield Promise.all([get03DTCObject(),get07DTCObject(),get0ADTCObject()]),_yield$Promise$all2=(0,_slicedToArray2.default)(_yield$Promise$all,3),current=_yield$Promise$all2[0],pending=_yield$Promise$all2[1],permanent=_yield$Promise$all2[2];setCurrentDTCs(current);setPendingDTCs(pending);setPermanentDTCs(permanent);}catch(error){setLastError(`All DTCs Error: ${error instanceof Error?error.message:String(error)}`);}finally{setLoading(null);}}),[get03DTCObject,get07DTCObject,get0ADTCObject]);var isConnected=state.status===_constants.ECUConnectionStatus.CONNECTED;var isLoading=loading!==null;return(0,_jsxRuntime.jsxs)(_reactNative.ScrollView,{style:styles.container,children:[(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.title,children:"DTC Manager 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})]}),(state.lastError||lastError)&&(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.errorContainer,children:[state.lastError&&(0,_jsxRuntime.jsxs)(_reactNative.Text,{style:styles.errorText,children:["ECU Error: ",state.lastError]}),lastError&&(0,_jsxRuntime.jsx)(_reactNative.Text,{style:styles.errorText,children:lastError})]}),(0,_jsxRuntime.jsx)(_reactNative.View,{style:styles.connectionButtons,children:(0,_jsxRuntime.jsx)(_reactNative.Button,{title:isConnected?'Disconnect ECU':'Connect ECU',onPress:isConnected?disconnectECU:connectWithECU,disabled:state.status===_constants.ECUConnectionStatus.CONNECTING||isLoading})}),(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.buttonsContainer,children:[(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.buttonRow,children:[(0,_jsxRuntime.jsx)(_reactNative.View,{style:styles.buttonItem,children:(0,_jsxRuntime.jsx)(_reactNative.Button,{title:"Get Current DTCs",onPress:fetchCurrentDTCs,disabled:!isConnected||isLoading})}),(0,_jsxRuntime.jsx)(_reactNative.View,{style:styles.buttonItem,children:(0,_jsxRuntime.jsx)(_reactNative.Button,{title:"Get Pending DTCs",onPress:fetchPendingDTCs,disabled:!isConnected||isLoading})})]}),(0,_jsxRuntime.jsxs)(_reactNative.View,{style:styles.buttonRow,children:[(0,_jsxRuntime.jsx)(_reactNative.View,{style:styles.buttonItem,children:(0,_jsxRuntime.jsx)(_reactNative.Button,{title:"Get Permanent DTCs",onPress:fetchPermanentDTCs,disabled:!isConnected||isLoading})}),(0,_jsxRuntime.jsx)(_reactNative.View,{style:styles.buttonItem,children:(0,_jsxRuntime.jsx)(_reactNative.Button,{title:"Get All DTCs",onPress:fetchAllDTCs,disabled:!isConnected||isLoading})})]})]}),loading&&(0,_jsxRuntime.jsxs)(_reactNative.Text,{style:styles.loadingText,children:["Loading ",loading==='all'?'all DTCs':`${loading} DTCs`,"..."]}),(0,_jsxRuntime.jsx)(_DTCRawDataViewer.DTCRawDataViewer,{title:"Current DTCs (Mode 03)",data:currentDTCs,loading:loading==='current'||loading==='all'}),(0,_jsxRuntime.jsx)(_DTCRawDataViewer.DTCRawDataViewer,{title:"Pending DTCs (Mode 07)",data:pendingDTCs,loading:loading==='pending'||loading==='all'}),(0,_jsxRuntime.jsx)(_DTCRawDataViewer.DTCRawDataViewer,{title:"Permanent DTCs (Mode 0A)",data:permanentDTCs,loading:loading==='permanent'||loading==='all'})]});};var styles=_reactNative.StyleSheet.create({container:{flex:1,padding:16,backgroundColor:'#fff'},title:{fontSize:20,fontWeight:'bold',marginBottom:16},statusContainer:{flexDirection:'row',marginBottom:8},label:{fontWeight:'bold',marginRight:4},value:{flex:1},errorContainer:{marginVertical:8,padding:8,backgroundColor:'#ffeeee',borderRadius:4},errorText:{color:'red'},loadingText:{marginVertical:8,fontStyle:'italic',color:'blue'},connectionButtons:{marginVertical:12},buttonsContainer:{marginBottom:12},buttonRow:{flexDirection:'row',justifyContent:'space-between',marginBottom:8},buttonItem:{flex:1,marginHorizontal:4}});
//# sourceMappingURL=DTCManagerExample.js.map