docs: refine README.md
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPServiceUtils.ts
index 5dbe2fb4953d66ab535141e0ac4de12362f2af74..b74740875b2753642eb673ae3dcd9a8cb5a3c89a 100644 (file)
@@ -3,14 +3,19 @@ import { dirname, join } from 'node:path';
 import { fileURLToPath } from 'node:url';
 
 import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv';
+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,
@@ -145,7 +150,7 @@ export class OCPPServiceUtils {
   public static convertDateToISOString<T extends JsonType>(obj: T): void {
     for (const key in obj) {
       // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
-      if (obj![key] instanceof Date) {
+      if (isDate(obj![key])) {
         // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
         (obj![key] as string) = (obj![key] as Date).toISOString();
         // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
@@ -217,6 +222,35 @@ export class OCPPServiceUtils {
     chargingStation.getConnectorStatus(connectorId)!.status = status;
   }
 
+  public static async isIdTagAuthorized(
+    chargingStation: ChargingStation,
+    connectorId: number,
+    idTag: string,
+  ): Promise<boolean> {
+    if (!chargingStation.getLocalAuthListEnabled() && !chargingStation.getRemoteAuthorization()) {
+      logger.warn(
+        `${chargingStation.logPrefix()} The charging station expects to authorize RFID tags but nor local authorization nor remote authorization are enabled. Misbehavior may occur`,
+      );
+    }
+    let authorized = false;
+    if (
+      chargingStation.getLocalAuthListEnabled() === true &&
+      OCPPServiceUtils.isIdTagLocalAuthorized(chargingStation, idTag)
+    ) {
+      const connectorStatus: ConnectorStatus = chargingStation.getConnectorStatus(connectorId)!;
+      connectorStatus.localAuthorizeIdTag = idTag;
+      connectorStatus.idTagLocalAuthorized = true;
+      authorized = true;
+    } else if (chargingStation.getRemoteAuthorization()) {
+      authorized = await OCPPServiceUtils.isIdTagRemoteAuthorized(
+        chargingStation,
+        connectorId,
+        idTag,
+      );
+    }
+    return authorized;
+  }
+
   protected static checkConnectorStatusTransition(
     chargingStation: ChargingStation,
     connectorId: number,
@@ -396,6 +430,35 @@ export class OCPPServiceUtils {
       : numberValue * options.unitMultiplier!;
   }
 
+  private static isIdTagLocalAuthorized(chargingStation: ChargingStation, idTag: string): boolean {
+    return (
+      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,