Persist circuit protection updates
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
import { protectionDeviceConfigurationSchema } from "../../shared/validation/protection-device.schemas.js";
|
||||
import type {
|
||||
BreakerTripCharacteristic,
|
||||
FuseUtilizationCategory,
|
||||
ProtectionDeviceType,
|
||||
RcdType,
|
||||
} from "../../shared/constants/protection-device.js";
|
||||
import type { SerializedProjectCommand } from "./project-command.model.js";
|
||||
|
||||
export const circuitProtectionUpdateCommandType =
|
||||
"circuit-protection.update" as const;
|
||||
export const circuitProtectionUpdateCommandSchemaVersion = 1 as const;
|
||||
|
||||
export interface CircuitProtectionSnapshot {
|
||||
circuitId: string;
|
||||
type: ProtectionDeviceType;
|
||||
ratedCurrentA: number;
|
||||
fuseUtilizationCategory: FuseUtilizationCategory | null;
|
||||
tripCharacteristic: BreakerTripCharacteristic | null;
|
||||
rcdType: RcdType | null;
|
||||
ratedResidualCurrentMa: number | null;
|
||||
}
|
||||
|
||||
interface CircuitProtectionUpdatePayload {
|
||||
circuitId: string;
|
||||
expected: CircuitProtectionSnapshot | null;
|
||||
target: CircuitProtectionSnapshot | null;
|
||||
}
|
||||
|
||||
export interface CircuitProtectionUpdateProjectCommand
|
||||
extends SerializedProjectCommand<CircuitProtectionUpdatePayload> {
|
||||
schemaVersion: typeof circuitProtectionUpdateCommandSchemaVersion;
|
||||
type: typeof circuitProtectionUpdateCommandType;
|
||||
}
|
||||
|
||||
export function createCircuitProtectionUpdateProjectCommand(
|
||||
circuitId: string,
|
||||
expected: CircuitProtectionSnapshot | null,
|
||||
target: CircuitProtectionSnapshot | null
|
||||
): CircuitProtectionUpdateProjectCommand {
|
||||
const command: CircuitProtectionUpdateProjectCommand = {
|
||||
schemaVersion: circuitProtectionUpdateCommandSchemaVersion,
|
||||
type: circuitProtectionUpdateCommandType,
|
||||
payload: { circuitId, expected, target },
|
||||
};
|
||||
assertCircuitProtectionUpdateProjectCommand(command);
|
||||
return command;
|
||||
}
|
||||
|
||||
export function assertCircuitProtectionUpdateProjectCommand(
|
||||
command: SerializedProjectCommand<unknown>
|
||||
): asserts command is CircuitProtectionUpdateProjectCommand {
|
||||
if (
|
||||
command.schemaVersion !== circuitProtectionUpdateCommandSchemaVersion ||
|
||||
command.type !== circuitProtectionUpdateCommandType ||
|
||||
!isPlainObject(command.payload) ||
|
||||
Object.keys(command.payload).length !== 3
|
||||
) {
|
||||
throw new Error("Unsupported circuit-protection update command.");
|
||||
}
|
||||
const { circuitId, expected, target } = command.payload;
|
||||
if (typeof circuitId !== "string" || !circuitId.trim()) {
|
||||
throw new Error("Circuit protection requires a circuit id.");
|
||||
}
|
||||
if (expected === null && target === null) {
|
||||
throw new Error("Circuit protection update must change state.");
|
||||
}
|
||||
if (expected !== null) {
|
||||
assertSnapshot(expected, circuitId);
|
||||
}
|
||||
if (target !== null) {
|
||||
assertSnapshot(target, circuitId);
|
||||
}
|
||||
if (JSON.stringify(expected) === JSON.stringify(target)) {
|
||||
throw new Error("Circuit protection update must change state.");
|
||||
}
|
||||
}
|
||||
|
||||
function assertSnapshot(value: unknown, circuitId: string) {
|
||||
if (
|
||||
!isPlainObject(value) ||
|
||||
Object.keys(value).length !== 7 ||
|
||||
value.circuitId !== circuitId
|
||||
) {
|
||||
throw new Error("Circuit-protection snapshot is invalid.");
|
||||
}
|
||||
const result = protectionDeviceConfigurationSchema.safeParse({
|
||||
type: value.type,
|
||||
ratedCurrentA: value.ratedCurrentA,
|
||||
...(value.fuseUtilizationCategory === null
|
||||
? {}
|
||||
: { fuseUtilizationCategory: value.fuseUtilizationCategory }),
|
||||
...(value.tripCharacteristic === null
|
||||
? {}
|
||||
: { tripCharacteristic: value.tripCharacteristic }),
|
||||
...(value.rcdType === null ? {} : { rcdType: value.rcdType }),
|
||||
...(value.ratedResidualCurrentMa === null
|
||||
? {}
|
||||
: { ratedResidualCurrentMa: value.ratedResidualCurrentMa }),
|
||||
});
|
||||
if (!result.success) {
|
||||
throw new Error("Circuit-protection configuration is invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return value !== null && typeof value === "object" && !Array.isArray(value);
|
||||
}
|
||||
Reference in New Issue
Block a user