Add tunable in template to disable limitation on custom metervalues
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index 8d5d0a20e27d86169f0efc86ec954efcfe8e9a2b..8dc9135fa7b80e314b0880214fd029f0f7c9a8db 100644 (file)
@@ -131,7 +131,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     commandName: OCPP16IncomingRequestCommand,
     commandPayload: JsonType
   ): Promise<void> {
-    let result: JsonType;
+    let response: JsonType;
     if (
       this.chargingStation.getOcppStrictCompliance() &&
       this.chargingStation.isInPendingState() &&
@@ -154,8 +154,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     ) {
       if (this.incomingRequestHandlers.has(commandName)) {
         try {
-          // Call the method to build the result
-          result = await this.incomingRequestHandlers.get(commandName)(commandPayload);
+          // Call the method to build the response
+          response = await this.incomingRequestHandlers.get(commandName)(commandPayload);
         } catch (error) {
           // Log
           logger.error(this.chargingStation.logPrefix() + ' Handle request error: %j', error);
@@ -184,19 +184,17 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         commandName
       );
     }
-    // Send the built result
-    await this.chargingStation.ocppRequestService.sendResult(messageId, result, commandName);
+    // Send the built response
+    await this.chargingStation.ocppRequestService.sendResponse(messageId, response, commandName);
   }
 
   // Simulate charging station restart
   private handleRequestReset(commandPayload: ResetRequest): DefaultResponse {
     // eslint-disable-next-line @typescript-eslint/no-misused-promises
     setImmediate(async (): Promise<void> => {
-      await this.chargingStation.stop(
+      await this.chargingStation.reset(
         (commandPayload.type + 'Reset') as OCPP16StopTransactionReason
       );
-      await Utils.sleep(this.chargingStation.stationInfo.resetTime);
-      this.chargingStation.start();
     });
     logger.info(
       `${this.chargingStation.logPrefix()} ${
@@ -950,6 +948,15 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     ) {
       return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED;
     }
+    // TODO: factor out the check on connector id
+    if (commandPayload?.connectorId < 0) {
+      logger.warn(
+        `${this.chargingStation.logPrefix()} ${
+          OCPP16IncomingRequestCommand.TRIGGER_MESSAGE
+        } incoming request received with invalid connectorId ${commandPayload.connectorId}`
+      );
+      return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_REJECTED;
+    }
     try {
       switch (commandPayload.requestedMessage) {
         case MessageTrigger.BootNotification:
@@ -999,6 +1006,49 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
               });
           }, Constants.OCPP_TRIGGER_MESSAGE_DELAY);
           return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED;
+        case MessageTrigger.StatusNotification:
+          setTimeout(() => {
+            if (commandPayload?.connectorId) {
+              this.chargingStation.ocppRequestService
+                .requestHandler<OCPP16StatusNotificationRequest, OCPP16StatusNotificationResponse>(
+                  OCPP16RequestCommand.STATUS_NOTIFICATION,
+                  {
+                    connectorId: commandPayload.connectorId,
+                    errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+                    status: this.chargingStation.getConnectorStatus(commandPayload.connectorId)
+                      .status,
+                  },
+                  {
+                    triggerMessage: true,
+                  }
+                )
+                .catch(() => {
+                  /* This is intentional */
+                });
+            } else {
+              for (const connectorId of this.chargingStation.connectors.keys()) {
+                this.chargingStation.ocppRequestService
+                  .requestHandler<
+                    OCPP16StatusNotificationRequest,
+                    OCPP16StatusNotificationResponse
+                  >(
+                    OCPP16RequestCommand.STATUS_NOTIFICATION,
+                    {
+                      connectorId,
+                      errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
+                      status: this.chargingStation.getConnectorStatus(connectorId).status,
+                    },
+                    {
+                      triggerMessage: true,
+                    }
+                  )
+                  .catch(() => {
+                    /* This is intentional */
+                  });
+              }
+            }
+          }, Constants.OCPP_TRIGGER_MESSAGE_DELAY);
+          return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_ACCEPTED;
         default:
           return Constants.OCPP_TRIGGER_MESSAGE_RESPONSE_NOT_IMPLEMENTED;
       }