diff --git a/.gitignore b/.gitignore
index 0a5c0a9..e670e82 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,4 +15,5 @@ db/
velconnect*.db
.idea/
velconnect/data/
-*.db.bak
\ No newline at end of file
+*.db.bak
+
diff --git a/example_dashboard/package-lock.json b/example_dashboard/package-lock.json
index 8dc4b44..3088eaf 100644
--- a/example_dashboard/package-lock.json
+++ b/example_dashboard/package-lock.json
@@ -8,6 +8,7 @@
"name": "example-dashboard",
"version": "0.0.1",
"dependencies": {
+ "@velaboratory/velconnect-svelte": "file:../velconnect-svelte-npm",
"humanize-duration": "^3.28.0",
"luxon": "^3.3.0",
"pocketbase": "^0.15.2"
@@ -31,6 +32,28 @@
"vite": "^4.3.6"
}
},
+ "../velconect-svelte-npm": {
+ "name": "@velaboratory/velconnect-svelte",
+ "version": "1.0.0",
+ "extraneous": true,
+ "license": "MIT",
+ "dependencies": {
+ "pocketbase": "^0.15.3",
+ "svelte": "^4.0.5"
+ }
+ },
+ "../velconnect-svelte-npm": {
+ "name": "@velaboratory/velconnect-svelte",
+ "version": "1.0.0",
+ "license": "MIT",
+ "dependencies": {
+ "pocketbase": "^0.15.3",
+ "svelte": "^4.0.5"
+ },
+ "devDependencies": {
+ "typescript": "^5.1.6"
+ }
+ },
"node_modules/@aashutoshrathi/word-wrap": {
"version": "1.2.6",
"resolved": "https://registry.npmjs.org/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz",
@@ -889,6 +912,10 @@
"url": "https://opencollective.com/typescript-eslint"
}
},
+ "node_modules/@velaboratory/velconnect-svelte": {
+ "resolved": "../velconnect-svelte-npm",
+ "link": true
+ },
"node_modules/acorn": {
"version": "8.10.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.10.0.tgz",
@@ -3712,6 +3739,14 @@
"eslint-visitor-keys": "^3.3.0"
}
},
+ "@velaboratory/velconnect-svelte": {
+ "version": "file:../velconnect-svelte-npm",
+ "requires": {
+ "pocketbase": "^0.15.3",
+ "svelte": "^4.0.5",
+ "typescript": "^5.1.6"
+ }
+ },
"acorn": {
"version": "8.10.0",
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.10.0.tgz",
diff --git a/example_dashboard/package.json b/example_dashboard/package.json
index 213b694..7040f34 100644
--- a/example_dashboard/package.json
+++ b/example_dashboard/package.json
@@ -31,6 +31,7 @@
},
"type": "module",
"dependencies": {
+ "@velaboratory/velconnect-svelte": "file:../velconnect-svelte-npm",
"humanize-duration": "^3.28.0",
"luxon": "^3.3.0",
"pocketbase": "^0.15.2"
diff --git a/example_dashboard/src/lib/components/Login.svelte b/example_dashboard/src/lib/components/Login.svelte
index 446db14..a10c308 100644
--- a/example_dashboard/src/lib/components/Login.svelte
+++ b/example_dashboard/src/lib/components/Login.svelte
@@ -1,44 +1,34 @@
{#if $currentUser}
- Signed in as {$currentUser.email}
+ Signed in as {$currentUser.username}
{:else}
{/if}
+
+
diff --git a/example_dashboard/src/lib/components/Pair.svelte b/example_dashboard/src/lib/components/Pair.svelte
index 73082a7..543e6a6 100644
--- a/example_dashboard/src/lib/components/Pair.svelte
+++ b/example_dashboard/src/lib/components/Pair.svelte
@@ -1,37 +1,17 @@
-
+
{#if errorMessage}
{errorMessage}
{/if}
diff --git a/example_dashboard/src/lib/js/velconnect.ts b/example_dashboard/src/lib/js/velconnect.ts
deleted file mode 100644
index 6931fd9..0000000
--- a/example_dashboard/src/lib/js/velconnect.ts
+++ /dev/null
@@ -1,184 +0,0 @@
-import PocketBase from 'pocketbase';
-import { writable } from 'svelte/store';
-import { type Record } from 'pocketbase';
-import { get } from 'svelte/store';
-
-export const pb = new PocketBase('https://velconnect-v3.ugavel.com');
-// export const pb = new PocketBase('http://localhost:8090/_');
-
-export const currentUser = writable(pb.authStore.model);
-
-pb.authStore.onChange((auth) => {
- console.log('authStore changed', auth);
- currentUser.set(pb.authStore.model);
-});
-
-export const pairedDevices = writable([]);
-export const currentDevice = writable('');
-
-interface HasData extends Record {
- data: { [key: string]: string };
-}
-export interface DeviceData extends Record {
- current_room: string;
- current_app: string;
- 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);
- }
-}
-
-export async function pair(pairingCode: string) {
- // find the device by pairing code
- try {
- let device = (await pb
- .collection('Device')
- .getFirstListItem(`pairing_code="${pairingCode}"`)) as DeviceData;
-
- // add it to the local data
- currentDevice.set(device.id);
- pairedDevices.set([...get(pairedDevices), device.id]);
-
- // add it to my account if logged in
- const u = get(currentUser)
- if (u) {
- u.devices.push(device.id);
- await pb.collection('Users').update(u.id, u);
- }
- return { error: null }
- } catch (e) {
- console.error('Not found: ' + e);
- if (e == "ClientResponseError 404: The requested resource wasn't found.") {
- return {
- error: 'Device not found with this pairing code.'
- }
- }
- return {
- error: e
- }
- }
-}
\ No newline at end of file
diff --git a/example_dashboard/src/routes/+page.svelte b/example_dashboard/src/routes/+page.svelte
index 76d26c0..3d8e04b 100644
--- a/example_dashboard/src/routes/+page.svelte
+++ b/example_dashboard/src/routes/+page.svelte
@@ -1,22 +1,24 @@