Skip to content

Commit

Permalink
Merge pull request #102 from Machriam/93-switchable-power-outlets
Browse files Browse the repository at this point in the history
Issue Power cycle requests with offline devices
  • Loading branch information
Machriam authored Jun 11, 2024
2 parents 07a4982 + a05a591 commit af91d83
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
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

0 comments on commit af91d83

Please sign in to comment.