diff --git a/app2/src/routes/+layout.svelte b/app2/src/routes/+layout.svelte index ed163f2..9106ded 100644 --- a/app2/src/routes/+layout.svelte +++ b/app2/src/routes/+layout.svelte @@ -24,7 +24,7 @@ onMount(async () => { if ($user.bearer_token !== '') { - validateUser($user); + await validateUser($user); } connectToEventSource(); connectToSocket() @@ -68,7 +68,7 @@ } onDestroy(() => { - NotificationSource?.close(); + disconnectEventSource(); socket?.close(); }); @@ -87,137 +87,75 @@ } catch (error) { console.error('Error:', error); } - return; } let menuOpen = false; let throttle = new throttler(); - let NotificationSource: EventSource; let socket: WebSocket - let reconnectIntervalId: number = 0; - let connectionLost = false; - let unresponsiveTimeout: number; + let eventSourceUrl = '/events'; + let eventSource: EventSource; + let unresponsiveTimeoutId: number; function connectToEventSource() { - NotificationSource = new EventSource('/events'); - console.log('Attempting SSE connection.'); + eventSource = new EventSource(eventSourceUrl); - NotificationSource.addEventListener('open', () => { - clearInterval(reconnectIntervalId); - reconnectIntervalId = 0; - connectionLost = false; - console.log('SSE connection established'); + eventSource.addEventListener('open', () => { notifications.success('Connection to device established', 5000); telemetry.setRSSI('found'); // Update store and flag as server being available again }); - NotificationSource.addEventListener( - 'rssi', - (event) => { - telemetry.setRSSI(event.data); - // Reset a timer to detect unresponsiveness - clearTimeout(unresponsiveTimeout); + eventSource.addEventListener('rssi', (event) => { + telemetry.setRSSI(event.data); + resetUnresponsiveCheck(); + }); - unresponsiveTimeout = setTimeout(() => { - console.log('Server is unresponsive'); - reconnectEventSource(); - }, 2000) as unknown as number; // Detect unresponsiveness after 2 seconds - }, - false - ); + eventSource.addEventListener('error', (event) => { + reconnectEventSource(); + }); - NotificationSource.addEventListener( - 'motion', - (event) => { - const data = JSON.parse(event.data); - servoAngles.set(data.angles); - }, - false - ); + eventSource.addEventListener('infoToast', (event) => { + notifications.info(event.data, 5000); + }); - NotificationSource.addEventListener( - 'error', - (event) => { - reconnectEventSource(); - }, - false - ); + eventSource.addEventListener('successToast', (event) => { + notifications.success(event.data, 5000); + }); - NotificationSource.addEventListener( - 'close', - (event) => { - reconnectEventSource(); - }, - false - ); + eventSource.addEventListener('warningToast', (event) => { + notifications.warning(event.data, 5000); + }); - NotificationSource.addEventListener( - 'infoToast', - (event) => { - notifications.info(event.data, 5000); - }, - false - ); + eventSource.addEventListener('errorToast', (event) => { + notifications.error(event.data, 5000); + }); - NotificationSource.addEventListener( - 'successToast', - (event) => { - notifications.success(event.data, 5000); - }, - false - ); + eventSource.addEventListener('battery', (event) => { + telemetry.setBattery(event.data); + }); - NotificationSource.addEventListener( - 'warningToast', - (event) => { - notifications.warning(event.data, 5000); - }, - false - ); + eventSource.addEventListener('download_ota', (event) => { + telemetry.setDownloadOTA(event.data); + }); + eventSource.addEventListener('analytics', (event) => { + analytics.addData(event.data); + }); + } - NotificationSource.addEventListener( - 'errorToast', - (event) => { - notifications.error(event.data, 5000); - }, - false - ); - - NotificationSource.addEventListener( - 'battery', - (event) => { - telemetry.setBattery(event.data); - }, - false - ); - - NotificationSource.addEventListener( - 'download_ota', - (event) => { - telemetry.setDownloadOTA(event.data); - }, - false - ); - NotificationSource.addEventListener( - 'analytics', - (event) => { - analytics.addData(event.data); - }, - false - ); + function disconnectEventSource() { + clearTimeout(unresponsiveTimeoutId); + eventSource?.close(); } function reconnectEventSource() { - if (connectionLost === false) { - NotificationSource.close(); - notifications.error('Connection to device lost', 5000); - if (reconnectIntervalId === 0) { - reconnectIntervalId = setInterval(connectToEventSource, 2000) as unknown as number; - console.log('SSE reconnect Timer ID: ' + reconnectIntervalId); - } - } - connectionLost = true; + notifications.error('Connection to device lost', 5000); + disconnectEventSource(); + connectToEventSource(); + } + + function resetUnresponsiveCheck() { + clearTimeout(unresponsiveTimeoutId); + unresponsiveTimeoutId = setTimeout(() => reconnectEventSource(), 2000); }