]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor: refine OCPP2 type definitions and usages
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 22 Oct 2025 10:42:34 +0000 (12:42 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 22 Oct 2025 10:42:34 +0000 (12:42 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts
src/charging-station/ocpp/2.0/OCPP20VariableManager.ts
src/types/index.ts
src/types/ocpp/2.0/Common.ts
src/types/ocpp/2.0/Variables.ts

index 36a10c2b735ef834ac41dbb23047b8eccb815f08..94935c8a261421051438e4ced852797d82355afe 100644 (file)
@@ -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,
+                },
               })
             }
           }
index 38189333e58288f4ccde4393b5d48e3103f0387c..f5a62c365c81f355b15ea5369c80afd27008d097 100644 (file)
@@ -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)
   }
 }
index bc10ab75f64d551d69de258d771a9b63e605ac77..e03605afaa0f939a11585dc8cca966e23b5905c7 100644 (file)
@@ -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'
index e66beac70e9a2e90806adbd03178e6ff646f7cfc..ac2acd396d34baae4ee13a86af5c853e62c478f9 100644 (file)
@@ -281,6 +281,6 @@ interface VariableAttributeType extends JsonObject {
 }
 
 interface VariableCharacteristicsType extends JsonObject {
-  dataType: string
+  dataType: DataEnumType
   supportsMonitoring: boolean
 }
index 8f7245f263452796513bc9c9499857b7dbe84e43..1dee871416beaea05cad68a5254edf69e21a6a5e 100644 (file)
@@ -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