92 lines
2.0 KiB
TypeScript
92 lines
2.0 KiB
TypeScript
import z from "zod";
|
|
import { logger } from "@/logger";
|
|
import { NanoKVMClient } from "./nano-kvm-client";
|
|
import {
|
|
type GpioSchema,
|
|
GpioSuccess,
|
|
type InfoSchema,
|
|
InfoSuccess,
|
|
type NanoKVMClientOptions,
|
|
TriggerPowerInput,
|
|
TriggerPowerSchema,
|
|
} from "./nano-kvm-client.schema";
|
|
|
|
export class NanoKVMService {
|
|
private client: NanoKVMClient;
|
|
|
|
constructor(options: {
|
|
clientOptions: z.input<typeof NanoKVMClientOptions>;
|
|
}) {
|
|
this.client = new NanoKVMClient(options.clientOptions);
|
|
}
|
|
|
|
async init() {
|
|
await this.client.init();
|
|
}
|
|
|
|
async getInfo(): Promise<z.output<typeof InfoSchema>> {
|
|
const data = await (await this.client.fetch("/api/vm/info", "GET")).json();
|
|
|
|
const result = InfoSuccess.safeParse(data);
|
|
|
|
if (!result.success) {
|
|
logger.error(data);
|
|
logger.fatal(`Failed getting info: `, z.prettifyError(result.error));
|
|
|
|
if (data === "unauthorized") {
|
|
await this.client.initAuth();
|
|
}
|
|
|
|
throw result.error;
|
|
}
|
|
|
|
return result.data.data;
|
|
}
|
|
|
|
async getGpio(): Promise<z.output<typeof GpioSchema>> {
|
|
const response = await this.client.fetch("/api/vm/gpio", "GET");
|
|
|
|
const data = await response.json();
|
|
|
|
const result = GpioSuccess.safeParse(data);
|
|
|
|
if (!result.success) {
|
|
logger.error(data);
|
|
logger.fatal(`Failed getting gpio: `, z.prettifyError(result.error));
|
|
|
|
if (data === "unauthorized") {
|
|
await this.client.initAuth();
|
|
}
|
|
|
|
throw result.error;
|
|
}
|
|
|
|
return result.data.data;
|
|
}
|
|
|
|
async triggerPower(input: z.input<typeof TriggerPowerInput>) {
|
|
const parsedInput = TriggerPowerInput.parse(input);
|
|
|
|
console.log(parsedInput);
|
|
|
|
const body = new FormData();
|
|
|
|
body.append("Type", parsedInput.type);
|
|
body.append("Duration", parsedInput.duration.toString());
|
|
|
|
const response = await this.client.fetch("/api/vm/gpio", "POST", body);
|
|
|
|
const data = await response.json();
|
|
|
|
const parsed = TriggerPowerSchema.parse(data);
|
|
|
|
return parsed;
|
|
}
|
|
|
|
async triggerReset() {
|
|
const result = await this.triggerPower({ duration: 8000 });
|
|
|
|
return result;
|
|
}
|
|
}
|