]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/commitdiff
refactor(ocpp20): extract named constants for hardcoded sleep/timeout values
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 15 Mar 2026 22:24:50 +0000 (23:24 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 15 Mar 2026 22:24:50 +0000 (23:24 +0100)
Replace 8 magic numbers with semantic constants:
- FIRMWARE_STATUS_DELAY_MS, FIRMWARE_INSTALL_DELAY_MS,
  FIRMWARE_VERIFY_DELAY_MS for firmware lifecycle simulation
- LOG_UPLOAD_STEP_DELAY_MS for log upload simulation
- RESET_DELAY_MS, RESET_IDLE_MONITOR_INTERVAL_MS for reset scheduling
- CERTIFICATE_VERIFY_DELAY_MS for auth strategy retry delay

src/charging-station/ocpp/2.0/OCPP20Constants.ts
src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts
src/charging-station/ocpp/auth/strategies/CertificateAuthStrategy.ts

index cf539d156b283049891437cb0c8d6df6b5613c9d..64d6c60790d92743bed774fc67b93fe808f02754 100644 (file)
@@ -138,12 +138,21 @@ export class OCPP20Constants extends OCPPConstants {
     // { from: OCPP20ConnectorStatusEnumType.Faulted, to: OCPP20ConnectorStatusEnumType.Faulted }
   ])
 
+  static readonly FIRMWARE_INSTALL_DELAY_MS = 5000
+  static readonly FIRMWARE_STATUS_DELAY_MS = 2000
+  static readonly FIRMWARE_VERIFY_DELAY_MS = 500
+
   /**
    * Default timeout in milliseconds for async OCPP 2.0 handler operations
    * (e.g., certificate file I/O). Prevents handlers from hanging indefinitely.
    */
   static readonly HANDLER_TIMEOUT_MS = 30_000
 
+  static readonly LOG_UPLOAD_STEP_DELAY_MS = 1000
+
+  static readonly RESET_DELAY_MS = 1000
+  static readonly RESET_IDLE_MONITOR_INTERVAL_MS = 5000
+
   /**
    * Set of MessageTriggerEnumType values that the charging station supports
    * in the TriggerMessage handler. Used for validation and capability reporting.
index e0c2046dd7b953cfd1caa9b87dd8ad837e34a5c6..a19f143e2a7ecf3aff4af446ef10bf785357efa8 100644 (file)
@@ -3107,7 +3107,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
             `${chargingStation.logPrefix()} ${moduleName}.scheduleEvseReset: EVSE ${evseId.toString()} reset completed`
           )
         }
-      }, 1000)
+      }, OCPP20Constants.RESET_DELAY_MS)
     })
   }
 
@@ -3130,7 +3130,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
       } else {
         clearInterval(monitorInterval)
       }
-    }, 5000)
+    }, OCPP20Constants.RESET_IDLE_MONITOR_INTERVAL_MS)
   }
 
   /**
@@ -3151,7 +3151,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
           )
         })
       }
-    }, 5000)
+    }, OCPP20Constants.RESET_IDLE_MONITOR_INTERVAL_MS)
   }
 
   private selectAvailableEvse (chargingStation: ChargingStation): number | undefined {
@@ -3433,7 +3433,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
       requestId
     )
 
-    await sleep(2000)
+    await sleep(OCPP20Constants.FIRMWARE_STATUS_DELAY_MS)
     if (checkAborted()) return
 
     // H9: If firmware location is empty or malformed, send DownloadFailed and stop
@@ -3457,7 +3457,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
     )
 
     if (signature != null) {
-      await sleep(500)
+      await sleep(OCPP20Constants.FIRMWARE_VERIFY_DELAY_MS)
       if (checkAborted()) return
       await this.sendFirmwareStatusNotification(
         chargingStation,
@@ -3491,7 +3491,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
       logger.debug(
         `${chargingStation.logPrefix()} ${moduleName}.simulateFirmwareUpdateLifecycle: Waiting for active transactions to end before installing (L01.FR.06)`
       )
-      await sleep(5000)
+      await sleep(OCPP20Constants.FIRMWARE_INSTALL_DELAY_MS)
     }
     if (checkAborted()) return
 
@@ -3501,7 +3501,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
       requestId
     )
 
-    await sleep(1000)
+    await sleep(OCPP20Constants.RESET_DELAY_MS)
     if (checkAborted()) return
     await this.sendFirmwareStatusNotification(
       chargingStation,
@@ -3538,7 +3538,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService {
       requestId
     )
 
-    await sleep(1000)
+    await sleep(OCPP20Constants.LOG_UPLOAD_STEP_DELAY_MS)
     await this.sendLogStatusNotification(
       chargingStation,
       UploadLogStatusEnumType.Uploaded,
index 758f531cc3b465707898e4f12a9c085480646789..650f19153b73a84c0076a513b49b5392cbe8a866 100644 (file)
@@ -11,6 +11,8 @@ import { OCPPVersion } from '../../../../types/index.js'
 import { isNotEmptyString, logger, sleep } from '../../../../utils/index.js'
 import { AuthenticationMethod, AuthorizationStatus, IdentifierType } from '../types/AuthTypes.js'
 
+const CERTIFICATE_VERIFY_DELAY_MS = 100
+
 /**
  * Certificate-based authentication strategy for OCPP 2.0+
  *
@@ -240,7 +242,7 @@ export class CertificateAuthStrategy implements AuthStrategy {
     config: AuthConfiguration
   ): Promise<boolean> {
     // Simulate validation delay
-    await sleep(100)
+    await sleep(CERTIFICATE_VERIFY_DELAY_MS)
 
     // In a real implementation, this would:
     // 1. Load trusted CA certificates from configuration