fix: ensure remote start transaction triggers a delayed start transaction
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 29 Jan 2024 15:27:01 +0000 (16:27 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 29 Jan 2024 15:27:01 +0000 (16:27 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/OCPPIncomingRequestService.ts

index 9e0fed9d087a1ac26c89f52c7348bb8259c6fc84..9b3748d3304dc9cd591075862c40b064062bdf54 100644 (file)
@@ -404,6 +404,41 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
           .bind(this)
       ]
     ])
+    this.on(
+      OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
+      (chargingStation: ChargingStation, connectorId: number, idTag: string) => {
+        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+        chargingStation.getConnectorStatus(connectorId)!.transactionRemoteStarted = true
+        chargingStation.ocppRequestService
+          .requestHandler<OCPP16StartTransactionRequest, OCPP16StartTransactionResponse>(
+          chargingStation,
+          OCPP16RequestCommand.START_TRANSACTION,
+          {
+            connectorId,
+            idTag
+          }
+        )
+          .then(response => {
+            if (response.status === OCPP16AuthorizationStatus.ACCEPTED) {
+              logger.debug(`
+        ${chargingStation.logPrefix()} Remote start transaction ACCEPTED on ${
+          chargingStation.stationInfo?.chargingStationId
+        }#${connectorId} for idTag '${idTag}'`)
+            } else {
+              logger.debug(`
+        ${chargingStation.logPrefix()} Remote start transaction REJECTED on ${
+          chargingStation.stationInfo?.chargingStationId
+        }#${connectorId} for idTag '${idTag}'`)
+            }
+          })
+          .catch(error => {
+            logger.error(
+              `${chargingStation.logPrefix()} ${moduleName}.constructor: Remote start transaction error:`,
+              error
+            )
+          })
+      }
+    )
     this.validatePayload = this.validatePayload.bind(this)
   }
 
@@ -1011,30 +1046,13 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         idTag
       )
     }
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    chargingStation.getConnectorStatus(transactionConnectorId)!.transactionRemoteStarted = true
-    if (
-      (
-        await chargingStation.ocppRequestService.requestHandler<
-        OCPP16StartTransactionRequest,
-        OCPP16StartTransactionResponse
-        >(chargingStation, OCPP16RequestCommand.START_TRANSACTION, {
-          connectorId: transactionConnectorId,
-          idTag
-        })
-      ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED
-    ) {
-      logger.debug(`
-      ${chargingStation.logPrefix()} Transaction remotely STARTED on ${
-        chargingStation.stationInfo?.chargingStationId
-      }#${transactionConnectorId} for idTag '${idTag}'`)
-      return OCPP16Constants.OCPP_RESPONSE_ACCEPTED
-    }
-    return await this.notifyRemoteStartTransactionRejected(
+    this.emit(
+      OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
       chargingStation,
       transactionConnectorId,
       idTag
     )
+    return OCPP16Constants.OCPP_RESPONSE_ACCEPTED
   }
 
   private async notifyRemoteStartTransactionRejected (
@@ -1051,7 +1069,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       )
     }
     logger.warn(
-      `${chargingStation.logPrefix()} Remote starting transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'`
+      `${chargingStation.logPrefix()} Remote start transaction REJECTED on connector id ${connectorId}, idTag '${idTag}', availability '${connectorStatus?.availability}', status '${connectorStatus?.status}'`
     )
     return OCPP16Constants.OCPP_RESPONSE_REJECTED
   }
index 7203569749e04d8ee88a238e4ac2f0ea754382da..49128ccbd1b74a9440d76ac998bd849f539ac723 100644 (file)
@@ -1,3 +1,5 @@
+import { EventEmitter } from 'node:events'
+
 import _Ajv, { type ValidateFunction } from 'ajv'
 import _ajvFormats from 'ajv-formats'
 
@@ -20,7 +22,7 @@ const ajvFormats = _ajvFormats.default
 
 const moduleName = 'OCPPIncomingRequestService'
 
-export abstract class OCPPIncomingRequestService {
+export abstract class OCPPIncomingRequestService extends EventEmitter {
   private static instance: OCPPIncomingRequestService | null = null
   private readonly version: OCPPVersion
   protected readonly ajv: Ajv
@@ -30,6 +32,7 @@ export abstract class OCPPIncomingRequestService {
   >
 
   protected constructor (version: OCPPVersion) {
+    super()
     this.version = version
     this.ajv = new Ajv({
       keywords: ['javaType'],