Permit to run code in async scope in the OCPP stack
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16IncomingRequestService.ts
index f951fc77145cceda22e15f77b0f873da8a901e12..52751b4bac994ddfc9e2bc0f68746093b7c2bb55 100644 (file)
@@ -303,7 +303,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     ) {
       if (
         this.incomingRequestHandlers.has(commandName) &&
-        ChargingStationUtils.isIncomingRequestCommandSupported(commandName, chargingStation)
+        OCPP16ServiceUtils.isIncomingRequestCommandSupported(chargingStation, commandName)
       ) {
         try {
           this.validatePayload(chargingStation, commandName, commandPayload);
@@ -378,10 +378,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     chargingStation: ChargingStation,
     commandPayload: ResetRequest
   ): DefaultResponse {
-    // eslint-disable-next-line @typescript-eslint/no-misused-promises
-    setImmediate(async (): Promise<void> => {
-      await chargingStation.reset((commandPayload.type + 'Reset') as OCPP16StopTransactionReason);
-    });
+    this.asyncResource.runInAsyncScope(
+      chargingStation.reset.bind(chargingStation) as (
+        this: ChargingStation,
+        ...args: any[]
+      ) => void,
+      chargingStation,
+      (commandPayload.type + 'Reset') as OCPP16StopTransactionReason
+    );
     logger.info(
       `${chargingStation.logPrefix()} ${
         commandPayload.type
@@ -392,7 +396,10 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     return Constants.OCPP_RESPONSE_ACCEPTED;
   }
 
-  private handleRequestClearCache(): DefaultResponse {
+  private handleRequestClearCache(chargingStation: ChargingStation): DefaultResponse {
+    chargingStation.authorizedTagsCache.deleteAuthorizedTags(
+      ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo)
+    );
     return Constants.OCPP_RESPONSE_ACCEPTED;
   }
 
@@ -537,15 +544,15 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     commandPayload: SetChargingProfileRequest
   ): SetChargingProfileResponse {
     if (
-      !OCPP16ServiceUtils.checkFeatureProfile(
+      OCPP16ServiceUtils.checkFeatureProfile(
         chargingStation,
         OCPP16SupportedFeatureProfiles.SmartCharging,
         OCPP16IncomingRequestCommand.SET_CHARGING_PROFILE
-      )
+      ) === false
     ) {
       return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_NOT_SUPPORTED;
     }
-    if (!chargingStation.getConnectorStatus(commandPayload.connectorId)) {
+    if (chargingStation.connectors.has(commandPayload.connectorId) === false) {
       logger.error(
         `${chargingStation.logPrefix()} Trying to set charging profile(s) to a non existing connector Id ${
           commandPayload.connectorId
@@ -569,7 +576,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     ) {
       return Constants.OCPP_SET_CHARGING_PROFILE_RESPONSE_REJECTED;
     }
-    chargingStation.setChargingProfile(
+    OCPP16ServiceUtils.setChargingProfile(
+      chargingStation,
       commandPayload.connectorId,
       commandPayload.csChargingProfiles
     );
@@ -587,16 +595,15 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     commandPayload: ClearChargingProfileRequest
   ): ClearChargingProfileResponse {
     if (
-      !OCPP16ServiceUtils.checkFeatureProfile(
+      OCPP16ServiceUtils.checkFeatureProfile(
         chargingStation,
         OCPP16SupportedFeatureProfiles.SmartCharging,
         OCPP16IncomingRequestCommand.CLEAR_CHARGING_PROFILE
-      )
+      ) === false
     ) {
       return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
     }
-    const connectorStatus = chargingStation.getConnectorStatus(commandPayload.connectorId);
-    if (!connectorStatus) {
+    if (chargingStation.connectors.has(commandPayload.connectorId) === false) {
       logger.error(
         `${chargingStation.logPrefix()} Trying to clear a charging profile(s) to a non existing connector Id ${
           commandPayload.connectorId
@@ -604,6 +611,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       );
       return Constants.OCPP_CLEAR_CHARGING_PROFILE_RESPONSE_UNKNOWN;
     }
+    const connectorStatus = chargingStation.getConnectorStatus(commandPayload.connectorId);
     if (commandPayload.connectorId && !Utils.isEmptyArray(connectorStatus.chargingProfiles)) {
       connectorStatus.chargingProfiles = [];
       logger.debug(
@@ -700,9 +708,8 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       return response;
     } else if (
       connectorId > 0 &&
-      (chargingStation.getConnectorStatus(0).availability === OCPP16AvailabilityType.OPERATIVE ||
-        (chargingStation.getConnectorStatus(0).availability ===
-          OCPP16AvailabilityType.INOPERATIVE &&
+      (chargingStation.isChargingStationAvailable() === true ||
+        (chargingStation.isChargingStationAvailable() === false &&
           commandPayload.type === OCPP16AvailabilityType.INOPERATIVE))
     ) {
       if (chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) {
@@ -729,8 +736,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     commandPayload: RemoteStartTransactionRequest
   ): Promise<DefaultResponse> {
     const transactionConnectorId = commandPayload.connectorId;
-    const connectorStatus = chargingStation.getConnectorStatus(transactionConnectorId);
-    if (transactionConnectorId) {
+    if (chargingStation.connectors.has(transactionConnectorId) === true) {
       const remoteStartTransactionLogMsg =
         chargingStation.logPrefix() +
         ' Transaction remotely STARTED on ' +
@@ -748,24 +754,25 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
         status: OCPP16ChargePointStatus.PREPARING,
         errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
       });
+      const connectorStatus = chargingStation.getConnectorStatus(transactionConnectorId);
       connectorStatus.status = OCPP16ChargePointStatus.PREPARING;
-      if (chargingStation.isChargingStationAvailable() && connectorStatus) {
+      if (chargingStation.isChargingStationAvailable() === true) {
         // Check if authorized
-        if (chargingStation.getAuthorizeRemoteTxRequests()) {
+        if (chargingStation.getAuthorizeRemoteTxRequests() === true) {
           let authorized = false;
           if (
-            chargingStation.getLocalAuthListEnabled() &&
-            chargingStation.hasAuthorizedTags() &&
+            chargingStation.getLocalAuthListEnabled() === true &&
+            chargingStation.hasAuthorizedTags() === true &&
             chargingStation.authorizedTagsCache
               .getAuthorizedTags(
                 ChargingStationUtils.getAuthorizationFile(chargingStation.stationInfo)
               )
-              .find((value) => value === commandPayload.idTag)
+              .find((idTag) => idTag === commandPayload.idTag)
           ) {
             connectorStatus.localAuthorizeIdTag = commandPayload.idTag;
             connectorStatus.idTagLocalAuthorized = true;
             authorized = true;
-          } else if (chargingStation.getMustAuthorizeAtRemoteStart()) {
+          } else if (chargingStation.getMustAuthorizeAtRemoteStart() === true) {
             connectorStatus.authorizeIdTag = commandPayload.idTag;
             const authorizeResponse: OCPP16AuthorizeResponse =
               await chargingStation.ocppRequestService.requestHandler<
@@ -782,14 +789,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
               `${chargingStation.logPrefix()} The charging station configuration expects authorize at remote start transaction but local authorization or authorize isn't enabled`
             );
           }
-          if (authorized) {
+          if (authorized === true) {
             // Authorization successful, start transaction
             if (
               this.setRemoteStartTransactionChargingProfile(
                 chargingStation,
                 transactionConnectorId,
                 commandPayload.chargingProfile
-              )
+              ) === true
             ) {
               connectorStatus.transactionRemoteStarted = true;
               if (
@@ -830,7 +837,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
             chargingStation,
             transactionConnectorId,
             commandPayload.chargingProfile
-          )
+          ) === true
         ) {
           connectorStatus.transactionRemoteStarted = true;
           if (
@@ -911,7 +918,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     cp: OCPP16ChargingProfile
   ): boolean {
     if (cp && cp.chargingProfilePurpose === ChargingProfilePurposeType.TX_PROFILE) {
-      chargingStation.setChargingProfile(connectorId, cp);
+      OCPP16ServiceUtils.setChargingProfile(chargingStation, connectorId, cp);
       logger.debug(
         `${chargingStation.logPrefix()} Charging profile(s) set at remote start transaction on connector id ${connectorId}, dump their stack: %j`,
         chargingStation.getConnectorStatus(connectorId).chargingProfiles
@@ -971,11 +978,11 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
     commandPayload: GetDiagnosticsRequest
   ): Promise<GetDiagnosticsResponse> {
     if (
-      !OCPP16ServiceUtils.checkFeatureProfile(
+      OCPP16ServiceUtils.checkFeatureProfile(
         chargingStation,
         OCPP16SupportedFeatureProfiles.FirmwareManagement,
         OCPP16IncomingRequestCommand.GET_DIAGNOSTICS
-      )
+      ) === false
     ) {
       return Constants.OCPP_RESPONSE_EMPTY;
     }