refactor: factor out charging station events waiter
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 7 Jun 2023 09:51:25 +0000 (11:51 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 7 Jun 2023 09:51:25 +0000 (11:51 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStationUtils.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/types/index.ts
src/types/ocpp/1.6/Responses.ts
src/types/ocpp/Responses.ts

index df0bb8e98cda7c85b30104ba9ed9fac37fceb610..b59d925fb233202461ba71d2d07a9d9d3c7f5c01 100644 (file)
@@ -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<number> => {
-    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 |');
   };
index f86c5d783b82657c483183f9bf43c562e2c11de1..033fc4b36f79647746106169fe462834d241e463 100644 (file)
@@ -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<number> => {
+    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) {
index 21da005239070a634ba3cb9b253c4eb1b8a769cf..fc675f401619c417969ada376083ee831d8c004c 100644 (file)
@@ -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<OCPP16CancelReservationResponse> {
+  ): Promise<GenericResponse> {
     if (
       !OCPP16ServiceUtils.checkFeatureProfile(
         chargingStation,
index fcdcd1a13489da0b1af14355f3735d669f60c78b..5d87f633e1f262004fb1232a157c5b5abd417976 100644 (file)
@@ -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<OCPP16CancelReservationResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<GenericResponse>(
           'assets/json-schemas/ocpp/1.6/CancelReservationResponse.json',
           moduleName,
           'constructor'
index 7bfa2150abfaf1c26927024b6250d617541b24db..2b833800b44800d2709eacb005130aa5f6cd1a1c 100644 (file)
@@ -130,7 +130,6 @@ export {
   type SetChargingProfileResponse,
   type UnlockConnectorResponse,
   type OCPP16ReserveNowResponse,
-  type OCPP16CancelReservationResponse,
 } from './ocpp/1.6/Responses';
 export { ChargePointErrorCode } from './ocpp/ChargePointErrorCode';
 export {
index 1a163aa82b72745c7ab42b560dae78c90f2642f5..9cd96b2473dbfccccbe7a7772b34c5f72b57184a 100644 (file)
@@ -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',
index 8735dc8ae10f2cf72362fe0ee9388a773794a9c2..94dcd07cfa7598f4aec17ac148bf89840fec0782 100644 (file)
@@ -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;