fix(simulator): make the modules export/import a bit less sensitive to
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPServiceUtils.ts
index 04ca704648a0170b588faae68753ef3102e88bbc..4b232033f70b1f71509e8034b4b6978a3a8194d8 100644 (file)
@@ -1,27 +1,33 @@
-import type { DefinedError, ErrorObject } from 'ajv';
+import fs from 'node:fs';
 
-import BaseError from '../../exception/BaseError';
-import type { JsonObject, JsonType } from '../../types/JsonType';
-import type { SampledValueTemplate } from '../../types/MeasurandPerPhaseSampledValueTemplates';
-import type { OCPP16StatusNotificationRequest } from '../../types/ocpp/1.6/Requests';
-import type { OCPP20StatusNotificationRequest } from '../../types/ocpp/2.0/Requests';
-import { ChargePointErrorCode } from '../../types/ocpp/ChargePointErrorCode';
-import { StandardParametersKey } from '../../types/ocpp/Configuration';
-import type { ConnectorStatusEnum } from '../../types/ocpp/ConnectorStatusEnum';
-import { ErrorType } from '../../types/ocpp/ErrorType';
-import { MessageType } from '../../types/ocpp/MessageType';
-import { MeterValueMeasurand, type MeterValuePhase } from '../../types/ocpp/MeterValues';
-import { OCPPVersion } from '../../types/ocpp/OCPPVersion';
+import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv';
+
+import { BaseError } from '../../exception';
 import {
+  ChargePointErrorCode,
+  type ConnectorStatusEnum,
+  ErrorType,
+  FileType,
   IncomingRequestCommand,
+  type JsonObject,
+  type JsonType,
   MessageTrigger,
+  MessageType,
+  MeterValueMeasurand,
+  type MeterValuePhase,
+  type OCPP16StatusNotificationRequest,
+  type OCPP20StatusNotificationRequest,
+  OCPPVersion,
   RequestCommand,
+  type SampledValueTemplate,
+  StandardParametersKey,
   type StatusNotificationRequest,
-} from '../../types/ocpp/Requests';
-import Constants from '../../utils/Constants';
-import logger from '../../utils/Logger';
-import Utils from '../../utils/Utils';
-import type ChargingStation from '../ChargingStation';
+} from '../../types';
+import { Constants } from '../../utils/Constants';
+import { FileUtils } from '../../utils/FileUtils';
+import { logger } from '../../utils/Logger';
+import { Utils } from '../../utils/Utils';
+import type { ChargingStation } from '../ChargingStation';
 import { ChargingStationConfigurationUtils } from '../ChargingStationConfigurationUtils';
 
 export class OCPPServiceUtils {
@@ -164,6 +170,25 @@ export class OCPPServiceUtils {
     }
   }
 
+  protected static parseJsonSchemaFile<T extends JsonType>(
+    filePath: string,
+    ocppVersion: OCPPVersion,
+    moduleName?: string,
+    methodName?: string
+  ): JSONSchemaType<T> {
+    try {
+      return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType<T>;
+    } catch (error) {
+      FileUtils.handleFileException(
+        filePath,
+        FileType.JsonSchema,
+        error as NodeJS.ErrnoException,
+        OCPPServiceUtils.logPrefix(ocppVersion, moduleName, methodName),
+        { throwError: false }
+      );
+    }
+  }
+
   protected static getSampledValueTemplate(
     chargingStation: ChargingStation,
     connectorId: number,
@@ -182,7 +207,7 @@ export class OCPPServiceUtils {
       ChargingStationConfigurationUtils.getConfigurationKey(
         chargingStation,
         StandardParametersKey.MeterValuesSampledData
-      )?.value.includes(measurand) === false
+      )?.value?.includes(measurand) === false
     ) {
       logger.debug(
         `${chargingStation.logPrefix()} Trying to get MeterValues measurand '${measurand}' ${onPhaseStr}in template on connectorId ${connectorId} not found in '${
@@ -192,10 +217,10 @@ export class OCPPServiceUtils {
       return;
     }
     const sampledValueTemplates: SampledValueTemplate[] =
-      chargingStation.getConnectorStatus(connectorId).MeterValues;
+      chargingStation.getConnectorStatus(connectorId)?.MeterValues;
     for (
       let index = 0;
-      Utils.isEmptyArray(sampledValueTemplates) === false && index < sampledValueTemplates.length;
+      Utils.isNotEmptyArray(sampledValueTemplates) === true && index < sampledValueTemplates.length;
       index++
     ) {
       if (
@@ -214,7 +239,7 @@ export class OCPPServiceUtils {
         ChargingStationConfigurationUtils.getConfigurationKey(
           chargingStation,
           StandardParametersKey.MeterValuesSampledData
-        )?.value.includes(measurand) === true
+        )?.value?.includes(measurand) === true
       ) {
         return sampledValueTemplates[index];
       } else if (
@@ -224,7 +249,7 @@ export class OCPPServiceUtils {
         ChargingStationConfigurationUtils.getConfigurationKey(
           chargingStation,
           StandardParametersKey.MeterValuesSampledData
-        )?.value.includes(measurand) === true
+        )?.value?.includes(measurand) === true
       ) {
         return sampledValueTemplates[index];
       } else if (
@@ -261,4 +286,16 @@ export class OCPPServiceUtils {
       ? Math.min(numberValue * options.unitMultiplier, limit)
       : numberValue * options.unitMultiplier;
   }
+
+  private static logPrefix = (
+    ocppVersion: OCPPVersion,
+    moduleName?: string,
+    methodName?: string
+  ): string => {
+    const logMsg =
+      Utils.isNotEmptyString(moduleName) && Utils.isNotEmptyString(methodName)
+        ? ` OCPP ${ocppVersion} | ${moduleName}.${methodName}:`
+        : ` OCPP ${ocppVersion} |`;
+    return Utils.logPrefix(logMsg);
+  };
 }