error message for wrong pasword, function to set device data directly

dev
Anton Franzluebbers 2023-08-06 13:00:53 -04:00
parent 42ee6458d9
commit 8fa9a4c056
2 changed files with 34 additions and 12 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@velaboratory/velconnect-svelte", "name": "@velaboratory/velconnect-svelte",
"version": "1.0.3", "version": "1.0.4",
"description": "Use VEL-Connect with a Svelte dashboard", "description": "Use VEL-Connect with a Svelte dashboard",
"main": "src/index.js", "main": "src/index.js",
"files": [ "files": [

View File

@ -49,8 +49,11 @@ let unsubscribeRoomData: () => void;
let unsubscribeCurrentDevice: () => void; let unsubscribeCurrentDevice: () => void;
let unsubscribeCurrentUser: () => void; let unsubscribeCurrentUser: () => void;
/** This is read-only data about the device */
export let deviceFields = writable<Device | null>(null); export let deviceFields = writable<Device | null>(null);
/** This is a profile with custom data about a device config */
export let deviceData = writable<DataBlock | null>(null); export let deviceData = writable<DataBlock | null>(null);
/** Data attached to a room */
export let roomData = writable<DataBlock | null>(null); export let roomData = writable<DataBlock | null>(null);
export let sending = false; export let sending = false;
@ -60,6 +63,7 @@ export async function startListening(baseUrl: string) {
if (get(currentDeviceId) != "") { if (get(currentDeviceId) != "") {
const d = (await pb.collection("Device").getOne(get(currentDeviceId), { const d = (await pb.collection("Device").getOne(get(currentDeviceId), {
expand: "data", expand: "data",
$cancelKey: "package",
})) as Device; })) as Device;
deviceData.set(d.expand.data as DataBlock); deviceData.set(d.expand.data as DataBlock);
// we don't need expand anymore, since it doesn't work in subscribe() // we don't need expand anymore, since it doesn't work in subscribe()
@ -67,14 +71,17 @@ export async function startListening(baseUrl: string) {
deviceFields.set(d); deviceFields.set(d);
} }
log("Subscribing to currentDeviceId");
unsubscribeCurrentDevice = currentDeviceId.subscribe(async (val) => { unsubscribeCurrentDevice = currentDeviceId.subscribe(async (val) => {
log("currentDeviceId subscribe change event"); log(`currentDeviceId subscribe change event: ${val}`);
unsubscribeDeviceFields?.(); unsubscribeDeviceFields?.();
unsubscribeDeviceData?.(); unsubscribeDeviceData?.();
if (val != "") { if (val != "") {
const d = (await pb log(`currentDeviceId is not empty`);
.collection("Device") const d = (await pb.collection("Device").getOne(get(currentDeviceId), {
.getOne(get(currentDeviceId), { expand: "data" })) as Device; expand: "data",
$cancelKey: "package",
})) as Device;
deviceData.set(d.expand.data as DataBlock); deviceData.set(d.expand.data as DataBlock);
// we don't need expand anymore, since it doesn't work in subscribe() // we don't need expand anymore, since it doesn't work in subscribe()
d.expand = {}; d.expand = {};
@ -115,9 +122,10 @@ export async function startListening(baseUrl: string) {
}); });
unsubscribeCurrentUser = currentUser.subscribe((user) => { unsubscribeCurrentUser = currentUser.subscribe((user) => {
log(`currentUser changed ${user}`); log(`currentUser changed: ${JSON.stringify(user)}`);
pairedDevices.set(user?.["devices"] ?? []); pairedDevices.set(user?.["devices"] ?? []);
currentDeviceId.set(get(pairedDevices)[0] ?? ""); currentDeviceId.set(get(pairedDevices)[0] ?? "");
log("set current device to: " + get(currentDeviceId));
}); });
} }
@ -171,14 +179,14 @@ export function delayedSend() {
abortController = newAbortController; abortController = newAbortController;
setTimeout(() => { setTimeout(() => {
if (!newAbortController.signal.aborted) { if (!newAbortController.signal.aborted) {
send(); sendNow();
} else { } else {
console.log("aborted"); console.log("aborted");
} }
}, 1000); }, 1000);
} }
export function send() { export function sendNow() {
console.log("sending..."); console.log("sending...");
sending = true; sending = true;
let promises: Promise<any>[] = []; let promises: Promise<any>[] = [];
@ -201,6 +209,8 @@ export function send() {
} }
export function removeDevice(d: string) { export function removeDevice(d: string) {
log("Removing device...");
pairedDevices.set(get(pairedDevices).filter((i) => i != d)); pairedDevices.set(get(pairedDevices).filter((i) => i != d));
if (get(currentDeviceId) == d) { if (get(currentDeviceId) == d) {
@ -223,6 +233,8 @@ export function removeDevice(d: string) {
export async function pair(pairingCode: string) { export async function pair(pairingCode: string) {
try { try {
log("Pairing...");
// find the device by pairing code // find the device by pairing code
const device = (await pb const device = (await pb
.collection("Device") .collection("Device")
@ -278,10 +290,12 @@ export async function pair(pairingCode: string) {
export async function login(username: string, password: string) { export async function login(username: string, password: string) {
try { try {
await pb.collection("Users").authWithPassword(username, password); const ret = await pb
return {}; .collection("Users")
} catch (err: any) { .authWithPassword(username, password);
return err; return { ret };
} catch (error: any) {
return { error };
} }
} }
@ -303,6 +317,14 @@ export function signOut() {
pb.authStore.clear(); pb.authStore.clear();
} }
export async function setDeviceData(data: { [key: string]: any }) {
const d = get(deviceData);
if (d) {
d.data = { ...d.data, ...data };
await pb.collection("DataBlock").update(d.id, d);
}
}
function log(msg: string) { function log(msg: string) {
if (debugLog) { if (debugLog) {
console.log(msg); console.log(msg);