typhon/src/services/CloudControlBackend.ts

70 lines
2.1 KiB
TypeScript

import { ControlPortState, ControllerControlStatus } from "../domain/ControlTypes";
import { ControlApiError } from "./ControlApiError";
import { ControllerControlBackend } from "./ControllerControlBackend";
import { TelemetryCache } from "./TelemetryCache";
export class CloudControlBackend implements ControllerControlBackend {
public constructor(private readonly cache: TelemetryCache) {}
public async getStatus(): Promise<ControllerControlStatus> {
const latest = this.cache.getLatest();
const firstController = latest?.controllers[0] ?? null;
const connected = latest?.controllers.some((controller) => controller.online) ?? false;
return {
mode: "cloud",
backend: "cloud-api",
controllerMac: null,
connected,
paired: false,
telemetrySource: "cloud",
lastSnapshotEpochSeconds: latest?.collectedAtEpochSeconds ?? null,
capabilities: {
pairing: false,
portControl: false,
advancedRules: true
},
ports: this.mapPorts(firstController?.ports ?? []),
notes: [
"Control write APIs are disabled in cloud mode during v2 buildout."
]
};
}
public async pair(): Promise<ControllerControlStatus> {
throw new ControlApiError("pairing is not supported in cloud mode", 409);
}
public async getPorts(): Promise<ControlPortState[]> {
const latest = this.cache.getLatest();
const firstController = latest?.controllers[0] ?? null;
return this.mapPorts(firstController?.ports ?? []);
}
public async setPortSpeed(): Promise<void> {
throw new ControlApiError("per-port speed writes are not supported in cloud mode", 409);
}
private mapPorts(
ports: Array<{
port: number;
name: string;
fanGroup: string;
currentSpeedLevel: number;
online: boolean;
powerState: boolean;
}>
): ControlPortState[] {
return ports
.map((port) => ({
port: port.port,
name: port.name,
fanGroup: port.fanGroup,
currentSpeedLevel: port.currentSpeedLevel,
online: port.online,
powerState: port.powerState
}))
.sort((a, b) => a.port - b.port);
}
}