Convert sendStopTransaction to OCPP message sending handler
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 8 Mar 2022 09:32:15 +0000 (10:32 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 8 Mar 2022 09:32:15 +0000 (10:32 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16RequestService.ts
src/charging-station/ocpp/OCPPRequestService.ts

index d76ff970a2445fdb0c0039e35175459e5f4641b5..a916000f1634d73d27d2c01e8b1a61d27165ab78 100644 (file)
@@ -10,6 +10,7 @@ import {
 
 import type ChargingStation from './ChargingStation';
 import Constants from '../utils/Constants';
+import { OCPP16ServiceUtils } from './ocpp/1.6/OCPP16ServiceUtils';
 import PerformanceStatistics from '../performance/PerformanceStatistics';
 import { RequestCommand } from '../types/ocpp/Requests';
 import { Status } from '../types/AutomaticTransactionGenerator';
@@ -329,12 +330,33 @@ export default class AutomaticTransactionGenerator {
     let stopResponse: StopTransactionResponse;
     if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) {
       transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId;
-      stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction(
-        transactionId,
-        this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
-        this.chargingStation.getTransactionIdTag(transactionId),
-        reason
-      );
+      if (
+        this.chargingStation.getBeginEndMeterValues() &&
+        this.chargingStation.getOcppStrictCompliance() &&
+        !this.chargingStation.getOutOfOrderEndMeterValues()
+      ) {
+        // FIXME: Implement OCPP version agnostic helpers
+        const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
+          this.chargingStation,
+          connectorId,
+          this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId)
+        );
+        await this.chargingStation.ocppRequestService.sendTransactionEndMeterValues(
+          connectorId,
+          transactionId,
+          transactionEndMeterValue
+        );
+      }
+      stopResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler(
+        RequestCommand.STOP_TRANSACTION,
+        {
+          transactionId,
+          meterStop:
+            this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
+          idTag: this.chargingStation.getTransactionIdTag(transactionId),
+          reason,
+        }
+      )) as StopTransactionResponse;
       this.connectorsStatus.get(connectorId).stopTransactionRequests++;
     } else {
       logger.warn(
index 539ab309e62276e5db999fcdd19988f505afded9..e90bd60be551901839c5963fe5938d5fb14eef82 100644 (file)
@@ -44,6 +44,7 @@ import { MessageType } from '../types/ocpp/MessageType';
 import OCPP16IncomingRequestService from './ocpp/1.6/OCPP16IncomingRequestService';
 import OCPP16RequestService from './ocpp/1.6/OCPP16RequestService';
 import OCPP16ResponseService from './ocpp/1.6/OCPP16ResponseService';
+import { OCPP16ServiceUtils } from './ocpp/1.6/OCPP16ServiceUtils';
 import OCPPError from '../exception/OCPPError';
 import OCPPIncomingRequestService from './ocpp/OCPPIncomingRequestService';
 import OCPPRequestService from './ocpp/OCPPRequestService';
@@ -1337,12 +1338,29 @@ export default class ChargingStation {
       for (const connectorId of this.connectors.keys()) {
         if (connectorId > 0 && this.getConnectorStatus(connectorId)?.transactionStarted) {
           const transactionId = this.getConnectorStatus(connectorId).transactionId;
-          await this.ocppRequestService.sendStopTransaction(
+          if (
+            this.getBeginEndMeterValues() &&
+            this.getOcppStrictCompliance() &&
+            !this.getOutOfOrderEndMeterValues()
+          ) {
+            // FIXME: Implement OCPP version agnostic helpers
+            const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
+              this,
+              connectorId,
+              this.getEnergyActiveImportRegisterByTransactionId(transactionId)
+            );
+            await this.ocppRequestService.sendTransactionEndMeterValues(
+              connectorId,
+              transactionId,
+              transactionEndMeterValue
+            );
+          }
+          await this.ocppRequestService.sendMessageHandler(RequestCommand.STOP_TRANSACTION, {
             transactionId,
-            this.getEnergyActiveImportRegisterByTransactionId(transactionId),
-            this.getTransactionIdTag(transactionId),
-            reason
-          );
+            meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId),
+            idTag: this.getTransactionIdTag(transactionId),
+            reason,
+          });
         }
       }
     }
index 6aae66f6a01ae8ddce12660ee1b086f6e1bae63f..b791f713ff954eb065534a5f10e561c9a8a24d3a 100644 (file)
@@ -37,6 +37,7 @@ import {
   OCPP16AuthorizeResponse,
   OCPP16StartTransactionResponse,
   OCPP16StopTransactionReason,
+  OCPP16StopTransactionResponse,
 } from '../../../types/ocpp/1.6/Transaction';
 
 import type ChargingStation from '../../ChargingStation';
@@ -48,6 +49,7 @@ import { JsonType } from '../../../types/JsonType';
 import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode';
 import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
 import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus';
+import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
 import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
 import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration';
 import OCPPError from '../../../exception/OCPPError';
@@ -204,12 +206,33 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     }
     if (this.chargingStation.getConnectorStatus(connectorId)?.transactionStarted) {
       const transactionId = this.chargingStation.getConnectorStatus(connectorId).transactionId;
-      const stopResponse = await this.chargingStation.ocppRequestService.sendStopTransaction(
-        transactionId,
-        this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
-        this.chargingStation.getTransactionIdTag(transactionId),
-        OCPP16StopTransactionReason.UNLOCK_COMMAND
-      );
+      if (
+        this.chargingStation.getBeginEndMeterValues() &&
+        this.chargingStation.getOcppStrictCompliance() &&
+        !this.chargingStation.getOutOfOrderEndMeterValues()
+      ) {
+        // FIXME: Implement OCPP version agnostic helpers
+        const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
+          this.chargingStation,
+          connectorId,
+          this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId)
+        );
+        await this.chargingStation.ocppRequestService.sendTransactionEndMeterValues(
+          connectorId,
+          transactionId,
+          transactionEndMeterValue
+        );
+      }
+      const stopResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler(
+        OCPP16RequestCommand.STOP_TRANSACTION,
+        {
+          transactionId,
+          meterStop:
+            this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
+          idTag: this.chargingStation.getTransactionIdTag(transactionId),
+          reason: OCPP16StopTransactionReason.UNLOCK_COMMAND,
+        }
+      )) as OCPP16StopTransactionResponse;
       if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) {
         return Constants.OCPP_RESPONSE_UNLOCKED;
       }
@@ -726,10 +749,31 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         );
         this.chargingStation.getConnectorStatus(connectorId).status =
           OCPP16ChargePointStatus.FINISHING;
-        await this.chargingStation.ocppRequestService.sendStopTransaction(
-          transactionId,
-          this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
-          this.chargingStation.getTransactionIdTag(transactionId)
+        if (
+          this.chargingStation.getBeginEndMeterValues() &&
+          this.chargingStation.getOcppStrictCompliance() &&
+          !this.chargingStation.getOutOfOrderEndMeterValues()
+        ) {
+          // FIXME: Implement OCPP version agnostic helpers
+          const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
+            this.chargingStation,
+            connectorId,
+            this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId)
+          );
+          await this.chargingStation.ocppRequestService.sendTransactionEndMeterValues(
+            connectorId,
+            transactionId,
+            transactionEndMeterValue
+          );
+        }
+        await this.chargingStation.ocppRequestService.sendMessageHandler(
+          OCPP16RequestCommand.STOP_TRANSACTION,
+          {
+            transactionId,
+            meterStop:
+              this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId),
+            idTag: this.chargingStation.getTransactionIdTag(transactionId),
+          }
         );
         return Constants.OCPP_RESPONSE_ACCEPTED;
       }
index ca024649fc4582bcc00eb35060b16423fb40d685..7ef652f01383e5ddf1a484021bbc062a8e6d5ee3 100644 (file)
@@ -2,9 +2,6 @@
 
 import {
   AuthorizeRequest,
-  OCPP16StartTransactionResponse,
-  OCPP16StopTransactionReason,
-  OCPP16StopTransactionResponse,
   StartTransactionRequest,
   StopTransactionRequest,
 } from '../../../types/ocpp/1.6/Transaction';
@@ -60,53 +57,6 @@ export default class OCPP16RequestService extends OCPPRequestService {
     );
   }
 
-  public async sendStopTransaction(
-    transactionId: number,
-    meterStop: number,
-    idTag?: string,
-    reason: OCPP16StopTransactionReason = OCPP16StopTransactionReason.NONE
-  ): Promise<OCPP16StopTransactionResponse> {
-    let connectorId: number;
-    for (const id of this.chargingStation.connectors.keys()) {
-      if (id > 0 && this.chargingStation.getConnectorStatus(id)?.transactionId === transactionId) {
-        connectorId = id;
-        break;
-      }
-    }
-    const transactionEndMeterValue = OCPP16ServiceUtils.buildTransactionEndMeterValue(
-      this.chargingStation,
-      connectorId,
-      meterStop
-    );
-    // FIXME: should be a callback, each OCPP commands implementation must do only one job
-    this.chargingStation.getBeginEndMeterValues() &&
-      this.chargingStation.getOcppStrictCompliance() &&
-      !this.chargingStation.getOutOfOrderEndMeterValues() &&
-      (await this.sendTransactionEndMeterValues(
-        connectorId,
-        transactionId,
-        transactionEndMeterValue
-      ));
-    const payload: StopTransactionRequest = {
-      transactionId,
-      ...(!Utils.isUndefined(idTag) && { idTag }),
-      meterStop,
-      timestamp: new Date().toISOString(),
-      ...(reason && { reason }),
-      ...(this.chargingStation.getTransactionDataMeterValues() && {
-        transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues(
-          this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue,
-          transactionEndMeterValue
-        ),
-      }),
-    };
-    return (await this.sendMessage(
-      Utils.generateUUID(),
-      payload,
-      OCPP16RequestCommand.STOP_TRANSACTION
-    )) as OCPP16StartTransactionResponse;
-  }
-
   public async sendMeterValues(
     connectorId: number,
     transactionId: number,
@@ -169,6 +119,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
     commandName: OCPP16RequestCommand,
     commandParams?: JsonType
   ): JsonType {
+    let connectorId: number;
     switch (commandName) {
       case OCPP16RequestCommand.AUTHORIZE:
         return {
@@ -230,6 +181,16 @@ export default class OCPP16RequestService extends OCPPRequestService {
           timestamp: new Date().toISOString(),
         } as StartTransactionRequest;
       case OCPP16RequestCommand.STOP_TRANSACTION:
+        for (const id of this.chargingStation.connectors.keys()) {
+          if (
+            id > 0 &&
+            this.chargingStation.getConnectorStatus(id)?.transactionId ===
+              commandParams?.transactionId
+          ) {
+            connectorId = id;
+            break;
+          }
+        }
         return {
           transactionId: commandParams?.transactionId,
           ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }),
@@ -238,11 +199,10 @@ export default class OCPP16RequestService extends OCPPRequestService {
           ...(commandParams?.reason && { reason: commandParams.reason }),
           ...(this.chargingStation.getTransactionDataMeterValues() && {
             transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues(
-              this.chargingStation.getConnectorStatus(commandParams?.connectorId as number)
-                .transactionBeginMeterValue,
+              this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue,
               OCPP16ServiceUtils.buildTransactionEndMeterValue(
                 this.chargingStation,
-                commandParams?.connectorId as number,
+                connectorId,
                 commandParams?.meterStop as number
               )
             ),
index f4a1d1699b9649c6fdf02159a359d4f186c1834b..4fe2c14155dde77ff6cd88a38f2170cad9a69307 100644 (file)
@@ -325,13 +325,6 @@ export default abstract class OCPPRequestService {
     params?: SendParams
   ): Promise<ResponseType>;
 
-  public abstract sendStopTransaction(
-    transactionId: number,
-    meterStop: number,
-    idTag?: string,
-    reason?: StopTransactionReason
-  ): Promise<StopTransactionResponse>;
-
   public abstract sendMeterValues(
     connectorId: number,
     transactionId: number,