diff --git a/GatewayApp/Frontend/Plantmonitor.Website/src/features/deviceConfiguration/+page.svelte b/GatewayApp/Frontend/Plantmonitor.Website/src/features/deviceConfiguration/+page.svelte index 249b46a1..ba93c9df 100644 --- a/GatewayApp/Frontend/Plantmonitor.Website/src/features/deviceConfiguration/+page.svelte +++ b/GatewayApp/Frontend/Plantmonitor.Website/src/features/deviceConfiguration/+page.svelte @@ -135,7 +135,7 @@ const switchDevices = devices.filter((d) => d.health.state != undefined && d.health.state & HealthState.CanSwitchOutlets); const outletClient = new PowerOutletClient(); for (let i = 0; i < switchDevices.length; i++) { - await outletClient.switchOutlet(switchDevices[i].ip, code); + await outletClient.disablePrompts().switchOutlet(switchDevices[i].ip, code).try(); await Task.delay(200); } } @@ -147,8 +147,13 @@ const outletDevices = devices.filter((d) => d.health?.deviceId != undefined); for (let i = 0; i < outletDevices.length; i++) { const deviceId = outletDevices[i].health.deviceId!; - var outlet = await outletClient.powerOutletForDevice(deviceId); - outletByDevice[deviceId] = outlet; + const {result, error, hasError} = await outletClient.powerOutletForDevice(deviceId).try(); + if (hasError) { + console.log(error); + outletByDevice[deviceId] = null; + continue; + } + outletByDevice[deviceId] = result; } } catch (ex) { console.log(ex); diff --git a/GatewayApp/Frontend/Plantmonitor.Website/src/services/GatewayAppApiBase.ts b/GatewayApp/Frontend/Plantmonitor.Website/src/services/GatewayAppApiBase.ts index a4ccafc8..44e5fd72 100644 --- a/GatewayApp/Frontend/Plantmonitor.Website/src/services/GatewayAppApiBase.ts +++ b/GatewayApp/Frontend/Plantmonitor.Website/src/services/GatewayAppApiBase.ts @@ -2,6 +2,7 @@ import { ApiException } from "./GatewayAppApi"; import { dev } from "$app/environment"; import { Constants } from "../Constants"; export class GatewayAppApiBase { + noPrompt = false; getBaseUrl(_: string, defaultUrl: string | undefined): string { const url = dev ? Constants.developmentUrl : `https://${location.hostname}`; return defaultUrl?.isEmpty() ?? true ? url : defaultUrl!; @@ -9,13 +10,17 @@ export class GatewayAppApiBase { transformOptions(options: RequestInit): Promise { return Promise.resolve(options); } + disablePrompts() { + this.noPrompt = true; + return this; + } // eslint-disable-next-line @typescript-eslint/no-explicit-any transformResult(url_: string, _response: Response, defaultFunction: (_response: Response) => Promise) { if (_response.status.isSuccessStatusCode()) return defaultFunction(_response); return (async () => { const text = await _response.text() const formattedText = `${new Date().toISOString()}\n${text.replaceAll("\\n", "\n").replaceAll("\"", "")}`; - alert(formattedText); + if (!this.noPrompt) alert(formattedText); throw new ApiException("An error occured", _response.status, formattedText, _response.headers, null); })(); } diff --git a/GatewayApp/Frontend/Plantmonitor.Website/src/types/typeExtensions.d.ts b/GatewayApp/Frontend/Plantmonitor.Website/src/types/typeExtensions.d.ts index eabcbdfd..ca70b03e 100644 --- a/GatewayApp/Frontend/Plantmonitor.Website/src/types/typeExtensions.d.ts +++ b/GatewayApp/Frontend/Plantmonitor.Website/src/types/typeExtensions.d.ts @@ -26,6 +26,9 @@ interface Uint8Array { toInt32(): number; toInt64(): bigint; } +interface Promise { + try(): Promise<{ result: T, error: unknown, hasError: boolean }>; +} Uint8Array.prototype.toInt32 = function (this: Uint8Array): number { return new DataView(this.slice(0, 4).buffer).getInt32(0, true); @@ -70,6 +73,15 @@ BigInt.prototype.fromTicksToDate = function (this: bigint): Date { const milliseconds = Number(this / tickDivider + zeroTime); return new Date(milliseconds); } +Promise.prototype.try = async function (this: Promise): Promise<{ result: T, error: unknown, hasError: boolean }> { + try { + const result = await this; + return { result, error: {}, hasError: false }; + } + catch (ex) { + return { result: {}, error: ex, hasError: true }; + } +} Number.prototype.isSuccessStatusCode = function (this: number): boolean { return this == 200 || this == 204; }