From: Jérôme Benoit Date: Wed, 22 Oct 2025 10:42:34 +0000 (+0200) Subject: refactor: refine OCPP2 type definitions and usages X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b6880bdfabdf3df05abb7c787b9be656e28e254e;p=e-mobility-charging-stations-simulator.git refactor: refine OCPP2 type definitions and usages Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts index 36a10c2b..94935c8a 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -9,6 +9,7 @@ import { AttributeEnumType, ConnectorEnumType, ConnectorStatusEnum, + DataEnumType, ErrorType, GenericDeviceModelStatusEnumType, type IncomingRequestHandler, @@ -24,6 +25,7 @@ import { type OCPP20NotifyReportRequest, type OCPP20NotifyReportResponse, OCPP20RequestCommand, + OCPP20VariableName, OCPPVersion, ReportBaseEnumType, type ReportDataType, @@ -272,7 +274,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { }, ], variableCharacteristics: { - dataType: 'string', + dataType: DataEnumType.string, supportsMonitoring: false, }, }) @@ -287,44 +289,44 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { if (stationInfo.chargePointModel) { reportData.push({ component: { name: OCPP20ComponentName.ChargingStation }, - variable: { name: 'Model' }, + variable: { name: OCPP20VariableName.Model }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: stationInfo.chargePointModel }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: false }, + variableCharacteristics: { dataType: DataEnumType.string, supportsMonitoring: false }, }) } if (stationInfo.chargePointVendor) { reportData.push({ component: { name: OCPP20ComponentName.ChargingStation }, - variable: { name: 'VendorName' }, + variable: { name: OCPP20VariableName.VendorName }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: stationInfo.chargePointVendor }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: false }, + variableCharacteristics: { dataType: DataEnumType.string, supportsMonitoring: false }, }) } if (stationInfo.chargePointSerialNumber) { reportData.push({ component: { name: OCPP20ComponentName.ChargingStation }, - variable: { name: 'SerialNumber' }, + variable: { name: OCPP20VariableName.SerialNumber }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: stationInfo.chargePointSerialNumber, }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: false }, + variableCharacteristics: { dataType: DataEnumType.string, supportsMonitoring: false }, }) } if (stationInfo.firmwareVersion) { reportData.push({ component: { name: OCPP20ComponentName.ChargingStation }, - variable: { name: 'FirmwareVersion' }, + variable: { name: OCPP20VariableName.FirmwareVersion }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: stationInfo.firmwareVersion }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: false }, + variableCharacteristics: { dataType: DataEnumType.string, supportsMonitoring: false }, }) } } @@ -349,7 +351,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { variable: { name: configKey.key }, variableAttribute: variableAttributes, variableCharacteristics: { - dataType: 'string', + dataType: DataEnumType.string, supportsMonitoring: false, }, }) @@ -364,11 +366,11 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { evse: { id: evseId }, name: OCPP20ComponentName.EVSE, }, - variable: { name: 'AvailabilityState' }, + variable: { name: OCPP20VariableName.AvailabilityState }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: evse.availability }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: true }, + variableCharacteristics: { dataType: DataEnumType.string, supportsMonitoring: true }, }) if (evse.connectors.size > 0) { for (const [connectorId, connector] of evse.connectors) { @@ -377,14 +379,17 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { evse: { connectorId, id: evseId }, name: OCPP20ComponentName.EVSE, }, - variable: { name: 'ConnectorType' }, + variable: { name: OCPP20VariableName.ConnectorType }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: connector.type ?? ConnectorEnumType.Unknown, }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: false }, + variableCharacteristics: { + dataType: DataEnumType.string, + supportsMonitoring: false, + }, }) } } @@ -398,14 +403,17 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { evse: { connectorId, id: 1 }, name: OCPP20ComponentName.Connector, }, - variable: { name: 'ConnectorType' }, + variable: { name: OCPP20VariableName.ConnectorType }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: connector.type ?? ConnectorEnumType.Unknown, }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: false }, + variableCharacteristics: { + dataType: DataEnumType.string, + supportsMonitoring: false, + }, }) } } @@ -418,38 +426,38 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { if (stationInfo.chargePointModel) { reportData.push({ component: { name: OCPP20ComponentName.ChargingStation }, - variable: { name: 'Model' }, + variable: { name: OCPP20VariableName.Model }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: stationInfo.chargePointModel }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: false }, + variableCharacteristics: { dataType: DataEnumType.string, supportsMonitoring: false }, }) } if (stationInfo.chargePointVendor) { reportData.push({ component: { name: OCPP20ComponentName.ChargingStation }, - variable: { name: 'VendorName' }, + variable: { name: OCPP20VariableName.VendorName }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: stationInfo.chargePointVendor }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: false }, + variableCharacteristics: { dataType: DataEnumType.string, supportsMonitoring: false }, }) } if (stationInfo.firmwareVersion) { reportData.push({ component: { name: OCPP20ComponentName.ChargingStation }, - variable: { name: 'FirmwareVersion' }, + variable: { name: OCPP20VariableName.FirmwareVersion }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: stationInfo.firmwareVersion }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: false }, + variableCharacteristics: { dataType: DataEnumType.string, supportsMonitoring: false }, }) } } reportData.push({ component: { name: OCPP20ComponentName.ChargingStation }, - variable: { name: 'AvailabilityState' }, + variable: { name: OCPP20VariableName.AvailabilityState }, variableAttribute: [ { type: AttributeEnumType.Actual as string, @@ -458,7 +466,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { : OCPP20ConnectorStatusEnumType.Unavailable, }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: true }, + variableCharacteristics: { dataType: DataEnumType.string, supportsMonitoring: true }, }) if (chargingStation.evses.size > 0) { @@ -468,11 +476,11 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { evse: { id: evseId }, name: OCPP20ComponentName.EVSE, }, - variable: { name: 'AvailabilityState' }, + variable: { name: OCPP20VariableName.AvailabilityState }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: evse.availability }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: true }, + variableCharacteristics: { dataType: DataEnumType.string, supportsMonitoring: true }, }) } } else if (chargingStation.connectors.size > 0) { @@ -484,14 +492,17 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { evse: { connectorId, id: 1 }, name: OCPP20ComponentName.Connector, }, - variable: { name: 'AvailabilityState' }, + variable: { name: OCPP20VariableName.AvailabilityState }, variableAttribute: [ { type: AttributeEnumType.Actual as string, value: connector.status ?? ConnectorStatusEnum.Unavailable, }, ], - variableCharacteristics: { dataType: 'string', supportsMonitoring: true }, + variableCharacteristics: { + dataType: DataEnumType.string, + supportsMonitoring: true, + }, }) } } diff --git a/src/charging-station/ocpp/2.0/OCPP20VariableManager.ts b/src/charging-station/ocpp/2.0/OCPP20VariableManager.ts index 38189333..f5a62c36 100644 --- a/src/charging-station/ocpp/2.0/OCPP20VariableManager.ts +++ b/src/charging-station/ocpp/2.0/OCPP20VariableManager.ts @@ -3,7 +3,9 @@ import { AttributeEnumType, type ComponentType, + GenericDeviceModelStatusEnumType, GetVariableStatusEnumType, + MutabilityEnumType, OCPP20ComponentName, type OCPP20GetVariableDataType, type OCPP20GetVariableResultType, @@ -20,7 +22,7 @@ import { type ChargingStation } from '../../ChargingStation.js' interface StandardVariableConfig { attributeTypes: AttributeEnumType[] defaultValue?: string - mutability: 'ReadOnly' | 'ReadWrite' | 'WriteOnly' + mutability: MutabilityEnumType persistent: boolean } @@ -69,7 +71,7 @@ export class OCPP20VariableManager { component: variableData.component, statusInfo: { additionalInfo: 'Internal error occurred while retrieving variable', - reasonCode: 'InternalError', + reasonCode: GenericDeviceModelStatusEnumType.Rejected, }, variable: variableData.variable, }) @@ -99,7 +101,7 @@ export class OCPP20VariableManager { component, statusInfo: { additionalInfo: `Component ${component.name} is not supported by this charging station`, - reasonCode: 'NotSupported', + reasonCode: GenericDeviceModelStatusEnumType.NotSupported, }, variable, } @@ -113,7 +115,7 @@ export class OCPP20VariableManager { component, statusInfo: { additionalInfo: `Variable ${variable.name} is not supported for component ${component.name}`, - reasonCode: 'NotSupported', + reasonCode: GenericDeviceModelStatusEnumType.NotSupported, }, variable, } @@ -127,7 +129,7 @@ export class OCPP20VariableManager { component, statusInfo: { additionalInfo: `Attribute type ${attributeType} is not supported for variable ${variable.name}`, - reasonCode: 'NotSupported', + reasonCode: GenericDeviceModelStatusEnumType.NotSupported, }, variable, } @@ -226,7 +228,7 @@ export class OCPP20VariableManager { { attributeTypes: [AttributeEnumType.Actual, AttributeEnumType.Target], defaultValue: '300', - mutability: 'ReadWrite', + mutability: MutabilityEnumType.ReadWrite, persistent: true, } ) @@ -236,7 +238,7 @@ export class OCPP20VariableManager { { attributeTypes: [AttributeEnumType.Actual, AttributeEnumType.Target], defaultValue: '30', - mutability: 'ReadWrite', + mutability: MutabilityEnumType.ReadWrite, persistent: true, } ) @@ -336,8 +338,6 @@ export class OCPP20VariableManager { ...Object.values(OCPP20RequiredVariableName), ] - return knownVariables.includes( - variable.name as OCPP20OptionalVariableName | OCPP20RequiredVariableName - ) + return knownVariables.includes(variable.name) } } diff --git a/src/types/index.ts b/src/types/index.ts index bc10ab75..e03605af 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -145,6 +145,7 @@ export { BootReasonEnumType, type ComponentType, type CustomDataType, + DataEnumType, GenericDeviceModelStatusEnumType, OCPP20ComponentName, OCPP20ConnectorStatusEnumType, @@ -174,10 +175,12 @@ export type { export { AttributeEnumType, GetVariableStatusEnumType, + MutabilityEnumType, type OCPP20GetVariableDataType, type OCPP20GetVariableResultType, OCPP20OptionalVariableName, OCPP20RequiredVariableName, + OCPP20VariableName, type VariableType, } from './ocpp/2.0/Variables.js' export { ChargePointErrorCode } from './ocpp/ChargePointErrorCode.js' diff --git a/src/types/ocpp/2.0/Common.ts b/src/types/ocpp/2.0/Common.ts index e66beac7..ac2acd39 100644 --- a/src/types/ocpp/2.0/Common.ts +++ b/src/types/ocpp/2.0/Common.ts @@ -281,6 +281,6 @@ interface VariableAttributeType extends JsonObject { } interface VariableCharacteristicsType extends JsonObject { - dataType: string + dataType: DataEnumType supportsMonitoring: boolean } diff --git a/src/types/ocpp/2.0/Variables.ts b/src/types/ocpp/2.0/Variables.ts index 8f7245f2..1dee8714 100644 --- a/src/types/ocpp/2.0/Variables.ts +++ b/src/types/ocpp/2.0/Variables.ts @@ -16,6 +16,12 @@ export enum GetVariableStatusEnumType { UnknownVariable = 'UnknownVariable', } +export enum MutabilityEnumType { + ReadOnly = 'ReadOnly', + ReadWrite = 'ReadWrite', + WriteOnly = 'WriteOnly', +} + export enum OCPP20OptionalVariableName { HeartbeatInterval = 'HeartbeatInterval', WebSocketPingInterval = 'WebSocketPingInterval', @@ -52,6 +58,15 @@ export enum OCPP20RequiredVariableName { UnlockOnEVSideDisconnect = 'UnlockOnEVSideDisconnect', } +export enum OCPP20VariableName { + AvailabilityState = 'AvailabilityState', + ConnectorType = 'ConnectorType', + FirmwareVersion = 'FirmwareVersion', + Model = 'Model', + SerialNumber = 'SerialNumber', + VendorName = 'VendorName', +} + export enum OCPP20VendorVariableName { ConnectionUrl = 'ConnectionUrl', } @@ -108,5 +123,6 @@ export interface VariableType extends JsonObject { type VariableName = | OCPP20OptionalVariableName | OCPP20RequiredVariableName + | OCPP20VariableName | OCPP20VendorVariableName | string