From: Jérôme Benoit Date: Wed, 7 Jun 2023 09:51:25 +0000 (+0200) Subject: refactor: factor out charging station events waiter X-Git-Tag: v1.2.16~3 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=b1f1b0f6ea0a9a5da396e20083071857e40c538c;p=e-mobility-charging-stations-simulator.git refactor: factor out charging station events waiter Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index df0bb8e9..b59d925f 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -7,6 +7,7 @@ import { type Worker, isMainThread } from 'node:worker_threads'; import chalk from 'chalk'; +import { ChargingStationUtils } from './ChargingStationUtils'; import type { AbstractUIServer } from './ui-server/AbstractUIServer'; import { UIServerFactory } from './ui-server/UIServerFactory'; import packageJson from '../../package.json' assert { type: 'json' }; @@ -161,7 +162,11 @@ export class Bootstrap extends EventEmitter { Constants.EMPTY_FREEZED_OBJECT ) ); - await this.waitForChargingStationsStopped(); + await ChargingStationUtils.waitForChargingStationEvents( + this, + ChargingStationWorkerMessageEvents.stopped, + this.numberOfChargingStations + ); await this.workerImplementation?.stop(); this.workerImplementation = null; this.uiServer?.stop(); @@ -338,23 +343,6 @@ export class Bootstrap extends EventEmitter { }); }; - private waitForChargingStationsStopped = async ( - stoppedEventsToWait = this.numberOfStartedChargingStations - ): Promise => { - return new Promise((resolve) => { - let stoppedEvents = 0; - if (stoppedEventsToWait === 0) { - resolve(stoppedEvents); - } - this.on(ChargingStationWorkerMessageEvents.stopped, () => { - ++stoppedEvents; - if (stoppedEvents === stoppedEventsToWait) { - resolve(stoppedEvents); - } - }); - }); - }; - private logPrefix = (): string => { return Utils.logPrefix(' Bootstrap |'); }; diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index f86c5d78..033fc4b3 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -1,4 +1,5 @@ import crypto from 'node:crypto'; +import type EventEmitter from 'node:events'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; @@ -18,6 +19,7 @@ import { type ChargingSchedulePeriod, type ChargingStationInfo, type ChargingStationTemplate, + ChargingStationWorkerMessageEvents, ConnectorPhaseRotation, type ConnectorStatus, ConnectorStatusEnum, @@ -539,6 +541,25 @@ export class ChargingStationUtils { ); } + public static waitForChargingStationEvents = async ( + emitter: EventEmitter, + event: ChargingStationWorkerMessageEvents, + eventsToWait: number + ): Promise => { + return new Promise((resolve) => { + let events = 0; + if (eventsToWait === 0) { + resolve(events); + } + emitter.on(event, () => { + ++events; + if (events === eventsToWait) { + resolve(events); + } + }); + }); + }; + private static getConfiguredNumberOfConnectors(stationTemplate: ChargingStationTemplate): number { let configuredMaxConnectors: number; if (Utils.isNotEmptyArray(stationTemplate.numberOfConnectors) === true) { diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 21da0052..fc675f40 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -39,7 +39,6 @@ import { type OCPP16BootNotificationRequest, type OCPP16BootNotificationResponse, type OCPP16CancelReservationRequest, - type OCPP16CancelReservationResponse, OCPP16ChargePointErrorCode, OCPP16ChargePointStatus, type OCPP16ChargingProfile, @@ -1601,7 +1600,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { private async handleRequestCancelReservation( chargingStation: ChargingStation, commandPayload: OCPP16CancelReservationRequest - ): Promise { + ): Promise { if ( !OCPP16ServiceUtils.checkFeatureProfile( chargingStation, diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index fcdcd1a1..5d87f633 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -25,7 +25,6 @@ import { type OCPP16AuthorizeRequest, type OCPP16AuthorizeResponse, type OCPP16BootNotificationResponse, - type OCPP16CancelReservationResponse, OCPP16ChargePointStatus, type OCPP16DataTransferResponse, type OCPP16DiagnosticsStatusNotificationResponse, @@ -295,7 +294,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ], [ OCPP16IncomingRequestCommand.CANCEL_RESERVATION, - OCPP16ServiceUtils.parseJsonSchemaFile( + OCPP16ServiceUtils.parseJsonSchemaFile( 'assets/json-schemas/ocpp/1.6/CancelReservationResponse.json', moduleName, 'constructor' diff --git a/src/types/index.ts b/src/types/index.ts index 7bfa2150..2b833800 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -130,7 +130,6 @@ export { type SetChargingProfileResponse, type UnlockConnectorResponse, type OCPP16ReserveNowResponse, - type OCPP16CancelReservationResponse, } from './ocpp/1.6/Responses'; export { ChargePointErrorCode } from './ocpp/ChargePointErrorCode'; export { diff --git a/src/types/ocpp/1.6/Responses.ts b/src/types/ocpp/1.6/Responses.ts index 1a163aa8..9cd96b24 100644 --- a/src/types/ocpp/1.6/Responses.ts +++ b/src/types/ocpp/1.6/Responses.ts @@ -110,10 +110,6 @@ export interface OCPP16DataTransferResponse extends JsonObject { data?: string; } -export interface OCPP16CancelReservationResponse extends JsonObject { - status: GenericStatus; -} - export enum OCPP16ReservationStatus { ACCEPTED = 'Accepted', FAULTED = 'Faulted', diff --git a/src/types/ocpp/Responses.ts b/src/types/ocpp/Responses.ts index 8735dc8a..94dcd07c 100644 --- a/src/types/ocpp/Responses.ts +++ b/src/types/ocpp/Responses.ts @@ -2,7 +2,6 @@ import type { OCPP16MeterValuesResponse } from './1.6/MeterValues'; import { OCPP16AvailabilityStatus, type OCPP16BootNotificationResponse, - type OCPP16CancelReservationResponse, OCPP16ChargingProfileStatus, OCPP16ClearChargingProfileStatus, OCPP16ConfigurationStatus, @@ -111,8 +110,9 @@ export const ReservationStatus = { ...OCPP16ReservationStatus, }; +export type CancelReservationStatus = GenericStatus; export const CancelReservationStatus = { ...GenericStatus, }; -export type CancelReservationResponse = OCPP16CancelReservationResponse; +export type CancelReservationResponse = GenericResponse;