diff --git a/RocketControlUnitGUI/src/lib/hooks/useInteraction.ts b/RocketControlUnitGUI/src/lib/hooks/useInteraction.ts index 3e9614a..1ac419f 100644 --- a/RocketControlUnitGUI/src/lib/hooks/useInteraction.ts +++ b/RocketControlUnitGUI/src/lib/hooks/useInteraction.ts @@ -166,4 +166,4 @@ export const useInteraction = (pocketbaseHook: PocketbaseHook) => { instantStateChange, resumeConfirmRemoveWeight, }; -}; +}; \ No newline at end of file diff --git a/RocketControlUnitGUI/src/lib/hooks/usePocketbase.ts b/RocketControlUnitGUI/src/lib/hooks/usePocketbase.ts index f4e3453..833741a 100644 --- a/RocketControlUnitGUI/src/lib/hooks/usePocketbase.ts +++ b/RocketControlUnitGUI/src/lib/hooks/usePocketbase.ts @@ -1,20 +1,26 @@ import PocketBase from 'pocketbase'; import type { Timestamps } from '../timestamps'; -import type { Stores } from '../stores'; -import { currentState } from '../stores'; +import { currentState, type Stores } from '../stores'; +import { fetchUsbMessage, decryptMessage } from '../message'; export type PocketbaseHook = ReturnType; export const usePocketbase = (timestamps: Timestamps, stores: Stores) => { - const pocketbase = new PocketBase('http://192.168.0.69:8090'); + const pocketbase = new PocketBase('http://localhost:8090'); + + const usbMessagePromise = fetchUsbMessage(); const authenticate = async () => { - const email = import.meta.env.VITE_EMAIL; - const password = import.meta.env.VITE_PASSWORD; + const usbMessage = await usbMessagePromise; + + if (usbMessage) { + const { email, password } = usbMessage; + + const decryptedEmail = decryptMessage(email); + const decryptedPassword = decryptMessage(password); - if (email && password) { pocketbase.authStore.clear(); - await pocketbase.admins.authWithPassword(email, password); + await pocketbase.admins.authWithPassword(decryptedEmail, decryptedPassword); return true; } @@ -29,10 +35,18 @@ export const usePocketbase = (timestamps: Timestamps, stores: Stores) => { }; const writeStateChange = async (state: string) => { - await pocketbase.collection('CommandMessage').create({ - target: 'NODE_DMB', - command: state - }); + const usbMessage = await usbMessagePromise; + + if (!usbMessage) { + return; + } + + if (decryptMessage(usbMessage.permission) == 'MasterKey') { + await pocketbase.collection('CommandMessage').create({ + target: 'NODE_DMB', + command: state + }); + } }; const writeCommandMessage = async (target: string, command: string) => { diff --git a/RocketControlUnitGUI/src/lib/message.ts b/RocketControlUnitGUI/src/lib/message.ts new file mode 100644 index 0000000..000ad24 --- /dev/null +++ b/RocketControlUnitGUI/src/lib/message.ts @@ -0,0 +1,41 @@ +export function decryptMessage(messageString: string) { + let decryptedText = ''; + let shift = 3; + + if (messageString == 'usb drive not found') { + return messageString; + } + + for (let char of messageString) { + if (/[a-zA-Z]/.test(char)) { + const shiftBase = char === char.toUpperCase() ? 'A'.charCodeAt(0) : 'a'.charCodeAt(0); + const decryptedChar = String.fromCharCode( + ((char.charCodeAt(0) - shiftBase - shift + 26) % 26) + shiftBase + ); + decryptedText += decryptedChar; + } else { + decryptedText += char; + } + } + + return decryptedText; +} + +export interface UsbData { + permission: string; + email: string; + password: string; +} + +export async function fetchUsbMessage(): Promise { + try { + const response = await fetch('http://127.0.0.1:5000/message'); + if (!response.ok) { + throw new Error('Network response was not ok'); + } + const data = await response.json() as UsbData; + return data; + } catch (error) { + console.error('Error:', error); + } +} \ No newline at end of file diff --git a/RocketControlUnitGUI/src/routes/+page.svelte b/RocketControlUnitGUI/src/routes/+page.svelte index 31176b1..4622ca3 100644 --- a/RocketControlUnitGUI/src/routes/+page.svelte +++ b/RocketControlUnitGUI/src/routes/+page.svelte @@ -68,6 +68,7 @@ timer_remaining } = stores; + onMount(() => { let heartbeatInterval: NodeJS.Timeout; @@ -137,6 +138,7 @@ window.removeEventListener('resize', handleResize); }; + }); $: ac1_display = $ac1_open === undefined ? 'N/A' : $ac1_open ? 'ON' : 'OFF'; @@ -604,7 +606,7 @@ - {#if $currentState == "RS_PRELAUNCH"} + {#if $currentState == "RS_PRELAUNCH" }