Add CS template tunable to enable persistent OCPP configuration
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index a86e995efb477d53675da0a458e7d9e37ca32b84..bfb229be4d08045e53deb2eb760750351165ef03 100644 (file)
@@ -324,12 +324,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     } else if (keyToChange && keyToChange.readonly) {
       return Constants.OCPP_CONFIGURATION_RESPONSE_REJECTED;
     } else if (keyToChange && !keyToChange.readonly) {
-      const keyIndex = this.chargingStation.configuration.configurationKey.indexOf(keyToChange);
       let valueChanged = false;
-      if (
-        this.chargingStation.configuration.configurationKey[keyIndex].value !== commandPayload.value
-      ) {
-        this.chargingStation.configuration.configurationKey[keyIndex].value = commandPayload.value;
+      if (keyToChange.value !== commandPayload.value) {
+        this.chargingStation.setConfigurationKeyValue(
+          commandPayload.key,
+          commandPayload.value,
+          true
+        );
         valueChanged = true;
       }
       let triggerHeartbeatRestart = false;
@@ -400,7 +401,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
   private handleRequestClearChargingProfile(
     commandPayload: ClearChargingProfileRequest
   ): ClearChargingProfileResponse {
-    if (!this.chargingStation.getConnectorStatus(commandPayload.connectorId)) {
+    const connectorStatus = this.chargingStation.getConnectorStatus(commandPayload.connectorId);
+    if (!connectorStatus) {
       logger.error(
         `${this.chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
           commandPayload.connectorId
@@ -408,16 +410,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       );
       return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
     }
-    if (
-      commandPayload.connectorId &&
-      !Utils.isEmptyArray(
-        this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles
-      )
-    ) {
-      this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles = [];
+    if (commandPayload.connectorId && !Utils.isEmptyArray(connectorStatus.chargingProfiles)) {
+      connectorStatus.chargingProfiles = [];
       logger.debug(
         `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
-        this.chargingStation.getConnectorStatus(commandPayload.connectorId).chargingProfiles
+        connectorStatus.chargingProfiles
       );
       return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_ACCEPTED;
     }
@@ -453,13 +450,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
                 clearCurrentCP = true;
               }
               if (clearCurrentCP) {
-                this.chargingStation.getConnectorStatus(
-                  commandPayload.connectorId
-                ).chargingProfiles[index] = {} as OCPP16ChargingProfile;
+                connectorStatus.chargingProfiles[index] = {} as OCPP16ChargingProfile;
                 logger.debug(
                   `${this.chargingStation.logPrefix()} Charging profile(s) cleared, dump their stack: %j`,
-                  this.chargingStation.getConnectorStatus(commandPayload.connectorId)
-                    .chargingProfiles
+                  connectorStatus.chargingProfiles
                 );
                 clearedCP = true;
               }
@@ -533,7 +527,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
   private async handleRequestRemoteStartTransaction(
     commandPayload: RemoteStartTransactionRequest
   ): Promise<DefaultResponse> {
-    const transactionConnectorId: number = commandPayload.connectorId;
+    const transactionConnectorId = commandPayload.connectorId;
+    const connectorStatus = this.chargingStation.getConnectorStatus(transactionConnectorId);
     if (transactionConnectorId) {
       await this.chargingStation.ocppRequestService.sendMessageHandler(
         OCPP16RequestCommand.STATUS_NOTIFICATION,
@@ -543,12 +538,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
         }
       );
-      this.chargingStation.getConnectorStatus(transactionConnectorId).status =
-        OCPP16ChargePointStatus.PREPARING;
-      if (
-        this.chargingStation.isChargingStationAvailable() &&
-        this.chargingStation.isConnectorAvailable(transactionConnectorId)
-      ) {
+      connectorStatus.status = OCPP16ChargePointStatus.PREPARING;
+      if (this.chargingStation.isChargingStationAvailable() && connectorStatus) {
         // Check if authorized
         if (this.chargingStation.getAuthorizeRemoteTxRequests()) {
           let authorized = false;
@@ -557,14 +548,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             this.chargingStation.hasAuthorizedTags() &&
             this.chargingStation.authorizedTags.find((value) => value === commandPayload.idTag)
           ) {
-            this.chargingStation.getConnectorStatus(transactionConnectorId).localAuthorizeIdTag =
-              commandPayload.idTag;
-            this.chargingStation.getConnectorStatus(transactionConnectorId).idTagLocalAuthorized =
-              true;
+            connectorStatus.localAuthorizeIdTag = commandPayload.idTag;
+            connectorStatus.idTagLocalAuthorized = true;
             authorized = true;
           } else if (this.chargingStation.getMayAuthorizeAtRemoteStart()) {
-            this.chargingStation.getConnectorStatus(transactionConnectorId).authorizeIdTag =
-              commandPayload.idTag;
+            connectorStatus.authorizeIdTag = commandPayload.idTag;
             const authorizeResponse: OCPP16AuthorizeResponse =
               (await this.chargingStation.ocppRequestService.sendMessageHandler(
                 OCPP16RequestCommand.AUTHORIZE,
@@ -588,9 +576,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
                 commandPayload.chargingProfile
               )
             ) {
-              this.chargingStation.getConnectorStatus(
-                transactionConnectorId
-              ).transactionRemoteStarted = true;
+              connectorStatus.transactionRemoteStarted = true;
               if (
                 (
                   (await this.chargingStation.ocppRequestService.sendMessageHandler(
@@ -635,8 +621,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             commandPayload.chargingProfile
           )
         ) {
-          this.chargingStation.getConnectorStatus(transactionConnectorId).transactionRemoteStarted =
-            true;
+          connectorStatus.transactionRemoteStarted = true;
           if (
             (
               (await this.chargingStation.ocppRequestService.sendMessageHandler(
@@ -829,8 +814,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
                 info.bytes / 1024
               } bytes transferred from diagnostics archive ${info.name}`
             );
-            await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(
-              OCPP16DiagnosticsStatus.Uploading
+            await this.chargingStation.ocppRequestService.sendMessageHandler(
+              OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
+              {
+                status: OCPP16DiagnosticsStatus.Uploading,
+              }
             );
           });
           uploadResponse = await ftpClient.uploadFrom(
@@ -838,8 +826,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             uri.pathname + diagnosticsArchive
           );
           if (uploadResponse.code === 226) {
-            await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(
-              OCPP16DiagnosticsStatus.Uploaded
+            await this.chargingStation.ocppRequestService.sendMessageHandler(
+              OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
+              {
+                status: OCPP16DiagnosticsStatus.Uploaded,
+              }
             );
             if (ftpClient) {
               ftpClient.close();
@@ -862,8 +853,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           OCPP16IncomingRequestCommand.GET_DIAGNOSTICS
         );
       } catch (error) {
-        await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(
-          OCPP16DiagnosticsStatus.UploadFailed
+        await this.chargingStation.ocppRequestService.sendMessageHandler(
+          OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
+          {
+            status: OCPP16DiagnosticsStatus.UploadFailed,
+          }
         );
         if (ftpClient) {
           ftpClient.close();
@@ -880,8 +874,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           uri.protocol
         } to transfer the diagnostic logs archive`
       );
-      await this.chargingStation.ocppRequestService.sendDiagnosticsStatusNotification(
-        OCPP16DiagnosticsStatus.UploadFailed
+      await this.chargingStation.ocppRequestService.sendMessageHandler(
+        OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
+        {
+          status: OCPP16DiagnosticsStatus.UploadFailed,
+        }
       );
       return Constants.OCPP_RESPONSE_EMPTY;
     }