From ce77ea9710defa1c9783add2b0d37543f32b10c1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 16 Mar 2026 13:27:33 +0100 Subject: [PATCH] refactor(ocpp): use union types in common code where possible - OCPPServiceUtils: use SampledValue union in generic constraints instead of inline OCPP16SampledValue | OCPP20SampledValue union; use StatusNotificationRequest union in satisfies check - BroadcastChannel: use MeterValuesRequest/Response unions instead of OCPP20-specific types in version-dispatched handleMeterValues Stack-specific types remain in version-switch branches where TypeScript requires narrowed types for array push operations. --- .../ChargingStationWorkerBroadcastChannel.ts | 8 ++--- src/charging-station/ocpp/OCPPServiceUtils.ts | 35 +++++++++---------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index de73e686..3b173a76 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -36,8 +36,6 @@ import { type OCPP20GetCertificateStatusResponse, type OCPP20LogStatusNotificationRequest, type OCPP20LogStatusNotificationResponse, - type OCPP20MeterValuesRequest, - type OCPP20MeterValuesResponse, type OCPP20NotifyCustomerInformationRequest, type OCPP20NotifyCustomerInformationResponse, type OCPP20NotifyReportRequest, @@ -467,12 +465,12 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne ): Promise { if (this.chargingStation.stationInfo?.ocppVersion === OCPPVersion.VERSION_201) { return await this.chargingStation.ocppRequestService.requestHandler< - OCPP20MeterValuesRequest, - OCPP20MeterValuesResponse + MeterValuesRequest, + MeterValuesResponse >( this.chargingStation, RequestCommand.METER_VALUES, - requestPayload as OCPP20MeterValuesRequest, + requestPayload as MeterValuesRequest, this.requestParams ) } diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 0de76ab2..f41c24af 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -41,7 +41,6 @@ import { type OCPP20ConnectorStatusEnumType, type OCPP20MeterValue, type OCPP20SampledValue, - type OCPP20StatusNotificationRequest, OCPPVersion, RequestCommand, type SampledValue, @@ -127,7 +126,7 @@ const buildStatusNotificationRequest = ( connectorStatus: status as OCPP20ConnectorStatusEnumType, evseId: resolvedEvseId, timestamp: new Date(), - } satisfies OCPP20StatusNotificationRequest + } satisfies StatusNotificationRequest } default: throw new OCPPError( @@ -526,7 +525,7 @@ const buildVoltageMeasurandValue = ( } } -const addMainVoltageToMeterValue = ( +const addMainVoltageToMeterValue = ( chargingStation: ChargingStation, meterValue: { sampledValue: TSampledValue[] }, voltageData: { template: SampledValueTemplate; value: number }, @@ -551,7 +550,7 @@ const addMainVoltageToMeterValue = ( +const addPhaseVoltageToMeterValue = ( chargingStation: ChargingStation, connectorId: number, meterValue: { sampledValue: TSampledValue[] }, @@ -600,21 +599,19 @@ const addPhaseVoltageToMeterValue = ( - chargingStation: ChargingStation, - connectorId: number, - meterValue: { sampledValue: TSampledValue[] }, - mainVoltageData: { template: SampledValueTemplate; value: number }, - phase: number, - buildVersionedSampledValue: ( - sampledValueTemplate: SampledValueTemplate, - value: number, - context?: MeterValueContext, - phase?: MeterValuePhase - ) => TSampledValue - ): void => { +const addLineToLineVoltageToMeterValue = ( + chargingStation: ChargingStation, + connectorId: number, + meterValue: { sampledValue: TSampledValue[] }, + mainVoltageData: { template: SampledValueTemplate; value: number }, + phase: number, + buildVersionedSampledValue: ( + sampledValueTemplate: SampledValueTemplate, + value: number, + context?: MeterValueContext, + phase?: MeterValuePhase + ) => TSampledValue +): void => { const stationInfo = chargingStation.stationInfo if (stationInfo?.phaseLineToLineVoltageMeterValues !== true) { return -- 2.53.0