refactor: trivial cleanups
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 2.0 / OCPP20ResponseService.ts
index f9d8fde9feb7d39d944bb1f5d38cf27b1f474f42..50566300231ddfe3347870f3234737d64d31df9e 100644 (file)
@@ -1,34 +1,31 @@
 // Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
 
-import fs from 'fs';
-import path from 'path';
-import { fileURLToPath } from 'url';
-
 import type { JSONSchemaType } from 'ajv';
 
-import OCPPError from '../../../exception/OCPPError';
-import type { JsonObject, JsonType } from '../../../types/JsonType';
+import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
+import { type ChargingStation, ChargingStationConfigurationUtils } from '../../../charging-station';
+import { OCPPError } from '../../../exception';
 import {
+  ErrorType,
+  type JsonObject,
+  type JsonType,
+  type OCPP20BootNotificationResponse,
+  type OCPP20ClearCacheResponse,
+  type OCPP20HeartbeatResponse,
   OCPP20IncomingRequestCommand,
+  OCPP20OptionalVariableName,
   OCPP20RequestCommand,
-} from '../../../types/ocpp/2.0/Requests';
-import type {
-  OCPP20BootNotificationResponse,
-  OCPP20ClearCacheResponse,
-  OCPP20HeartbeatResponse,
-  OCPP20StatusNotificationResponse,
-} from '../../../types/ocpp/2.0/Responses';
-import { ErrorType } from '../../../types/ocpp/ErrorType';
-import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
-import { RegistrationStatusEnumType, ResponseHandler } from '../../../types/ocpp/Responses';
-import logger from '../../../utils/Logger';
-import type ChargingStation from '../../ChargingStation';
-import OCPPResponseService from '../OCPPResponseService';
-import { OCPP20ServiceUtils } from './OCPP20ServiceUtils';
+  type OCPP20StatusNotificationResponse,
+  OCPPVersion,
+  RegistrationStatusEnumType,
+  type ResponseHandler,
+} from '../../../types';
+import { logger } from '../../../utils';
+import { OCPPResponseService } from '../OCPPResponseService';
 
 const moduleName = 'OCPP20ResponseService';
 
-export default class OCPP20ResponseService extends OCPPResponseService {
+export class OCPP20ResponseService extends OCPPResponseService {
   public jsonIncomingRequestResponseSchemas: Map<
     OCPP20IncomingRequestCommand,
     JSONSchemaType<JsonObject>
@@ -38,9 +35,9 @@ export default class OCPP20ResponseService extends OCPPResponseService {
   private jsonSchemas: Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>;
 
   public constructor() {
-    if (new.target?.name === moduleName) {
-      throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
-    }
+    // if (new.target?.name === moduleName) {
+    //   throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
+    // }
     super(OCPPVersion.VERSION_20);
     this.responseHandlers = new Map<OCPP20RequestCommand, ResponseHandler>([
       [OCPP20RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)],
@@ -50,32 +47,44 @@ export default class OCPP20ResponseService extends OCPPResponseService {
     this.jsonSchemas = new Map<OCPP20RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP20RequestCommand.BOOT_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP20BootNotificationResponse>(
-          '../../../assets/json-schemas/ocpp/2.0/BootNotificationResponse.json'
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationResponse>(
+          'assets/json-schemas/ocpp/2.0/BootNotificationResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP20RequestCommand.HEARTBEAT,
-        this.parseJsonSchemaFile<OCPP20HeartbeatResponse>(
-          '../../../assets/json-schemas/ocpp/2.0/HeartbeatResponse.json'
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatResponse>(
+          'assets/json-schemas/ocpp/2.0/HeartbeatResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP20RequestCommand.STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP20StatusNotificationResponse>(
-          '../../../assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json'
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationResponse>(
+          'assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
     ]);
     this.jsonIncomingRequestResponseSchemas = new Map([
       [
         OCPP20IncomingRequestCommand.CLEAR_CACHE,
-        this.parseJsonSchemaFile<OCPP20ClearCacheResponse>(
-          '../../../assets/json-schemas/ocpp/2.0/ClearCacheResponse.json'
+        OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheResponse>(
+          'assets/json-schemas/ocpp/2.0/ClearCacheResponse.json',
+          moduleName,
+          'constructor'
         ),
       ],
     ]);
-    this.validatePayload.bind(this);
+    this.validatePayload = this.validatePayload.bind(this) as (
+      chargingStation: ChargingStation,
+      commandName: OCPP20RequestCommand,
+      payload: JsonType
+    ) => boolean;
   }
 
   public async responseHandler(
@@ -153,23 +162,14 @@ export default class OCPP20ResponseService extends OCPPResponseService {
     payload: OCPP20BootNotificationResponse
   ): void {
     if (payload.status === RegistrationStatusEnumType.ACCEPTED) {
-      // ChargingStationConfigurationUtils.addConfigurationKey(
-      //   chargingStation,
-      //   OCPP16StandardParametersKey.HeartbeatInterval,
-      //   payload.interval.toString(),
-      //   {},
-      //   { overwrite: true, save: true }
-      // );
-      // ChargingStationConfigurationUtils.addConfigurationKey(
-      //   chargingStation,
-      //   OCPP16StandardParametersKey.HeartBeatInterval,
-      //   payload.interval.toString(),
-      //   { visible: false },
-      //   { overwrite: true, save: true }
-      // );
-      chargingStation.heartbeatSetInterval
-        ? chargingStation.restartHeartbeat()
-        : chargingStation.startHeartbeat();
+      ChargingStationConfigurationUtils.addConfigurationKey(
+        chargingStation,
+        OCPP20OptionalVariableName.HeartbeatInterval,
+        payload.interval.toString(),
+        {},
+        { overwrite: true, save: true }
+      );
+      OCPP20ServiceUtils.startHeartbeatInterval(chargingStation, payload.interval);
     }
     if (Object.values(RegistrationStatusEnumType).includes(payload.status)) {
       const logMsg = `${chargingStation.logPrefix()} Charging station in '${
@@ -180,19 +180,9 @@ export default class OCPP20ResponseService extends OCPPResponseService {
         : logger.info(logMsg);
     } else {
       logger.error(
-        chargingStation.logPrefix() +
-          ' Charging station boot notification response received: %j with undefined registration status',
+        `${chargingStation.logPrefix()} Charging station boot notification response received: %j with undefined registration status`,
         payload
       );
     }
   }
-
-  private parseJsonSchemaFile<T extends JsonType>(relativePath: string): JSONSchemaType<T> {
-    return JSON.parse(
-      fs.readFileSync(
-        path.resolve(path.dirname(fileURLToPath(import.meta.url)), relativePath),
-        'utf8'
-      )
-    ) as JSONSchemaType<T>;
-  }
 }