📏 Formats app code

This commit is contained in:
Rune Harlyk
2024-02-23 09:16:20 +01:00
parent 2e1d99b1df
commit 22b54261f0
34 changed files with 1525 additions and 1400 deletions
+56 -57
View File
@@ -1,72 +1,71 @@
import { Result } from '$lib/utilities/result';
class FileService {
private dbName = 'fileStorageDB';
private dbVersion = 1;
private storeName = 'files';
private dbPromise: Promise<Result<IDBDatabase, string>>;
private dbName = 'fileStorageDB';
private dbVersion = 1;
private storeName = 'files';
private dbPromise: Promise<Result<IDBDatabase, string>>;
constructor() {
this.dbPromise = this.openDatabase();
}
constructor() {
this.dbPromise = this.openDatabase();
}
private async openDatabase(): Promise<Result<IDBDatabase, string>> {
return new Promise((resolve) => {
const request = indexedDB.open(this.dbName, this.dbVersion);
private async openDatabase(): Promise<Result<IDBDatabase, string>> {
return new Promise((resolve) => {
const request = indexedDB.open(this.dbName, this.dbVersion);
request.onerror = () => resolve(Result.err("Error opening database"));
request.onerror = () => resolve(Result.err('Error opening database'));
request.onsuccess = () => resolve(Result.ok(request.result));
request.onsuccess = () => resolve(Result.ok(request.result));
request.onupgradeneeded = (event) => {
const db = request.result;
if (!db.objectStoreNames.contains(this.storeName)) {
db.createObjectStore(this.storeName);
}
};
});
}
request.onupgradeneeded = (event) => {
const db = request.result;
if (!db.objectStoreNames.contains(this.storeName)) {
db.createObjectStore(this.storeName);
}
};
});
}
private async getStore(mode: IDBTransactionMode): Promise<Result<IDBObjectStore, string>> {
const dbResult = await this.dbPromise;
if (dbResult.isErr()) {
return Result.err("Database not initialized properly");
}
const db = dbResult.inner;
const transaction = db.transaction(this.storeName, mode);
return Result.ok(transaction.objectStore(this.storeName));
}
private async getStore(mode: IDBTransactionMode): Promise<Result<IDBObjectStore, string>> {
const dbResult = await this.dbPromise;
if (dbResult.isErr()) {
return Result.err('Database not initialized properly');
}
const db = dbResult.inner;
const transaction = db.transaction(this.storeName, mode);
return Result.ok(transaction.objectStore(this.storeName));
}
public async saveFile(key: string, file: Uint8Array): Promise<Result<IDBValidKey, string>> {
const storeResult = await this.getStore("readwrite");
if (storeResult.isErr()) {
return Result.err("Failed to access object store for writing");
}
const store = storeResult.inner;
public async saveFile(key: string, file: Uint8Array): Promise<Result<IDBValidKey, string>> {
const storeResult = await this.getStore('readwrite');
if (storeResult.isErr()) {
return Result.err('Failed to access object store for writing');
}
const store = storeResult.inner;
return new Promise((resolve) => {
const request = store.put(file, key);
request.onsuccess = () => resolve(Result.ok(request.result));
request.onerror = () => resolve(Result.err("Failed to save file"));
});
}
return new Promise((resolve) => {
const request = store.put(file, key);
request.onsuccess = () => resolve(Result.ok(request.result));
request.onerror = () => resolve(Result.err('Failed to save file'));
});
}
public async getFile(key: string): Promise<Result<Uint8Array | undefined, string>> {
const storeResult = await this.getStore("readonly");
if (storeResult.isErr()) {
return Result.err("Failed to access object store for reading");
}
const store = storeResult.inner;
public async getFile(key: string): Promise<Result<Uint8Array | undefined, string>> {
const storeResult = await this.getStore('readonly');
if (storeResult.isErr()) {
return Result.err('Failed to access object store for reading');
}
const store = storeResult.inner;
return new Promise((resolve) => {
const request = store.get(key);
request.onsuccess = () =>
resolve(request.result ? Result.ok(request.result) : Result.err("File content not found"))
request.onerror = () =>
resolve(Result.err("Failed to retrieve file"));
});
}
return new Promise((resolve) => {
const request = store.get(key);
request.onsuccess = () =>
resolve(request.result ? Result.ok(request.result) : Result.err('File content not found'));
request.onerror = () => resolve(Result.err('Failed to retrieve file'));
});
}
}
export default new FileService();
export default new FileService();
+2 -2
View File
@@ -1,2 +1,2 @@
export * from './file-service'
export * from './socket-service'
export { default as fileService } from './file-service';
export { default as socketService } from './socket-service';
+87 -76
View File
@@ -1,89 +1,100 @@
import { Result, Ok } from '$lib/utilities';
import { writable, type Writable } from 'svelte/store';
type WebsocketData = string | ArrayBufferLike | Blob | ArrayBufferView
type WebsocketData = string | ArrayBufferLike | Blob | ArrayBufferView;
// TODO
/**
* MOVE THE store to a store.ts file
*
* Make an object on the class that encapsulate all the stores
*
* Make the handle message function look up the type and set the value, to simplify the code
*/
class SocketService {
public isConnected = writable(false);
public angles = writable(new Int16Array(12).fill(0));
public log = writable([] as string[]);
public battery = writable({});
public mpu = writable({ heading: 0 });
public distances = writable({});
public settings = writable({});
public systemInfo = writable({} as number);
public dataBuffer = writable(new Float32Array(13));
public servoBuffer: Writable<Int16Array | number[]> = writable(new Int16Array(12));
public data = writable();
private socket!: WebSocket;
public isConnected = writable(false);
public angles = writable(new Int16Array(12).fill(0));
public log = writable([] as string[]);
public battery = writable({});
public mpu = writable({ heading: 0 });
public distances = writable({});
public settings = writable({});
public systemInfo = writable({} as number);
public dataBuffer = writable(new Float32Array(13));
public servoBuffer: Writable<Int16Array | number[]> = writable(new Int16Array(12));
public data = writable();
private socket!: WebSocket;
constructor() {}
constructor() {}
public connect(url: string): void {
this.socket = new WebSocket(url);
this.socket.binaryType = 'arraybuffer';
this.socket.onopen = () => this.handleConnected();
this.socket.onclose = () => this.handleDisconnected();
this.socket.onmessage = (event: unknown) => this.handleMessage(event);
}
public connect(url: string): void {
this.socket = new WebSocket(url);
this.socket.binaryType = "arraybuffer";
this.socket.onopen = () => this.handleConnected();
this.socket.onclose = () => this.handleDisconnected();
this.socket.onmessage = (event: unknown) => this.handleMessage(event);
}
public send(data: WebsocketData): Result<void, string> {
if (this.socket.readyState === WebSocket.OPEN) {
this.socket.send(data);
return Ok.void();
}
return Result.err('The connection is not open');
}
public send(data: WebsocketData): Result<void, string> {
if (this.socket.readyState === WebSocket.OPEN){
this.socket.send(data)
return Ok.void()
}
return Result.err("The connection is not open")
}
private handleConnected(): void {
this.isConnected.set(true);
}
private handleConnected(): void {
this.isConnected.set(true);
}
private handleDisconnected(): void {
this.isConnected.set(false);
}
private handleDisconnected(): void {
this.isConnected.set(false);
}
private handleMessage(event: any): void {
if (event.data instanceof ArrayBuffer) {
let buffer = new Int8Array(event.data);
if (buffer.length === 44) {
this.dataBuffer.set(new Float32Array(buffer.buffer));
}
} else {
let data = event.data;
try {
data = JSON.parse(event.data);
} catch (error) {
console.warn(error);
}
switch (data.type) {
case "angles":
this.angles.set(data.angles);
break;
case "logs":
this.log.set(data.logs);
break;
case "log":
this.log.update(entries => { entries.push(data.log); return entries; });
break;
case "settings":
this.settings.set(data.settings);
case "info":
this.systemInfo.set(data.info);
break;
case "mpu":
this.mpu.set(data.mpu);
break;
case "distances":
this.distances.set(data.distances);
break;
case "battery":
this.battery.set(data.battery);
break;
}
}
}
private handleMessage(event: any): void {
if (event.data instanceof ArrayBuffer) {
let buffer = new Int8Array(event.data);
if (buffer.length === 44) {
this.dataBuffer.set(new Float32Array(buffer.buffer));
}
} else {
let data = event.data;
try {
data = JSON.parse(event.data);
} catch (error) {
console.warn(error);
}
switch (data.type) {
case 'angles':
this.angles.set(data.angles);
break;
case 'logs':
this.log.set(data.logs);
break;
case 'log':
this.log.update((entries) => {
entries.push(data.log);
return entries;
});
break;
case 'settings':
this.settings.set(data.settings);
case 'info':
this.systemInfo.set(data.info);
break;
case 'mpu':
this.mpu.set(data.mpu);
break;
case 'distances':
this.distances.set(data.distances);
break;
case 'battery':
this.battery.set(data.battery);
break;
}
}
}
}
export default new SocketService();
export default new SocketService();