From: Jérôme Benoit Date: Sun, 15 Mar 2026 22:24:50 +0000 (+0100) Subject: refactor(ocpp20): extract named constants for hardcoded sleep/timeout values X-Git-Tag: ocpp-server@v3.1.0~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=adfe8d486791632d1e03b9ff58be02ba4fa4c9da;p=e-mobility-charging-stations-simulator.git refactor(ocpp20): extract named constants for hardcoded sleep/timeout values 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 --- diff --git a/src/charging-station/ocpp/2.0/OCPP20Constants.ts b/src/charging-station/ocpp/2.0/OCPP20Constants.ts index cf539d15..64d6c607 100644 --- a/src/charging-station/ocpp/2.0/OCPP20Constants.ts +++ b/src/charging-station/ocpp/2.0/OCPP20Constants.ts @@ -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. diff --git a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts index e0c2046d..a19f143e 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -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, diff --git a/src/charging-station/ocpp/auth/strategies/CertificateAuthStrategy.ts b/src/charging-station/ocpp/auth/strategies/CertificateAuthStrategy.ts index 758f531c..650f1915 100644 --- a/src/charging-station/ocpp/auth/strategies/CertificateAuthStrategy.ts +++ b/src/charging-station/ocpp/auth/strategies/CertificateAuthStrategy.ts @@ -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 { // 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