Standardize phase type labels
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
import {
|
||||
resolveCircuitPhaseType,
|
||||
resolveProjectVoltage,
|
||||
normalizeKnownElectricalPhaseType,
|
||||
} from "../services/project-voltage.service.js";
|
||||
|
||||
export const legacyProjectStateSnapshotSchemaVersion = 1 as const;
|
||||
@@ -225,7 +226,7 @@ export function parseProjectStateSnapshot(
|
||||
value: unknown
|
||||
): ProjectStateSnapshot {
|
||||
const version = isPlainObject(value) ? value.schemaVersion : undefined;
|
||||
const snapshot =
|
||||
const parsedSnapshot =
|
||||
version === legacyProjectStateSnapshotSchemaVersion
|
||||
? upgradeSupplyTypesProjectStateSnapshot(
|
||||
upgradeDistributionBoardProjectStateSnapshot(
|
||||
@@ -255,10 +256,57 @@ export function parseProjectStateSnapshot(
|
||||
supplyTypesProjectStateSnapshotSchema.parse(value)
|
||||
)
|
||||
: projectStateSnapshotSchema.parse(value);
|
||||
const snapshot = normalizeSnapshotPhaseTypes(parsedSnapshot);
|
||||
assertProjectStateSnapshotRelations(snapshot);
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
function normalizeSnapshotPhaseTypes(
|
||||
snapshot: ProjectStateSnapshot
|
||||
): ProjectStateSnapshot {
|
||||
const sectionById = new Map(
|
||||
snapshot.circuitSections.map((section) => [section.id, section])
|
||||
);
|
||||
const projectDeviceById = new Map(
|
||||
snapshot.projectDevices.map((device) => [device.id, device])
|
||||
);
|
||||
return {
|
||||
...snapshot,
|
||||
circuits: snapshot.circuits.map((circuit) => {
|
||||
const section = sectionById.get(circuit.sectionId);
|
||||
const deviceRows = circuit.deviceRows.map((row) => {
|
||||
const normalizedPhaseType =
|
||||
normalizeKnownElectricalPhaseType(row.phaseType);
|
||||
const linkedPhaseType = row.linkedProjectDeviceId
|
||||
? projectDeviceById.get(row.linkedProjectDeviceId)?.phaseType
|
||||
: undefined;
|
||||
return {
|
||||
...row,
|
||||
phaseType:
|
||||
normalizedPhaseType ??
|
||||
linkedPhaseType ??
|
||||
(section?.key === "three_phase"
|
||||
? "three_phase"
|
||||
: "single_phase"),
|
||||
};
|
||||
});
|
||||
return {
|
||||
...circuit,
|
||||
voltage: section
|
||||
? resolveProjectVoltage(
|
||||
resolveCircuitPhaseType(
|
||||
section.key,
|
||||
deviceRows.map((row) => row.phaseType)
|
||||
),
|
||||
snapshot.project
|
||||
)
|
||||
: circuit.voltage,
|
||||
deviceRows,
|
||||
};
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
function upgradeLegacyProjectStateSnapshot(
|
||||
snapshot: z.infer<typeof legacyProjectStateSnapshotSchema>
|
||||
): z.infer<typeof previousProjectStateSnapshotSchema> {
|
||||
|
||||
@@ -1,5 +1,39 @@
|
||||
export type ElectricalPhaseType = "single_phase" | "three_phase";
|
||||
|
||||
export function isElectricalPhaseType(
|
||||
value: unknown
|
||||
): value is ElectricalPhaseType {
|
||||
return value === "single_phase" || value === "three_phase";
|
||||
}
|
||||
|
||||
export function normalizeKnownElectricalPhaseType(
|
||||
value: string | null
|
||||
): ElectricalPhaseType | null {
|
||||
if (value === null) {
|
||||
return null;
|
||||
}
|
||||
const normalized = value
|
||||
.trim()
|
||||
.toLocaleLowerCase("de-DE")
|
||||
.replaceAll("-", "")
|
||||
.replaceAll("_", "");
|
||||
if (
|
||||
["1ph", "1phase", "1phasig", "einphasig", "singlephase"].includes(
|
||||
normalized
|
||||
)
|
||||
) {
|
||||
return "single_phase";
|
||||
}
|
||||
if (
|
||||
["3ph", "3phase", "3phasig", "dreiphasig", "threephase"].includes(
|
||||
normalized
|
||||
)
|
||||
) {
|
||||
return "three_phase";
|
||||
}
|
||||
throw new Error(`Unknown electrical phase type: ${value}`);
|
||||
}
|
||||
|
||||
export interface ProjectVoltageSettings {
|
||||
singlePhaseVoltageV: number;
|
||||
threePhaseVoltageV: number;
|
||||
@@ -25,8 +59,7 @@ export function resolveCircuitPhaseType(
|
||||
return "single_phase";
|
||||
}
|
||||
const assignedPhaseTypes = devicePhaseTypes.filter(
|
||||
(phaseType): phaseType is ElectricalPhaseType =>
|
||||
phaseType === "single_phase" || phaseType === "three_phase"
|
||||
isElectricalPhaseType
|
||||
);
|
||||
return assignedPhaseTypes.length > 0 &&
|
||||
assignedPhaseTypes.every((phaseType) => phaseType === "three_phase")
|
||||
|
||||
Reference in New Issue
Block a user