Add protection domain foundation

This commit is contained in:
2026-07-30 19:03:36 +02:00
parent a72d2c76e0
commit a88c4b2086
13 changed files with 909 additions and 2 deletions
@@ -0,0 +1,40 @@
import type {
BreakerTripCharacteristic,
FuseProtectionDeviceType,
FuseUtilizationCategory,
RatedResidualCurrentMa,
RcdType,
} from "../../shared/constants/protection-device.js";
export interface FuseProtectionDeviceConfiguration {
type: FuseProtectionDeviceType;
ratedCurrentA: number;
fuseUtilizationCategory: FuseUtilizationCategory;
}
export interface BreakerProtectionDeviceConfiguration {
type: "LS" | "AFDD";
ratedCurrentA: number;
tripCharacteristic: BreakerTripCharacteristic;
}
export interface ResidualCurrentDeviceConfiguration {
type: "FI";
ratedCurrentA: number;
rcdType: RcdType;
ratedResidualCurrentMa: RatedResidualCurrentMa;
}
export interface CombinedResidualCurrentBreakerConfiguration {
type: "FI_LS";
ratedCurrentA: number;
tripCharacteristic: BreakerTripCharacteristic;
rcdType: RcdType;
ratedResidualCurrentMa: RatedResidualCurrentMa;
}
export type ProtectionDeviceConfiguration =
| FuseProtectionDeviceConfiguration
| BreakerProtectionDeviceConfiguration
| ResidualCurrentDeviceConfiguration
| CombinedResidualCurrentBreakerConfiguration;
@@ -0,0 +1,165 @@
import {
circuitGroupCategoryNumbers,
type CircuitGroupCategory,
} from "../../shared/constants/circuit-group.js";
export type GroupedEquipmentIdentifierKind =
| "group_upstream_protection"
| "group_rcd"
| "circuit";
export interface ParsedGroupedEquipmentIdentifier {
category: CircuitGroupCategory;
groupNumber: number;
kind: GroupedEquipmentIdentifierKind;
circuitNumber: number | null;
}
const categoryByNumber: Record<string, CircuitGroupCategory> = {
"1": "lighting",
"2": "single_phase",
"3": "three_phase",
};
function requirePositiveInteger(value: number, fieldName: string): void {
if (!Number.isInteger(value) || value < 1) {
throw new RangeError(`${fieldName} must be a positive integer`);
}
}
function getGroupStem(
category: CircuitGroupCategory,
functionLetter: "F" | "Q",
groupNumber: number
): string {
requirePositiveInteger(groupNumber, "groupNumber");
return `-${circuitGroupCategoryNumbers[category]}${functionLetter}${groupNumber}`;
}
export function formatGroupUpstreamProtectionIdentifier(
category: CircuitGroupCategory,
groupNumber: number
): string {
return `${getGroupStem(category, "F", groupNumber)}.0`;
}
export function formatGroupRcdIdentifier(
category: CircuitGroupCategory,
groupNumber: number
): string {
return `${getGroupStem(category, "Q", groupNumber)}.0`;
}
export function formatGroupedCircuitIdentifier(
category: CircuitGroupCategory,
groupNumber: number,
circuitNumber: number
): string {
requirePositiveInteger(circuitNumber, "circuitNumber");
return `${getGroupStem(category, "F", groupNumber)}.${circuitNumber}`;
}
export function parseGroupedEquipmentIdentifier(
equipmentIdentifier: string
): ParsedGroupedEquipmentIdentifier | null {
const match = /^-(1|2|3)(F|Q)([1-9]\d*)\.(0|[1-9]\d*)$/.exec(
equipmentIdentifier
);
if (!match) {
return null;
}
const [, categoryNumber, functionLetter, groupNumberValue, suffixValue] =
match;
const category = categoryByNumber[categoryNumber];
const groupNumber = Number(groupNumberValue);
const suffix = Number(suffixValue);
if (functionLetter === "Q") {
return suffix === 0
? {
category,
groupNumber,
kind: "group_rcd",
circuitNumber: null,
}
: null;
}
if (suffix === 0) {
return {
category,
groupNumber,
kind: "group_upstream_protection",
circuitNumber: null,
};
}
return {
category,
groupNumber,
kind: "circuit",
circuitNumber: suffix,
};
}
export function getNextGroupedCircuitNumber(
category: CircuitGroupCategory,
groupNumber: number,
equipmentIdentifiers: Iterable<string>
): number {
requirePositiveInteger(groupNumber, "groupNumber");
let highestCircuitNumber = 0;
for (const equipmentIdentifier of equipmentIdentifiers) {
const parsed = parseGroupedEquipmentIdentifier(equipmentIdentifier);
if (
parsed?.kind === "circuit" &&
parsed.category === category &&
parsed.groupNumber === groupNumber &&
parsed.circuitNumber !== null
) {
highestCircuitNumber = Math.max(
highestCircuitNumber,
parsed.circuitNumber
);
}
}
return highestCircuitNumber + 1;
}
export function getNextGroupedCircuitIdentifier(
category: CircuitGroupCategory,
groupNumber: number,
equipmentIdentifiers: Iterable<string>
): string {
return formatGroupedCircuitIdentifier(
category,
groupNumber,
getNextGroupedCircuitNumber(
category,
groupNumber,
equipmentIdentifiers
)
);
}
export function getNextCircuitGroupNumber(
category: CircuitGroupCategory,
groups: Iterable<{
category: CircuitGroupCategory;
groupNumber: number;
}>
): number {
let highestGroupNumber = 0;
for (const group of groups) {
if (group.category === category) {
requirePositiveInteger(group.groupNumber, "groupNumber");
highestGroupNumber = Math.max(highestGroupNumber, group.groupNumber);
}
}
return highestGroupNumber + 1;
}
@@ -0,0 +1,41 @@
import type { ProtectionDeviceConfiguration } from "../models/protection-device.model.js";
import type { CircuitGroupCategory } from "../../shared/constants/circuit-group.js";
const defaultCircuitProtectionByCategory = {
lighting: {
type: "LS",
ratedCurrentA: 10,
tripCharacteristic: "B",
},
single_phase: {
type: "FI_LS",
ratedCurrentA: 16,
tripCharacteristic: "B",
rcdType: "A",
ratedResidualCurrentMa: 30,
},
three_phase: {
type: "FI_LS",
ratedCurrentA: 16,
tripCharacteristic: "B",
rcdType: "A",
ratedResidualCurrentMa: 30,
},
} as const satisfies Record<CircuitGroupCategory, ProtectionDeviceConfiguration>;
const defaultGroupRcd = {
type: "FI",
ratedCurrentA: 40,
rcdType: "A",
ratedResidualCurrentMa: 30,
} as const satisfies ProtectionDeviceConfiguration;
export function createDefaultCircuitProtection(
category: CircuitGroupCategory
): ProtectionDeviceConfiguration {
return { ...defaultCircuitProtectionByCategory[category] };
}
export function createDefaultGroupRcd(): ProtectionDeviceConfiguration {
return { ...defaultGroupRcd };
}
+20
View File
@@ -0,0 +1,20 @@
export const circuitGroupCategories = [
"lighting",
"single_phase",
"three_phase",
] as const;
export type CircuitGroupCategory = (typeof circuitGroupCategories)[number];
export const circuitGroupCategoryNumbers: Record<CircuitGroupCategory, 1 | 2 | 3> =
{
lighting: 1,
single_phase: 2,
three_phase: 3,
};
export const circuitGroupCategoryLabels: Record<CircuitGroupCategory, string> = {
lighting: "Beleuchtung",
single_phase: "1-phasig",
three_phase: "3-phasig",
};
@@ -0,0 +1,54 @@
export const distributionBoardComponentRoles = [
"main_switch",
"surge_protective_device",
"group_upstream_protection",
"group_residual_current_protection",
"auxiliary",
] as const;
export type DistributionBoardComponentRole =
(typeof distributionBoardComponentRoles)[number];
export const distributionBoardComponentPlacements = [
"header",
"group",
"footer",
] as const;
export type DistributionBoardComponentPlacement =
(typeof distributionBoardComponentPlacements)[number];
export const distributionBoardComponentRoleLabels: Record<
DistributionBoardComponentRole,
string
> = {
main_switch: "Hauptschalter",
surge_protective_device: "Überspannungsableiter",
group_upstream_protection: "Gruppenvorsicherung",
group_residual_current_protection: "Gruppen-FI",
auxiliary: "Verteilergerät",
};
export const defaultMainSwitchComponent = {
equipmentIdentifier: "-Q0",
name: "Hauptschalter",
role: "main_switch",
placement: "header",
} as const satisfies {
equipmentIdentifier: string;
name: string;
role: DistributionBoardComponentRole;
placement: DistributionBoardComponentPlacement;
};
export const defaultSurgeProtectiveDeviceComponent = {
equipmentIdentifier: "-FA",
name: "Überspannungsableiter",
role: "surge_protective_device",
placement: "header",
} as const satisfies {
equipmentIdentifier: string;
name: string;
role: DistributionBoardComponentRole;
placement: DistributionBoardComponentPlacement;
};
+108
View File
@@ -0,0 +1,108 @@
export const fuseProtectionDeviceTypes = [
"D02",
"NH000",
"NH00",
"NH0",
"NH1",
"NH2",
"NH3",
] as const;
export type FuseProtectionDeviceType =
(typeof fuseProtectionDeviceTypes)[number];
export const breakerProtectionDeviceTypes = ["LS", "FI_LS", "AFDD"] as const;
export type BreakerProtectionDeviceType =
(typeof breakerProtectionDeviceTypes)[number];
export const protectionDeviceTypes = [
...fuseProtectionDeviceTypes,
"LS",
"FI",
"FI_LS",
"AFDD",
] as const;
export type ProtectionDeviceType = (typeof protectionDeviceTypes)[number];
export const fuseUtilizationCategories = ["gG", "gR"] as const;
export type FuseUtilizationCategory =
(typeof fuseUtilizationCategories)[number];
export const breakerTripCharacteristics = ["B", "C", "D", "Z"] as const;
export type BreakerTripCharacteristic =
(typeof breakerTripCharacteristics)[number];
export const rcdTypes = ["A", "AC", "B", "B+"] as const;
export type RcdType = (typeof rcdTypes)[number];
export const ratedResidualCurrentsMa = [10, 30, 100, 300, 500] as const;
export type RatedResidualCurrentMa =
(typeof ratedResidualCurrentsMa)[number];
const d02RatedCurrentsA = [20, 25, 32, 35, 40, 50, 63] as const;
const nh000AndNh00RatedCurrentsA = [
6, 10, 16, 20, 25, 32, 35, 40, 50, 63, 80, 100, 125, 160,
] as const;
const nh0RatedCurrentsA = [
6, 10, 16, 20, 25, 32, 35, 40, 50, 63, 80, 100, 125, 160, 200, 224, 250,
] as const;
const nh1RatedCurrentsA = [
16, 20, 25, 32, 35, 40, 50, 63, 80, 100, 125, 160, 200, 224, 250,
] as const;
const nh2RatedCurrentsA = [
25, 32, 35, 40, 50, 63, 80, 100, 125, 160, 200, 224, 250, 300, 315, 355,
400,
] as const;
const nh3RatedCurrentsA = [
50, 63, 80, 100, 125, 160, 200, 224, 250, 315, 355, 400, 500, 630, 800,
] as const;
const breakerRatedCurrentsA = [
6, 10, 13, 16, 20, 25, 32, 40, 50, 63, 80, 100, 125,
] as const;
const rcdRatedCurrentsA = [16, 25, 40, 63, 80, 100, 125] as const;
export const allowedRatedCurrentsAByProtectionDeviceType = {
D02: d02RatedCurrentsA,
NH000: nh000AndNh00RatedCurrentsA,
NH00: nh000AndNh00RatedCurrentsA,
NH0: nh0RatedCurrentsA,
NH1: nh1RatedCurrentsA,
NH2: nh2RatedCurrentsA,
NH3: nh3RatedCurrentsA,
LS: breakerRatedCurrentsA,
FI: rcdRatedCurrentsA,
FI_LS: breakerRatedCurrentsA,
AFDD: breakerRatedCurrentsA,
} as const satisfies Record<ProtectionDeviceType, readonly number[]>;
export const protectionDeviceTypeLabels: Record<ProtectionDeviceType, string> = {
D02: "D02 / Neozed",
NH000: "NH000",
NH00: "NH00",
NH0: "NH0",
NH1: "NH1",
NH2: "NH2",
NH3: "NH3",
LS: "LS",
FI: "FI",
FI_LS: "FI/LS",
AFDD: "AFDD",
};
export function isAllowedRatedCurrentA(
type: ProtectionDeviceType,
ratedCurrentA: number
): boolean {
const allowedValues: readonly number[] =
allowedRatedCurrentsAByProtectionDeviceType[type];
return allowedValues.includes(ratedCurrentA);
}
export function isAllowedRatedResidualCurrentMa(
ratedResidualCurrentMa: number
): ratedResidualCurrentMa is RatedResidualCurrentMa {
const allowedValues: readonly number[] = ratedResidualCurrentsMa;
return allowedValues.includes(ratedResidualCurrentMa);
}
@@ -0,0 +1,95 @@
import { z } from "zod";
import {
breakerTripCharacteristics,
fuseProtectionDeviceTypes,
fuseUtilizationCategories,
isAllowedRatedCurrentA,
isAllowedRatedResidualCurrentMa,
protectionDeviceTypes,
rcdTypes,
} from "../constants/protection-device.js";
function hasValue(value: unknown): boolean {
return value !== undefined;
}
export const protectionDeviceConfigurationSchema = z
.object({
type: z.enum(protectionDeviceTypes),
ratedCurrentA: z.number().finite().positive(),
fuseUtilizationCategory: z.enum(fuseUtilizationCategories).optional(),
tripCharacteristic: z.enum(breakerTripCharacteristics).optional(),
rcdType: z.enum(rcdTypes).optional(),
ratedResidualCurrentMa: z
.number()
.int()
.refine(isAllowedRatedResidualCurrentMa, {
message: "Der Bemessungsdifferenzstrom ist nicht zulässig.",
})
.optional(),
})
.strict()
.superRefine((value, context) => {
if (!isAllowedRatedCurrentA(value.type, value.ratedCurrentA)) {
context.addIssue({
code: "custom",
path: ["ratedCurrentA"],
message: "Der Bemessungsstrom ist für diesen Schutzgerätetyp nicht zulässig.",
});
}
const isFuse = (fuseProtectionDeviceTypes as readonly string[]).includes(
value.type
);
const hasBreakerFunction =
value.type === "LS" || value.type === "FI_LS" || value.type === "AFDD";
const hasResidualCurrentFunction =
value.type === "FI" || value.type === "FI_LS";
if (isFuse !== hasValue(value.fuseUtilizationCategory)) {
context.addIssue({
code: "custom",
path: ["fuseUtilizationCategory"],
message: isFuse
? "Für diesen Sicherungstyp ist eine Sicherungscharakteristik erforderlich."
: "Dieser Schutzgerätetyp darf keine Sicherungscharakteristik enthalten.",
});
}
if (hasBreakerFunction !== hasValue(value.tripCharacteristic)) {
context.addIssue({
code: "custom",
path: ["tripCharacteristic"],
message: hasBreakerFunction
? "Für diesen Schutzgerätetyp ist eine Auslösecharakteristik erforderlich."
: "Dieser Schutzgerätetyp darf keine Auslösecharakteristik enthalten.",
});
}
if (hasResidualCurrentFunction !== hasValue(value.rcdType)) {
context.addIssue({
code: "custom",
path: ["rcdType"],
message: hasResidualCurrentFunction
? "Für diesen Schutzgerätetyp ist ein FI-Typ erforderlich."
: "Dieser Schutzgerätetyp darf keinen FI-Typ enthalten.",
});
}
if (
hasResidualCurrentFunction !==
hasValue(value.ratedResidualCurrentMa)
) {
context.addIssue({
code: "custom",
path: ["ratedResidualCurrentMa"],
message: hasResidualCurrentFunction
? "Für diesen Schutzgerätetyp ist ein Bemessungsdifferenzstrom erforderlich."
: "Dieser Schutzgerätetyp darf keinen Bemessungsdifferenzstrom enthalten.",
});
}
});
export type ProtectionDeviceConfigurationInput = z.infer<
typeof protectionDeviceConfigurationSchema
>;