diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 41dc087..0368ec5 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -12,6 +12,9 @@ + diff --git a/app/(tabs)/_layout.tsx b/app/(tabs)/_layout.tsx index cafb2ad..39ea0cd 100644 --- a/app/(tabs)/_layout.tsx +++ b/app/(tabs)/_layout.tsx @@ -1,8 +1,17 @@ -import React from "react"; +import React, { useEffect } from "react"; import { Tabs } from "expo-router"; import { TAB_CONFIG } from "@/constants/config"; +import { useSelector } from "react-redux"; +import { RootState } from "@/store"; +import { initSocket } from "@/services/socket"; export default function TabLayout() { + const { isLoggedIn } = useSelector((state: RootState) => state.auth); + if (!isLoggedIn) return null; + + useEffect(() => { + initSocket(); + }, [isLoggedIn]); return ( state.telemetry); useLayoutEffect(() => { navigation.setOptions({ @@ -51,10 +54,10 @@ export default function HomeScreen() { subMessage="Service Reminder" /> - + - + {}} /> - {/* */} + + + alert(JSON.stringify(e.nativeEvent.coordinate))} + title={"Test Marker"} + description={"This is a description of the marker"} + /> + + + { const lang = await getLanguage(); if (!lang) { - console.log("Redirecting to language init..."); setShouldRedirect(true); } @@ -42,15 +34,11 @@ export default function RootLayout() { }, []); useEffect(() => { - if (error) throw error; - }, [error]); - - useEffect(() => { - if (loaded && appIsReady) { + if (appIsReady) { SplashScreen.hideAsync(); router.replace("/auth/login"); } - }, [loaded, appIsReady]); + }, [appIsReady]); useEffect(() => { if (appIsReady && shouldRedirect) { @@ -59,8 +47,7 @@ export default function RootLayout() { } }, [appIsReady, shouldRedirect]); - if (!loaded || !appIsReady) { - console.log("!loaded || !appIsReady", loaded, appIsReady); + if (!appIsReady) { return null; } @@ -73,7 +60,6 @@ function RootLayoutNav() { - diff --git a/services/socket.ts b/services/socket.ts new file mode 100644 index 0000000..01898e1 --- /dev/null +++ b/services/socket.ts @@ -0,0 +1,54 @@ +import { io, Socket } from "socket.io-client"; +import { store } from "../store"; +import { updateTelemetry } from "../store/telemetrySlice"; + +const SERVER_URL = + "http://dev.vec-tr.ai:8089/?dashboardId=deviceDashboardSocket&assetId=V16000868651064644504"; +const TOKEN = + "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiYWN0aW9uIjoiYXV0aCIsInRva2VuLXZlcnNpb24iOjAsImlhdCI6MTc1MTM0OTUwMSwiZXhwIjoxNzUxNDM1OTAxfQ.0BB0vRcFSMFDC6aILuc__pG3Ycy8EndYOAwLwCHji1M"; + +let socket: Socket | null = null; + +export const initSocket = () => { + socket = io(SERVER_URL, { + transports: ["websocket"], + extraHeaders: { + Authorization: `Bearer ${TOKEN}`, + controllingServer: "http://dev.vec-tr.ai:8082", + }, + reconnection: true, + }); + + socket.on("connect", () => { + console.log("Socket connected:", socket?.id); + }); + + socket.on("connect_error", (error) => { + console.error("Socket connection error:", error); + }); + + socket.on("disconnect", (reason) => { + console.log("Socket disconnecteddddd", reason, "abc"); + }); + + socket.on("dataUpdate", handleSocketData); +}; + +export const disconnectSocket = () => { + if (socket) { + socket.disconnect(); + console.log("Socket disconnected"); + } +}; + +const handleSocketData = (data: any) => { + try { + const SoH = + data.dataSeries.assetData[0].bms[0].bmsSpecific.ivecSpecific.soh; + const SoC = data?.dataSeries?.assetData?.[0]?.bms?.[0]?.batterySoc; + // console.log(SoC, SoH); + store.dispatch(updateTelemetry({ SoH, SoC })); + } catch (err) { + console.error("Error handling socket data:", err); + } +}; diff --git a/store/rootReducer.ts b/store/rootReducer.ts index 2bb4180..3eb108c 100644 --- a/store/rootReducer.ts +++ b/store/rootReducer.ts @@ -1,8 +1,10 @@ import { combineReducers } from "@reduxjs/toolkit"; import authreducer from "./authSlice"; +import telemetryReducer from "./telemetrySlice"; const rootReducer = combineReducers({ auth: authreducer, + telemetry: telemetryReducer, }); export default rootReducer; diff --git a/store/telemetrySlice.ts b/store/telemetrySlice.ts new file mode 100644 index 0000000..77381a2 --- /dev/null +++ b/store/telemetrySlice.ts @@ -0,0 +1,24 @@ +import { createSlice, PayloadAction } from "@reduxjs/toolkit"; + +interface TelemetryState { + SoH: number; + SoC: number; +} + +const initialState: TelemetryState = { + SoH: 0, + SoC: 0, +}; + +export const telemetrySlice = createSlice({ + name: "telemetry", + initialState, + reducers: { + updateTelemetry: (state, action: PayloadAction) => { + return { ...state, ...action.payload }; + }, + }, +}); + +export const { updateTelemetry } = telemetrySlice.actions; +export default telemetrySlice.reducer;