Use arrow function for log messages prefixing
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPServiceUtils.ts
index cf2aa07eb9d9c280295a74f10e84bdb5bf273b9a..6d21182891224ec436dba3946993b776d3cc6eb4 100644 (file)
@@ -1,6 +1,9 @@
-import type { DefinedError, ErrorObject } from 'ajv';
+import fs from 'node:fs';
+
+import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv';
 
 import BaseError from '../../exception/BaseError';
+import { FileType } from '../../types/FileType';
 import type { JsonObject, JsonType } from '../../types/JsonType';
 import type { SampledValueTemplate } from '../../types/MeasurandPerPhaseSampledValueTemplates';
 import type { OCPP16StatusNotificationRequest } from '../../types/ocpp/1.6/Requests';
@@ -19,6 +22,7 @@ import {
   type StatusNotificationRequest,
 } from '../../types/ocpp/Requests';
 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';
@@ -53,6 +57,8 @@ export class OCPPServiceUtils {
         return 'response';
       case MessageType.CALL_ERROR_MESSAGE:
         return 'error';
+      default:
+        return 'unknown';
     }
   }
 
@@ -162,6 +168,23 @@ export class OCPPServiceUtils {
     }
   }
 
+  protected static parseJsonSchemaFile<T extends JsonType>(
+    filePath: string,
+    ocppVersion: OCPPVersion
+  ): 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),
+        { throwError: false }
+      );
+    }
+  }
+
   protected static getSampledValueTemplate(
     chargingStation: ChargingStation,
     connectorId: number,
@@ -180,7 +203,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 '${
@@ -190,7 +213,7 @@ 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;
@@ -212,7 +235,7 @@ export class OCPPServiceUtils {
         ChargingStationConfigurationUtils.getConfigurationKey(
           chargingStation,
           StandardParametersKey.MeterValuesSampledData
-        )?.value.includes(measurand) === true
+        )?.value?.includes(measurand) === true
       ) {
         return sampledValueTemplates[index];
       } else if (
@@ -222,7 +245,7 @@ export class OCPPServiceUtils {
         ChargingStationConfigurationUtils.getConfigurationKey(
           chargingStation,
           StandardParametersKey.MeterValuesSampledData
-        )?.value.includes(measurand) === true
+        )?.value?.includes(measurand) === true
       ) {
         return sampledValueTemplates[index];
       } else if (
@@ -259,4 +282,8 @@ export class OCPPServiceUtils {
       ? Math.min(numberValue * options.unitMultiplier, limit)
       : numberValue * options.unitMultiplier;
   }
+
+  private static logPrefix = (ocppVersion: OCPPVersion): string => {
+    return Utils.logPrefix(` OCPP ${ocppVersion} |`);
+  };
 }