From a9c74cb4acc678197c59157ea4f6189311e93469 Mon Sep 17 00:00:00 2001 From: Anton Franzluebbers Date: Fri, 7 Jul 2023 23:17:54 -0400 Subject: [PATCH] velconnect v3 ---- GO! --- ...deploy_oracle.yml => deploy_oracle_v2.yml} | 2 +- ...loy_oracle_pb.yml => deploy_oracle_v3.yml} | 2 +- example_dashboard/src/lib/js/velconnect.ts | 130 ++++++++++++++- example_dashboard/src/routes/+page.svelte | 157 +++--------------- velconnect/docker-compose.yml | 2 +- 5 files changed, 152 insertions(+), 141 deletions(-) rename .github/workflows/{deploy_oracle.yml => deploy_oracle_v2.yml} (83%) rename .github/workflows/{deploy_oracle_pb.yml => deploy_oracle_v3.yml} (85%) diff --git a/.github/workflows/deploy_oracle.yml b/.github/workflows/deploy_oracle_v2.yml similarity index 83% rename from .github/workflows/deploy_oracle.yml rename to .github/workflows/deploy_oracle_v2.yml index cbd72cb..bfcb029 100644 --- a/.github/workflows/deploy_oracle.yml +++ b/.github/workflows/deploy_oracle_v2.yml @@ -15,6 +15,6 @@ jobs: echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts - name: connect and pull - run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd /home/ubuntu/VEL-Connect-PB/velconnect && git pull && docker compose up -d --build --remove-orphans && exit" + run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd /home/ubuntu/VEL-Connect-v2/velconnect && git pull && docker compose up -d --build && exit" - name: cleanup run: rm -rf ~/.ssh diff --git a/.github/workflows/deploy_oracle_pb.yml b/.github/workflows/deploy_oracle_v3.yml similarity index 85% rename from .github/workflows/deploy_oracle_pb.yml rename to .github/workflows/deploy_oracle_v3.yml index 48f2d72..9a8de9d 100644 --- a/.github/workflows/deploy_oracle_pb.yml +++ b/.github/workflows/deploy_oracle_v3.yml @@ -15,7 +15,7 @@ jobs: echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa ssh-keyscan -H ${{ secrets.SSH_HOST }} > ~/.ssh/known_hosts - name: connect and pull - run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd ${{ secrets.SSH_WORK_DIR }}/../VEL-Connect-PB && git pull && docker compose up -d --build && exit" + run: ssh ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} "cd /home/ubuntu/VEL-Connect-v3/velconnect && git pull && docker compose up -d --build && exit" - name: cleanup run: rm -rf ~/.ssh diff --git a/example_dashboard/src/lib/js/velconnect.ts b/example_dashboard/src/lib/js/velconnect.ts index 6580881..155c2f1 100644 --- a/example_dashboard/src/lib/js/velconnect.ts +++ b/example_dashboard/src/lib/js/velconnect.ts @@ -1,6 +1,7 @@ import PocketBase from 'pocketbase'; import { writable } from 'svelte/store'; import { type Record } from 'pocketbase'; +import { get } from 'svelte/store'; export const pb = new PocketBase('http://127.0.0.1:8090'); @@ -11,8 +12,7 @@ pb.authStore.onChange((auth) => { currentUser.set(pb.authStore.model); }); -let pairedDevicesInit: string[] = []; -export const pairedDevices = writable(pairedDevicesInit); +export const pairedDevices = writable([]); export const currentDevice = writable(''); interface HasData extends Record { @@ -24,3 +24,129 @@ export interface DeviceData extends Record { data: { [key: string]: string }; } export interface RoomData extends HasData {} + +const device = get(currentDevice); +if (device == '' && device.length > 0) { + currentDevice.set(device[0]); +} + +let unsubscribeDeviceData: () => void; +let unsubscribeRoomData: () => void; +let unsubscribeCurrentDevice: () => void; +let unsubscribeCurrentUser: () => void; + +export let deviceData = writable(null); +export let roomData = writable(null); + +export let sending = false; + +export async function startListening() { + if (get(currentDevice) != '') { + deviceData = await pb.collection('Device').getOne(get(currentDevice)); + } + + unsubscribeCurrentDevice = currentDevice.subscribe(async (val) => { + console.log('current device changed'); + unsubscribeDeviceData?.(); + if (val != '') { + const d = (await pb.collection('Device').getOne(get(currentDevice))) as DeviceData; + deviceData.set(d); + if (d != null) getRoomData(d); + unsubscribeDeviceData = await pb.collection('Device').subscribe(val, async (data) => { + const d = data.record as DeviceData; + deviceData.set(d); + getRoomData(d); + }); + } else { + deviceData.set(null); + roomData.set(null); + } + }); + + unsubscribeCurrentUser = currentUser.subscribe((user) => { + pairedDevices.set(user?.devices ?? []); + }); +} + +export function stopListening() { + unsubscribeCurrentDevice?.(); + unsubscribeDeviceData?.(); + unsubscribeRoomData?.(); + unsubscribeCurrentUser?.(); +} + +async function getRoomData(deviceData: DeviceData) { + unsubscribeRoomData?.(); + + // create or just fetch room by name + const r = await fetch( + `${pb.baseUrl}/data_block/${deviceData.current_app}_${deviceData.current_room}`, + { + method: 'POST' + } + ).then((r) => r.json()); + roomData.set(r); + if (r) { + unsubscribeDeviceData = await pb.collection('DataBlock').subscribe(r.id, (data) => { + roomData.set(data.record as RoomData); + unsubscribeRoomData?.(); + }); + } else { + console.error('Failed to get or create room'); + } +} + +let abortController = new AbortController(); +export function delayedSend() { + console.log('fn: delayedSend()'); + + // abort the previous send + abortController.abort(); + const newAbortController = new AbortController(); + abortController = newAbortController; + setTimeout(() => { + if (!newAbortController.signal.aborted) { + send(); + } else { + console.log('aborted'); + } + }, 1000); +} + +export function send() { + console.log('sending...'); + sending = true; + let promises = []; + const r = get(roomData); + const d = get(deviceData); + if (d) { + promises.push(pb.collection('Device').update(d.id, d)); + } + if (r) { + promises.push(pb.collection('DataBlock').update(r.id, r)); + } + Promise.all(promises).then(() => { + sending = false; + }); +} + +export function removeDevice(d: string) { + pairedDevices.set(get(pairedDevices).filter((i) => i != d)); + + if (get(currentDevice) == d) { + console.log('Removed current device'); + + // if there are still devices left + if (get(pairedDevices).length > 0) { + currentDevice.set(get(pairedDevices)[0]); + } else { + currentDevice.set(''); + } + } + + const user = get(currentUser); + if (user) { + user.devices.filter((i: string) => i != d); + pb.collection('Users').update(user.id, user); + } +} diff --git a/example_dashboard/src/routes/+page.svelte b/example_dashboard/src/routes/+page.svelte index 95870b9..76d26c0 100644 --- a/example_dashboard/src/routes/+page.svelte +++ b/example_dashboard/src/routes/+page.svelte @@ -1,141 +1,26 @@ @@ -182,25 +67,25 @@ {/if} -{#if deviceData != null && deviceData.data != null} +{#if $deviceData != null && $deviceData.data != null}

Device Info

Device ID:
- {deviceData.id} + {$deviceData.id}
Pairing Code:
- {deviceData.pairing_code} + {$deviceData.pairing_code}
First Seen
-

{prettyDate(deviceData.created)}

+

{prettyDate($deviceData.created)}

Last Seen
-

{prettyDate(deviceData.updated)}

+

{prettyDate($deviceData.updated)}

@@ -212,7 +97,7 @@ @@ -231,14 +116,14 @@
Current Room
- + Shareable Link
@@ -258,9 +143,9 @@

Raw JSON:

Device Data
-
{JSON.stringify(deviceData, null, 2)}
+
{JSON.stringify($deviceData, null, 2)}
Room Data
-
{JSON.stringify(roomData, null, 2)}
+
{JSON.stringify($roomData, null, 2)}
{/if}