fix: ensure the second stage at handling incoming request is executed
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index 12431266172c3d9c468c9fde322ff860991bfe00..53f87ab2667e089824c84632654dbbc413444492 100644 (file)
@@ -83,6 +83,7 @@ import {
   OCPP16SupportedFeatureProfiles,
   type OCPP16TriggerMessageRequest,
   type OCPP16TriggerMessageResponse,
+  OCPP16TriggerMessageStatus,
   type OCPP16UpdateFirmwareRequest,
   type OCPP16UpdateFirmwareResponse,
   type OCPPConfigurationKey,
@@ -406,35 +407,148 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     ])
     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}'`
+      (
+        chargingStation: ChargingStation,
+        request: RemoteStartTransactionRequest,
+        response: GenericResponse
+      ) => {
+        if (response.status === GenericStatus.Accepted) {
+          const { connectorId, idTag } = request
+          // 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
               )
-            } else {
-              logger.debug(
-                `${chargingStation.logPrefix()} Remote start transaction REJECTED on ${chargingStation.stationInfo?.chargingStationId}#${connectorId} for idTag '${idTag}'`
+            })
+        }
+      }
+    )
+    this.on(
+      OCPP16IncomingRequestCommand.TRIGGER_MESSAGE,
+      (
+        chargingStation: ChargingStation,
+        request: OCPP16TriggerMessageRequest,
+        response: OCPP16TriggerMessageResponse
+      ) => {
+        if (response.status !== OCPP16TriggerMessageStatus.ACCEPTED) {
+          return
+        }
+        const { requestedMessage, connectorId } = request
+        const errorHandler = (error: Error): void => {
+          logger.error(
+            `${chargingStation.logPrefix()} ${moduleName}.constructor: Trigger ${requestedMessage} error:`,
+            error
+          )
+        }
+        switch (requestedMessage) {
+          case OCPP16MessageTrigger.BootNotification:
+            chargingStation.ocppRequestService
+              .requestHandler<OCPP16BootNotificationRequest, OCPP16BootNotificationResponse>(
+              chargingStation,
+              OCPP16RequestCommand.BOOT_NOTIFICATION,
+              chargingStation.bootNotificationRequest,
+              { skipBufferingOnError: true, triggerMessage: true }
+            )
+              .then(response => {
+                chargingStation.bootNotificationResponse = response
+              })
+              .catch(errorHandler)
+            break
+          case OCPP16MessageTrigger.Heartbeat:
+            chargingStation.ocppRequestService
+              .requestHandler<OCPP16HeartbeatRequest, OCPP16HeartbeatResponse>(
+              chargingStation,
+              OCPP16RequestCommand.HEARTBEAT,
+              undefined,
+              {
+                triggerMessage: true
+              }
+            )
+              .catch(errorHandler)
+            break
+          case OCPP16MessageTrigger.StatusNotification:
+            if (connectorId != null) {
+              chargingStation.ocppRequestService
+                .requestHandler<OCPP16StatusNotificationRequest, OCPP16StatusNotificationResponse>(
+                chargingStation,
+                OCPP16RequestCommand.STATUS_NOTIFICATION,
+                {
+                  connectorId,
+                  errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+                  status: chargingStation.getConnectorStatus(connectorId)?.status
+                },
+                {
+                  triggerMessage: true
+                }
               )
+                .catch(errorHandler)
+            } else if (chargingStation.hasEvses) {
+              for (const evseStatus of chargingStation.evses.values()) {
+                for (const [id, connectorStatus] of evseStatus.connectors) {
+                  chargingStation.ocppRequestService
+                    .requestHandler<
+                  OCPP16StatusNotificationRequest,
+                  OCPP16StatusNotificationResponse
+                  >(
+                    chargingStation,
+                    OCPP16RequestCommand.STATUS_NOTIFICATION,
+                    {
+                      connectorId: id,
+                      errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+                      status: connectorStatus.status
+                    },
+                    {
+                      triggerMessage: true
+                    }
+                  )
+                    .catch(errorHandler)
+                }
+              }
+            } else {
+              for (const [id, connectorStatus] of chargingStation.connectors) {
+                chargingStation.ocppRequestService
+                  .requestHandler<
+                OCPP16StatusNotificationRequest,
+                OCPP16StatusNotificationResponse
+                >(
+                  chargingStation,
+                  OCPP16RequestCommand.STATUS_NOTIFICATION,
+                  {
+                    connectorId: id,
+                    errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+                    status: connectorStatus.status
+                  },
+                  {
+                    triggerMessage: true
+                  }
+                )
+                  .catch(errorHandler)
+              }
             }
-          })
-          .catch(error => {
-            logger.error(
-              `${chargingStation.logPrefix()} ${moduleName}.constructor: Remote start transaction error:`,
-              error
-            )
-          })
+            break
+        }
       }
     )
     this.validatePayload = this.validatePayload.bind(this)
@@ -521,6 +635,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
       response,
       commandName
     )
+    this.emit(commandName, chargingStation, commandPayload, response)
   }
 
   private validatePayload (
@@ -1044,12 +1159,6 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         idTag
       )
     }
-    this.emit(
-      OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
-      chargingStation,
-      transactionConnectorId,
-      idTag
-    )
     return OCPP16Constants.OCPP_RESPONSE_ACCEPTED
   }
 
@@ -1448,110 +1557,13 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     ) {
       return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED
     }
-    try {
-      switch (requestedMessage) {
-        case OCPP16MessageTrigger.BootNotification:
-          setTimeout(() => {
-            chargingStation.ocppRequestService
-              .requestHandler<OCPP16BootNotificationRequest, OCPP16BootNotificationResponse>(
-              chargingStation,
-              OCPP16RequestCommand.BOOT_NOTIFICATION,
-              chargingStation.bootNotificationRequest,
-              { skipBufferingOnError: true, triggerMessage: true }
-            )
-              .then(response => {
-                chargingStation.bootNotificationResponse = response
-              })
-              .catch(Constants.EMPTY_FUNCTION)
-          }, OCPP16Constants.OCPP_TRIGGER_MESSAGE_DELAY)
-          return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
-        case OCPP16MessageTrigger.Heartbeat:
-          setTimeout(() => {
-            chargingStation.ocppRequestService
-              .requestHandler<OCPP16HeartbeatRequest, OCPP16HeartbeatResponse>(
-              chargingStation,
-              OCPP16RequestCommand.HEARTBEAT,
-              undefined,
-              {
-                triggerMessage: true
-              }
-            )
-              .catch(Constants.EMPTY_FUNCTION)
-          }, OCPP16Constants.OCPP_TRIGGER_MESSAGE_DELAY)
-          return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
-        case OCPP16MessageTrigger.StatusNotification:
-          setTimeout(() => {
-            if (connectorId != null) {
-              chargingStation.ocppRequestService
-                .requestHandler<OCPP16StatusNotificationRequest, OCPP16StatusNotificationResponse>(
-                chargingStation,
-                OCPP16RequestCommand.STATUS_NOTIFICATION,
-                {
-                  connectorId,
-                  errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
-                  status: chargingStation.getConnectorStatus(connectorId)?.status
-                },
-                {
-                  triggerMessage: true
-                }
-              )
-                .catch(Constants.EMPTY_FUNCTION)
-            } else if (chargingStation.hasEvses) {
-              for (const evseStatus of chargingStation.evses.values()) {
-                for (const [id, connectorStatus] of evseStatus.connectors) {
-                  chargingStation.ocppRequestService
-                    .requestHandler<
-                  OCPP16StatusNotificationRequest,
-                  OCPP16StatusNotificationResponse
-                  >(
-                    chargingStation,
-                    OCPP16RequestCommand.STATUS_NOTIFICATION,
-                    {
-                      connectorId: id,
-                      errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
-                      status: connectorStatus.status
-                    },
-                    {
-                      triggerMessage: true
-                    }
-                  )
-                    .catch(Constants.EMPTY_FUNCTION)
-                }
-              }
-            } else {
-              for (const [id, connectorStatus] of chargingStation.connectors) {
-                chargingStation.ocppRequestService
-                  .requestHandler<
-                OCPP16StatusNotificationRequest,
-                OCPP16StatusNotificationResponse
-                >(
-                  chargingStation,
-                  OCPP16RequestCommand.STATUS_NOTIFICATION,
-                  {
-                    connectorId: id,
-                    errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
-                    status: connectorStatus.status
-                  },
-                  {
-                    triggerMessage: true
-                  }
-                )
-                  .catch(Constants.EMPTY_FUNCTION)
-              }
-            }
-          }, OCPP16Constants.OCPP_TRIGGER_MESSAGE_DELAY)
-          return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
-        default:
-          return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
-      }
-    } catch (error) {
-      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-      return this.handleIncomingRequestError<OCPP16TriggerMessageResponse>(
-        chargingStation,
-        OCPP16IncomingRequestCommand.TRIGGER_MESSAGE,
-        error as Error,
-        { errorResponse: OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED }
-      )!
+    switch (requestedMessage) {
+      case OCPP16MessageTrigger.BootNotification:
+      case OCPP16MessageTrigger.Heartbeat:
+      case OCPP16MessageTrigger.StatusNotification:
+        return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
+      default:
+        return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED
     }
   }