refactor: factor out remote stop transaction helper
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 31 Jul 2023 12:26:35 +0000 (14:26 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 31 Jul 2023 12:26:35 +0000 (14:26 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts

index 72d1457ceb17c2f8e81e86dae41bce1498dbcfc3..20cedf507ab556b110504b6202c670ed7551f5e5 100644 (file)
@@ -1002,27 +1002,12 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     commandPayload: RemoteStopTransactionRequest,
   ): Promise<GenericResponse> {
     const transactionId = commandPayload.transactionId;
-    const remoteStopTransaction = async (connectorId: number): Promise<GenericResponse> => {
-      await OCPP16ServiceUtils.sendAndSetConnectorStatus(
-        chargingStation,
-        connectorId,
-        OCPP16ChargePointStatus.Finishing,
-      );
-      const stopResponse = await chargingStation.stopTransactionOnConnector(
-        connectorId,
-        OCPP16StopTransactionReason.REMOTE,
-      );
-      if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
-        return OCPP16Constants.OCPP_RESPONSE_ACCEPTED;
-      }
-      return OCPP16Constants.OCPP_RESPONSE_REJECTED;
-    };
     if (chargingStation.hasEvses) {
       for (const [evseId, evseStatus] of chargingStation.evses) {
         if (evseId > 0) {
           for (const [connectorId, connectorStatus] of evseStatus.connectors) {
             if (connectorStatus.transactionId === transactionId) {
-              return remoteStopTransaction(connectorId);
+              return OCPP16ServiceUtils.remoteStopTransaction(chargingStation, connectorId);
             }
           }
         }
@@ -1033,7 +1018,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
           connectorId > 0 &&
           chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId
         ) {
-          return remoteStopTransaction(connectorId);
+          return OCPP16ServiceUtils.remoteStopTransaction(chargingStation, connectorId);
         }
       }
     }
index cf055beb949d05f5844c86a450fd6cfc27c7f8ee..e2665090ed3e44dbf6841f1519520adec91f2a4c 100644 (file)
@@ -9,12 +9,14 @@ import {
   type ClearChargingProfileRequest,
   CurrentType,
   ErrorType,
+  type GenericResponse,
   type JsonType,
   type MeasurandPerPhaseSampledValueTemplates,
   type MeasurandValues,
   MeterValueContext,
   MeterValueLocation,
   MeterValueUnit,
+  OCPP16AuthorizationStatus,
   OCPP16AvailabilityType,
   type OCPP16ChangeAvailabilityResponse,
   OCPP16ChargePointStatus,
@@ -26,6 +28,7 @@ import {
   OCPP16RequestCommand,
   type OCPP16SampledValue,
   OCPP16StandardParametersKey,
+  OCPP16StopTransactionReason,
   type OCPP16SupportedFeatureProfiles,
   OCPPVersion,
   type SampledValueTemplate,
@@ -792,6 +795,25 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
     return meterValues;
   }
 
+  public static remoteStopTransaction = async (
+    chargingStation: ChargingStation,
+    connectorId: number,
+  ): Promise<GenericResponse> => {
+    await OCPP16ServiceUtils.sendAndSetConnectorStatus(
+      chargingStation,
+      connectorId,
+      OCPP16ChargePointStatus.Finishing,
+    );
+    const stopResponse = await chargingStation.stopTransactionOnConnector(
+      connectorId,
+      OCPP16StopTransactionReason.REMOTE,
+    );
+    if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
+      return OCPP16Constants.OCPP_RESPONSE_ACCEPTED;
+    }
+    return OCPP16Constants.OCPP_RESPONSE_REJECTED;
+  };
+
   public static changeAvailability = async (
     chargingStation: ChargingStation,
     connectorId: number,