fix: fix authorize response handling
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 30 Jul 2023 21:30:56 +0000 (23:30 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 30 Jul 2023 21:30:56 +0000 (23:30 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts
src/charging-station/ocpp/OCPPServiceUtils.ts

index 1dc0a80924e9ee08dad6506e354e168277332984..ae4beec8186ac4c22820fec9d792714f65b4a5ec 100644 (file)
@@ -7,12 +7,11 @@ import { hoursToMilliseconds, secondsToMilliseconds } from 'date-fns';
 import type { ChargingStation } from './ChargingStation';
 import { checkChargingStation } from './Helpers';
 import { IdTagsCache } from './IdTagsCache';
+import { OCPPServiceUtils } from './ocpp';
 import { BaseError } from '../exception';
 import { PerformanceStatistics } from '../performance';
 import {
   AuthorizationStatus,
-  type AuthorizeRequest,
-  type AuthorizeResponse,
   ConnectorStatusEnum,
   RequestCommand,
   type StartTransactionRequest,
@@ -397,24 +396,8 @@ export class AutomaticTransactionGenerator extends AsyncResource {
         connectorId,
       )} start transaction with an idTag '${idTag}'`;
       if (this.getRequireAuthorize()) {
-        // Authorize idTag
-        const authorizeResponse: AuthorizeResponse =
-          await this.chargingStation.ocppRequestService.requestHandler<
-            AuthorizeRequest,
-            AuthorizeResponse
-          >(this.chargingStation, RequestCommand.AUTHORIZE, {
-            idTag,
-          });
         ++this.connectorsStatus.get(connectorId)!.authorizeRequests!;
-        if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) {
-          if (
-            isNullOrUndefined(this.chargingStation.getConnectorStatus(connectorId)?.authorizeIdTag)
-          ) {
-            logger.warn(
-              `${this.chargingStation.logPrefix()} IdTag ${idTag} is not set as authorized remotely, applying deferred initialization`,
-            );
-            this.chargingStation.getConnectorStatus(connectorId)!.authorizeIdTag = idTag;
-          }
+        if (await OCPPServiceUtils.isIdTagAuthorized(this.chargingStation, connectorId, idTag)) {
           ++this.connectorsStatus.get(connectorId)!.acceptedAuthorizeRequests!;
           logger.info(startTransactionLogMsg);
           // Start transaction
index 262f3b5305d94ebd43431732b090545c0ce42d96..3ec3c159bc88d219fabcc6d39ecaccd504d2f880 100644 (file)
@@ -472,8 +472,8 @@ export class OCPP16ResponseService extends OCPPResponseService {
     const authorizeConnectorIdDefined = !isNullOrUndefined(authorizeConnectorId);
     if (payload.idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) {
       if (authorizeConnectorIdDefined) {
-        chargingStation.getConnectorStatus(authorizeConnectorId!)!.authorizeIdTag =
-          requestPayload.idTag;
+        // chargingStation.getConnectorStatus(authorizeConnectorId!)!.authorizeIdTag =
+        //   requestPayload.idTag;
         chargingStation.getConnectorStatus(authorizeConnectorId!)!.idTagAuthorized = true;
       }
       logger.debug(
index bf019cee3c6fffc6756f0f8d0a97b664dc676387..00594e1c7ed664809d195ff412f6eab9e48a851e 100644 (file)
@@ -2,11 +2,10 @@
 
 import type { JSONSchemaType } from 'ajv';
 
-import { type ChargingStation, getIdTagsFile, hasFeatureProfile } from '../../../charging-station';
+import { type ChargingStation, hasFeatureProfile } from '../../../charging-station';
 import { OCPPError } from '../../../exception';
 import {
   type ClearChargingProfileRequest,
-  type ConnectorStatus,
   CurrentType,
   ErrorType,
   type JsonType,
@@ -15,9 +14,6 @@ import {
   MeterValueContext,
   MeterValueLocation,
   MeterValueUnit,
-  OCPP16AuthorizationStatus,
-  type OCPP16AuthorizeRequest,
-  type OCPP16AuthorizeResponse,
   type OCPP16ChargingProfile,
   type OCPP16IncomingRequestCommand,
   type OCPP16MeterValue,
@@ -41,7 +37,6 @@ import {
   getRandomFloatRounded,
   getRandomInteger,
   isNotEmptyArray,
-  isNotEmptyString,
   isNullOrUndefined,
   isUndefined,
   logger,
@@ -886,29 +881,6 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
     );
   }
 
-  public static async isIdTagAuthorized(
-    chargingStation: ChargingStation,
-    connectorId: number,
-    idTag: string,
-  ): Promise<boolean> {
-    let authorized = false;
-    const connectorStatus: ConnectorStatus = chargingStation.getConnectorStatus(connectorId)!;
-    if (OCPP16ServiceUtils.isIdTagLocalAuthorized(chargingStation, idTag)) {
-      connectorStatus.localAuthorizeIdTag = idTag;
-      connectorStatus.idTagLocalAuthorized = true;
-      authorized = true;
-    } else {
-      authorized = await OCPP16ServiceUtils.isIdTagRemoteAuthorized(chargingStation, idTag);
-      if (authorized && isNullOrUndefined(connectorStatus.authorizeIdTag)) {
-        logger.warn(
-          `${chargingStation.logPrefix()} IdTag ${idTag} is not set as authorized remotely, applying deferred initialization`,
-        );
-        connectorStatus.authorizeIdTag = idTag;
-      }
-    }
-    return authorized;
-  }
-
   private static buildSampledValue(
     sampledValueTemplate: SampledValueTemplate,
     value: number,
@@ -984,30 +956,4 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils {
         return MeterValueUnit.VOLT;
     }
   }
-
-  private static isIdTagLocalAuthorized(chargingStation: ChargingStation, idTag: string): boolean {
-    return (
-      chargingStation.getLocalAuthListEnabled() === true &&
-      chargingStation.hasIdTags() === true &&
-      isNotEmptyString(
-        chargingStation.idTagsCache
-          .getIdTags(getIdTagsFile(chargingStation.stationInfo)!)
-          ?.find((tag) => tag === idTag),
-      )
-    );
-  }
-
-  private static async isIdTagRemoteAuthorized(
-    chargingStation: ChargingStation,
-    idTag: string,
-  ): Promise<boolean> {
-    const authorizeResponse: OCPP16AuthorizeResponse =
-      await chargingStation.ocppRequestService.requestHandler<
-        OCPP16AuthorizeRequest,
-        OCPP16AuthorizeResponse
-      >(chargingStation, OCPP16RequestCommand.AUTHORIZE, {
-        idTag,
-      });
-    return authorizeResponse?.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED;
-  }
 }
index d09d8b1a1afaffc72b9891ab52f72b48f6282c69..64fe38eaacdc39f7d45745cf89c743abd8ab4a58 100644 (file)
@@ -8,10 +8,14 @@ import { isDate } from 'date-fns';
 import { OCPP16Constants } from './1.6/OCPP16Constants';
 import { OCPP20Constants } from './2.0/OCPP20Constants';
 import { OCPPConstants } from './OCPPConstants';
-import { type ChargingStation, getConfigurationKey } from '../../charging-station';
+import { type ChargingStation, getConfigurationKey, getIdTagsFile } from '../../charging-station';
 import { BaseError } from '../../exception';
 import {
+  AuthorizationStatus,
+  type AuthorizeRequest,
+  type AuthorizeResponse,
   ChargePointErrorCode,
+  type ConnectorStatus,
   type ConnectorStatusEnum,
   ErrorType,
   FileType,
@@ -218,6 +222,27 @@ export class OCPPServiceUtils {
     chargingStation.getConnectorStatus(connectorId)!.status = status;
   }
 
+  public static async isIdTagAuthorized(
+    chargingStation: ChargingStation,
+    connectorId: number,
+    idTag: string,
+  ): Promise<boolean> {
+    let authorized = false;
+    if (OCPPServiceUtils.isIdTagLocalAuthorized(chargingStation, idTag)) {
+      const connectorStatus: ConnectorStatus = chargingStation.getConnectorStatus(connectorId)!;
+      connectorStatus.localAuthorizeIdTag = idTag;
+      connectorStatus.idTagLocalAuthorized = true;
+      authorized = true;
+    } else {
+      authorized = await OCPPServiceUtils.isIdTagRemoteAuthorized(
+        chargingStation,
+        connectorId,
+        idTag,
+      );
+    }
+    return authorized;
+  }
+
   protected static checkConnectorStatusTransition(
     chargingStation: ChargingStation,
     connectorId: number,
@@ -397,6 +422,36 @@ export class OCPPServiceUtils {
       : numberValue * options.unitMultiplier!;
   }
 
+  private static isIdTagLocalAuthorized(chargingStation: ChargingStation, idTag: string): boolean {
+    return (
+      chargingStation.getLocalAuthListEnabled() === true &&
+      chargingStation.hasIdTags() === true &&
+      isNotEmptyString(
+        chargingStation.idTagsCache
+          .getIdTags(getIdTagsFile(chargingStation.stationInfo)!)
+          ?.find((tag) => tag === idTag),
+      )
+    );
+  }
+
+  private static async isIdTagRemoteAuthorized(
+    chargingStation: ChargingStation,
+    connectorId: number,
+    idTag: string,
+  ): Promise<boolean> {
+    chargingStation.getConnectorStatus(connectorId)!.authorizeIdTag = idTag;
+    return (
+      (
+        await chargingStation.ocppRequestService.requestHandler<
+          AuthorizeRequest,
+          AuthorizeResponse
+        >(chargingStation, RequestCommand.AUTHORIZE, {
+          idTag,
+        })
+      )?.idTagInfo?.status === AuthorizationStatus.ACCEPTED
+    );
+  }
+
   private static logPrefix = (
     ocppVersion: OCPPVersion,
     moduleName?: string,