refactor: add helper to test connector
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 28 Apr 2023 14:16:03 +0000 (16:16 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 28 Apr 2023 14:16:03 +0000 (16:16 +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

index 22d554d1341dbcbe8bf5ec410e5a6071acd0dbd0..b28c520ba17e0a4704e8b7dfa376f998ec988ced 100644 (file)
@@ -46,7 +46,6 @@ import {
   type ChargingStationInfo,
   type ChargingStationOcppConfiguration,
   type ChargingStationTemplate,
-  ConnectorPhaseRotation,
   type ConnectorStatus,
   ConnectorStatusEnum,
   CurrentType,
@@ -152,6 +151,10 @@ export class ChargingStation {
     this.initialize();
   }
 
+  public get hasEvses(): boolean {
+    return this.connectors.size === 0 && this.evses.size > 0;
+  }
+
   private get wsConnectionUrl(): URL {
     return new URL(
       `${
@@ -166,10 +169,6 @@ export class ChargingStation {
     );
   }
 
-  private get hasEvses(): boolean {
-    return this.connectors.size === 0 && this.evses.size > 0;
-  }
-
   public logPrefix = (): string => {
     return Utils.logPrefix(
       ` ${
@@ -245,6 +244,18 @@ export class ChargingStation {
     return this.getConnectorStatus(0)?.availability === AvailabilityType.Operative;
   }
 
+  public hasConnector(connectorId: number): boolean {
+    if (this.hasEvses) {
+      for (const evseStatus of this.evses.values()) {
+        if (evseStatus.connectors.has(connectorId)) {
+          return true;
+        }
+      }
+      return false;
+    }
+    return this.connectors.has(connectorId);
+  }
+
   public isConnectorAvailable(connectorId: number): boolean {
     return (
       connectorId > 0 &&
index 642cb18cc4a82824ffa67cc451040bef55eca2df..3cb96e192c2ddbb0e56280379911eb7af32e2af3 100644 (file)
@@ -394,7 +394,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     commandPayload: UnlockConnectorRequest
   ): Promise<UnlockConnectorResponse> {
     const connectorId = commandPayload.connectorId;
-    if (chargingStation.connectors.has(connectorId) === false) {
+    if (chargingStation.hasConnector(connectorId) === false) {
       logger.error(
         `${chargingStation.logPrefix()} Trying to unlock a non existing connector id ${connectorId.toString()}`
       );
@@ -541,7 +541,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     ) {
       return OCPP16Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED;
     }
-    if (chargingStation.connectors.has(commandPayload.connectorId) === false) {
+    if (chargingStation.hasConnector(commandPayload.connectorId) === false) {
       logger.error(
         `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector id ${
           commandPayload.connectorId
@@ -597,7 +597,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     ) {
       return OCPP16Constants.OCPP_RESPONSE_REJECTED;
     }
-    if (chargingStation.connectors.has(commandPayload.connectorId) === false) {
+    if (chargingStation.hasConnector(commandPayload.connectorId) === false) {
       logger.error(
         `${chargingStation.logPrefix()} Trying to get composite schedule to a non existing connector id ${
           commandPayload.connectorId
@@ -647,7 +647,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     ) {
       return OCPP16Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
     }
-    if (chargingStation.connectors.has(commandPayload.connectorId) === false) {
+    if (chargingStation.hasConnector(commandPayload.connectorId) === false) {
       logger.error(
         `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector id ${
           commandPayload.connectorId
@@ -722,7 +722,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     commandPayload: ChangeAvailabilityRequest
   ): Promise<ChangeAvailabilityResponse> {
     const connectorId: number = commandPayload.connectorId;
-    if (chargingStation.connectors.has(connectorId) === false) {
+    if (chargingStation.hasConnector(connectorId) === false) {
       logger.error(
         `${chargingStation.logPrefix()} Trying to change the availability of a non existing connector id ${connectorId.toString()}`
       );
@@ -775,7 +775,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     commandPayload: RemoteStartTransactionRequest
   ): Promise<GenericResponse> {
     const transactionConnectorId = commandPayload.connectorId;
-    if (chargingStation.connectors.has(transactionConnectorId) === true) {
+    if (chargingStation.hasConnector(transactionConnectorId) === true) {
       const remoteStartTransactionLogMsg = `${chargingStation.logPrefix()} Transaction remotely STARTED on ${
         chargingStation.stationInfo.chargingStationId
       }#${transactionConnectorId.toString()} for idTag '${commandPayload.idTag}'`;
index 743455ff8ae783d86611b06272ddba8c7b9e8dee..5cc6cbe7312ad701699bb1beb1087352e4191126 100644 (file)
@@ -440,7 +440,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
     requestPayload: OCPP16StartTransactionRequest
   ): Promise<void> {
     const connectorId = requestPayload.connectorId;
-    if (connectorId === 0 || !chargingStation.connectors.has(connectorId)) {
+    if (connectorId === 0 || chargingStation.hasConnector(connectorId) === false) {
       logger.error(
         `${chargingStation.logPrefix()} Trying to start a transaction on a non existing connector id ${connectorId.toString()}`
       );