Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue Power cycle requests with offline devices #102

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ 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!;
}
transformOptions(options: RequestInit): Promise<RequestInit> {
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<any>) {
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);
})();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ interface Uint8Array {
toInt32(): number;
toInt64(): bigint;
}
interface Promise<T> {
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);
Expand Down Expand Up @@ -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<T>): 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;
}
Expand Down