Merge pull request #960 from SAP/fix-remote-start
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index 9b3748d3304dc9cd591075862c40b064062bdf54..7f932cb0e19165e65899f8265f5f7763ce415821 100644 (file)
@@ -420,15 +420,13 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         )
           .then(response => {
             if (response.status === OCPP16AuthorizationStatus.ACCEPTED) {
-              logger.debug(`
-        ${chargingStation.logPrefix()} Remote start transaction ACCEPTED on ${
-          chargingStation.stationInfo?.chargingStationId
-        }#${connectorId} for idTag '${idTag}'`)
+              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}'`)
+              logger.debug(
+                `${chargingStation.logPrefix()} Remote start transaction REJECTED on ${chargingStation.stationInfo?.chargingStationId}#${connectorId} for idTag '${idTag}'`
+              )
             }
           })
           .catch(error => {
@@ -439,6 +437,107 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
           })
       }
     )
+    this.on(
+      `Trigger${OCPP16MessageTrigger.BootNotification}`,
+      (chargingStation: ChargingStation) => {
+        chargingStation.ocppRequestService
+          .requestHandler<OCPP16BootNotificationRequest, OCPP16BootNotificationResponse>(
+          chargingStation,
+          OCPP16RequestCommand.BOOT_NOTIFICATION,
+          chargingStation.bootNotificationRequest,
+          { skipBufferingOnError: true, triggerMessage: true }
+        )
+          .then(response => {
+            chargingStation.bootNotificationResponse = response
+          })
+          .catch(error => {
+            logger.error(
+              `${chargingStation.logPrefix()} ${moduleName}.constructor: Trigger boot notification error:`,
+              error
+            )
+          })
+      }
+    )
+    this.on(`Trigger${OCPP16MessageTrigger.Heartbeat}`, (chargingStation: ChargingStation) => {
+      chargingStation.ocppRequestService
+        .requestHandler<OCPP16HeartbeatRequest, OCPP16HeartbeatResponse>(
+        chargingStation,
+        OCPP16RequestCommand.HEARTBEAT,
+        undefined,
+        {
+          triggerMessage: true
+        }
+      )
+        .catch(error => {
+          logger.error(
+            `${chargingStation.logPrefix()} ${moduleName}.constructor: Trigger heartbeat error:`,
+            error
+          )
+        })
+    })
+    this.on(
+      `$Trigger${OCPP16MessageTrigger.StatusNotification}`,
+      (chargingStation: ChargingStation, connectorId?: number) => {
+        const errorHandler = (error: Error): void => {
+          logger.error(
+            `${chargingStation.logPrefix()} ${moduleName}.constructor: Trigger status notification error:`,
+            error
+          )
+        }
+        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)
+          }
+        }
+      }
+    )
     this.validatePayload = this.validatePayload.bind(this)
   }
 
@@ -1046,12 +1145,16 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
         idTag
       )
     }
-    this.emit(
-      OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
-      chargingStation,
-      transactionConnectorId,
-      idTag
-    )
+    Promise.resolve()
+      .then(() =>
+        this.emit(
+          OCPP16IncomingRequestCommand.REMOTE_START_TRANSACTION,
+          chargingStation,
+          transactionConnectorId,
+          idTag
+        )
+      )
+      .catch(Constants.EMPTY_FUNCTION)
     return OCPP16Constants.OCPP_RESPONSE_ACCEPTED
   }
 
@@ -1453,95 +1556,27 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService {
     try {
       switch (requestedMessage) {
         case OCPP16MessageTrigger.BootNotification:
-          setTimeout(() => {
-            chargingStation.ocppRequestService
-              .requestHandler<OCPP16BootNotificationRequest, OCPP16BootNotificationResponse>(
-              chargingStation,
-              OCPP16RequestCommand.BOOT_NOTIFICATION,
-              chargingStation.bootNotificationRequest,
-              { skipBufferingOnError: true, triggerMessage: true }
+          Promise.resolve()
+            .then(() =>
+              this.emit(`Trigger${OCPP16MessageTrigger.BootNotification}`, chargingStation)
             )
-              .then(response => {
-                chargingStation.bootNotificationResponse = response
-              })
-              .catch(Constants.EMPTY_FUNCTION)
-          }, OCPP16Constants.OCPP_TRIGGER_MESSAGE_DELAY)
+            .catch(Constants.EMPTY_FUNCTION)
           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)
+          Promise.resolve()
+            .then(() => this.emit(`Trigger${OCPP16MessageTrigger.Heartbeat}`, chargingStation))
+            .catch(Constants.EMPTY_FUNCTION)
           return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
         case OCPP16MessageTrigger.StatusNotification:
-          setTimeout(() => {
-            if (connectorId != null) {
-              chargingStation.ocppRequestService
-                .requestHandler<OCPP16StatusNotificationRequest, OCPP16StatusNotificationResponse>(
+          Promise.resolve()
+            .then(() =>
+              this.emit(
+                `Trigger${OCPP16MessageTrigger.StatusNotification}`,
                 chargingStation,
-                OCPP16RequestCommand.STATUS_NOTIFICATION,
-                {
-                  connectorId,
-                  errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
-                  status: chargingStation.getConnectorStatus(connectorId)?.status
-                },
-                {
-                  triggerMessage: true
-                }
+                connectorId
               )
-                .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)
+            )
+            .catch(Constants.EMPTY_FUNCTION)
           return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED
         default:
           return OCPP16Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED