refactor: factor out change availability helper
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 31 Jul 2023 12:21:45 +0000 (14:21 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 31 Jul 2023 12:21:45 +0000 (14:21 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts
src/types/index.ts
src/types/ocpp/1.6/Requests.ts
src/types/ocpp/1.6/Responses.ts
src/types/ocpp/Reservation.ts

index eb73f002c90f0cbc5289a59550dece1364ce6c7a..d3b53bd22cfe7071a3c3c565b3dbf0b52d501af5 100644 (file)
@@ -104,7 +104,7 @@ import {
   RegistrationStatusEnumType,
   RequestCommand,
   type Reservation,
-  type ReservationFilterKey,
+  type ReservationKey,
   ReservationTerminationReason,
   type Response,
   StandardParametersKey,
@@ -988,7 +988,7 @@ export class ChargingStation {
   }
 
   public getReservationBy(
-    filterKey: ReservationFilterKey,
+    filterKey: ReservationKey,
     value: number | string,
   ): Reservation | undefined {
     if (this.hasEvses) {
@@ -1076,10 +1076,10 @@ export class ChargingStation {
     }
   }
 
-  private restartReservationExpiryDateSetInterval(): void {
-    this.stopReservationExpirationSetInterval();
-    this.startReservationExpirationSetInterval();
-  }
+  // private restartReservationExpiryDateSetInterval(): void {
+  //   this.stopReservationExpirationSetInterval();
+  //   this.startReservationExpirationSetInterval();
+  // }
 
   private getNumberOfReservableConnectors(): number {
     let reservableConnectors = 0;
index 356e453bde54863444e4fd4589c7432871a5f83a..72d1457ceb17c2f8e81e86dae41bce1498dbcfc3 100644 (file)
@@ -19,13 +19,10 @@ import {
 } from '../../../charging-station';
 import { OCPPError } from '../../../exception';
 import {
-  type ChangeAvailabilityRequest,
-  type ChangeAvailabilityResponse,
   type ChangeConfigurationRequest,
   type ChangeConfigurationResponse,
   type ClearChargingProfileRequest,
   type ClearChargingProfileResponse,
-  type ConnectorStatus,
   ErrorType,
   type GenericResponse,
   GenericStatus,
@@ -41,6 +38,8 @@ import {
   type OCPP16BootNotificationRequest,
   type OCPP16BootNotificationResponse,
   type OCPP16CancelReservationRequest,
+  type OCPP16ChangeAvailabilityRequest,
+  type OCPP16ChangeAvailabilityResponse,
   OCPP16ChargePointErrorCode,
   OCPP16ChargePointStatus,
   type OCPP16ChargingProfile,
@@ -259,7 +258,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY,
-        OCPP16ServiceUtils.parseJsonSchemaFile<ChangeAvailabilityRequest>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16ChangeAvailabilityRequest>(
           'assets/json-schemas/ocpp/1.6/ChangeAvailability.json',
           moduleName,
           'constructor',
@@ -757,8 +756,8 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
 
   private async handleRequestChangeAvailability(
     chargingStation: ChargingStation,
-    commandPayload: ChangeAvailabilityRequest,
-  ): Promise<ChangeAvailabilityResponse> {
+    commandPayload: OCPP16ChangeAvailabilityRequest,
+  ): Promise<OCPP16ChangeAvailabilityResponse> {
     const connectorId: number = commandPayload.connectorId;
     if (chargingStation.hasConnector(connectorId) === false) {
       logger.error(
@@ -772,33 +771,29 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         ? OCPP16ChargePointStatus.Available
         : OCPP16ChargePointStatus.Unavailable;
     if (connectorId === 0) {
-      let response: ChangeAvailabilityResponse =
-        OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
-      const changeAvailability = async (id: number, connectorStatus: ConnectorStatus) => {
-        if (connectorStatus?.transactionStarted === true) {
-          response = OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
-        }
-        connectorStatus.availability = commandPayload.type;
-        if (response === OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) {
-          await OCPP16ServiceUtils.sendAndSetConnectorStatus(
-            chargingStation,
-            id,
-            chargePointStatus,
-          );
-        }
-      };
+      let response: OCPP16ChangeAvailabilityResponse;
       if (chargingStation.hasEvses) {
         for (const evseStatus of chargingStation.evses.values()) {
-          for (const [id, connectorStatus] of evseStatus.connectors) {
-            await changeAvailability(id, connectorStatus);
+          for (const id of evseStatus.connectors.keys()) {
+            response = await OCPP16ServiceUtils.changeAvailability(
+              chargingStation,
+              id,
+              chargePointStatus,
+              commandPayload.type,
+            );
           }
         }
       } else {
         for (const id of chargingStation.connectors.keys()) {
-          await changeAvailability(id, chargingStation.getConnectorStatus(id)!);
+          response = await OCPP16ServiceUtils.changeAvailability(
+            chargingStation,
+            id,
+            chargePointStatus,
+            commandPayload.type,
+          );
         }
       }
-      return response;
+      return response!;
     } else if (
       connectorId > 0 &&
       (chargingStation.isChargingStationAvailable() === true ||
index 0f697f630fe079a0b0e9aa72c6c13ed2154e631c..ae8b6287bd8757e01b134cfd2e50a67167bae383 100644 (file)
@@ -14,7 +14,6 @@ import {
 } from '../../../charging-station';
 import { OCPPError } from '../../../exception';
 import {
-  type ChangeAvailabilityResponse,
   type ChangeConfigurationResponse,
   type ClearChargingProfileResponse,
   ErrorType,
@@ -27,6 +26,7 @@ import {
   type OCPP16AuthorizeRequest,
   type OCPP16AuthorizeResponse,
   type OCPP16BootNotificationResponse,
+  type OCPP16ChangeAvailabilityResponse,
   OCPP16ChargePointStatus,
   type OCPP16DataTransferResponse,
   type OCPP16DiagnosticsStatusNotificationResponse,
@@ -209,7 +209,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
       ],
       [
         OCPP16IncomingRequestCommand.CHANGE_AVAILABILITY,
-        OCPP16ServiceUtils.parseJsonSchemaFile<ChangeAvailabilityResponse>(
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16ChangeAvailabilityResponse>(
           'assets/json-schemas/ocpp/1.6/ChangeAvailabilityResponse.json',
           moduleName,
           'constructor',
index 00594e1c7ed664809d195ff412f6eab9e48a851e..cf055beb949d05f5844c86a450fd6cfc27c7f8ee 100644 (file)
@@ -2,6 +2,7 @@
 
 import type { JSONSchemaType } from 'ajv';
 
+import { OCPP16Constants } from './OCPP16Constants';
 import { type ChargingStation, hasFeatureProfile } from '../../../charging-station';
 import { OCPPError } from '../../../exception';
 import {
@@ -14,6 +15,9 @@ import {
   MeterValueContext,
   MeterValueLocation,
   MeterValueUnit,
+  OCPP16AvailabilityType,
+  type OCPP16ChangeAvailabilityResponse,
+  OCPP16ChargePointStatus,
   type OCPP16ChargingProfile,
   type OCPP16IncomingRequestCommand,
   type OCPP16MeterValue,
@@ -788,6 +792,29 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
     return meterValues;
   }
 
+  public static changeAvailability = async (
+    chargingStation: ChargingStation,
+    connectorId: number,
+    chargePointStatus: OCPP16ChargePointStatus,
+    availabilityType: OCPP16AvailabilityType,
+  ): Promise<OCPP16ChangeAvailabilityResponse> => {
+    let response: OCPP16ChangeAvailabilityResponse =
+      OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
+    const connectorStatus = chargingStation.getConnectorStatus(connectorId)!;
+    if (connectorStatus?.transactionStarted === true) {
+      response = OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
+    }
+    connectorStatus.availability = availabilityType;
+    if (response === OCPP16Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) {
+      await OCPP16ServiceUtils.sendAndSetConnectorStatus(
+        chargingStation,
+        connectorId,
+        chargePointStatus,
+      );
+    }
+    return response;
+  };
+
   public static setChargingProfile(
     chargingStation: ChargingStation,
     connectorId: number,
index 11f1138ce69a895aafe5a77d42dc6e384a005d2c..57437830a2ace05f3236da9c4d00c8d62fb52c25 100644 (file)
@@ -80,7 +80,7 @@ export {
   type MessageEvent,
 } from './WorkerBroadcastChannel';
 export {
-  type ChangeAvailabilityRequest,
+  type OCPP16ChangeAvailabilityRequest,
   type ChangeConfigurationRequest,
   type ClearChargingProfileRequest,
   type GetConfigurationRequest,
@@ -110,7 +110,7 @@ export {
   type OCPP16CancelReservationRequest,
 } from './ocpp/1.6/Requests';
 export {
-  type ChangeAvailabilityResponse,
+  type OCPP16ChangeAvailabilityResponse,
   type ChangeConfigurationResponse,
   type ClearChargingProfileResponse,
   type GetConfigurationResponse,
@@ -257,6 +257,6 @@ export {
 } from './WebSocket';
 export {
   type Reservation,
-  type ReservationFilterKey,
+  type ReservationKey,
   ReservationTerminationReason,
 } from './ocpp/Reservation';
index 39e8a9bb3269509639e70b43fb65d4e9f3496e8e..c6f041e58581e40c7d92ad81b43dccaeb16f48b0 100644 (file)
@@ -119,7 +119,7 @@ export enum OCPP16AvailabilityType {
   Operative = 'Operative',
 }
 
-export interface ChangeAvailabilityRequest extends JsonObject {
+export interface OCPP16ChangeAvailabilityRequest extends JsonObject {
   connectorId: number;
   type: OCPP16AvailabilityType;
 }
index 2e5e285b2ef7378ec2a1033b2df071bee0dfdfca..c98c03a36b13210e0b448ff68f32b1e862401291 100644 (file)
@@ -65,7 +65,7 @@ export enum OCPP16AvailabilityStatus {
   SCHEDULED = 'Scheduled',
 }
 
-export interface ChangeAvailabilityResponse extends JsonObject {
+export interface OCPP16ChangeAvailabilityResponse extends JsonObject {
   status: OCPP16AvailabilityStatus;
 }
 
index 0d79375e15b826cef5e97fd60bf6a610c2c3b9d9..cddc316995e56651087d15fadd9d217db9ea3105 100644 (file)
@@ -10,4 +10,4 @@ export enum ReservationTerminationReason {
   REPLACE_EXISTING = 'ReplaceExisting',
 }
 
-export type ReservationFilterKey = keyof OCPP16ReserveNowRequest;
+export type ReservationKey = keyof OCPP16ReserveNowRequest;