refactor: cleanup imports
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPServiceUtils.ts
index c5387f33f9c693f41ff21f1b20d9f40d7c5cd514..7433f71d27c4278d39f77caa85315fb89b5daefb 100644 (file)
@@ -1,5 +1,5 @@
-import fs from 'node:fs';
-import path from 'node:path';
+import { readFileSync } from 'node:fs';
+import { dirname, join } from 'node:path';
 import { fileURLToPath } from 'node:url';
 
 import type { DefinedError, ErrorObject, JSONSchemaType } from 'ajv';
@@ -30,7 +30,7 @@ import {
   type StatusNotificationRequest,
   type StatusNotificationResponse,
 } from '../../types';
-import { Constants, ErrorUtils, Utils, logger } from '../../utils';
+import { Utils, handleFileException, logger } from '../../utils';
 
 export class OCPPServiceUtils {
   protected constructor() {
@@ -185,17 +185,26 @@ export class OCPPServiceUtils {
     chargingStation: ChargingStation,
     connectorId: number,
     status: ConnectorStatusEnum,
-    evseId?: number
+    evseId?: number,
+    options: { send: boolean } = { send: true }
   ) {
-    OCPPServiceUtils.checkConnectorStatusTransition(chargingStation, connectorId, status);
-    await chargingStation.ocppRequestService.requestHandler<
-      StatusNotificationRequest,
-      StatusNotificationResponse
-    >(
-      chargingStation,
-      RequestCommand.STATUS_NOTIFICATION,
-      OCPPServiceUtils.buildStatusNotificationRequest(chargingStation, connectorId, status, evseId)
-    );
+    options = { send: true, ...options };
+    if (options.send) {
+      OCPPServiceUtils.checkConnectorStatusTransition(chargingStation, connectorId, status);
+      await chargingStation.ocppRequestService.requestHandler<
+        StatusNotificationRequest,
+        StatusNotificationResponse
+      >(
+        chargingStation,
+        RequestCommand.STATUS_NOTIFICATION,
+        OCPPServiceUtils.buildStatusNotificationRequest(
+          chargingStation,
+          connectorId,
+          status,
+          evseId
+        )
+      );
+    }
     chargingStation.getConnectorStatus(connectorId).status = status;
   }
 
@@ -260,11 +269,11 @@ export class OCPPServiceUtils {
     moduleName?: string,
     methodName?: string
   ): JSONSchemaType<T> {
-    const filePath = path.join(path.dirname(fileURLToPath(import.meta.url)), relativePath);
+    const filePath = join(dirname(fileURLToPath(import.meta.url)), relativePath);
     try {
-      return JSON.parse(fs.readFileSync(filePath, 'utf8')) as JSONSchemaType<T>;
+      return JSON.parse(readFileSync(filePath, 'utf8')) as JSONSchemaType<T>;
     } catch (error) {
-      ErrorUtils.handleFileException(
+      handleFileException(
         filePath,
         FileType.JsonSchema,
         error as NodeJS.ErrnoException,
@@ -363,8 +372,13 @@ export class OCPPServiceUtils {
       unitMultiplier: 1,
     }
   ): number {
-    options.limitationEnabled = options?.limitationEnabled ?? true;
-    options.unitMultiplier = options?.unitMultiplier ?? 1;
+    options = {
+      ...{
+        limitationEnabled: true,
+        unitMultiplier: 1,
+      },
+      ...options,
+    };
     const parsedInt = parseInt(value);
     const numberValue = isNaN(parsedInt) ? Infinity : parsedInt;
     return options?.limitationEnabled