refactor(simulator): remove unneeded type casting
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16RequestService.ts
index 15b2f2dd64e8ba130d8a31268d04ff82fa501f19..1d571b042dfdedecab3add913269c88171714926 100644 (file)
@@ -1,42 +1,38 @@
 // 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 { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
-import OCPPError from '../../../exception/OCPPError';
-import type { JsonObject, JsonType } from '../../../types/JsonType';
-import type { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues';
+import type { ChargingStation } from '../../../charging-station';
+import { OCPPError } from '../../../exception';
 import {
+  ErrorType,
+  type JsonObject,
+  type JsonType,
+  type OCPP16AuthorizeRequest,
   type OCPP16BootNotificationRequest,
   type OCPP16DataTransferRequest,
   type OCPP16DiagnosticsStatusNotificationRequest,
   type OCPP16FirmwareStatusNotificationRequest,
   type OCPP16HeartbeatRequest,
+  type OCPP16MeterValuesRequest,
   OCPP16RequestCommand,
+  type OCPP16StartTransactionRequest,
   type OCPP16StatusNotificationRequest,
-} from '../../../types/ocpp/1.6/Requests';
-import type {
-  OCPP16AuthorizeRequest,
-  OCPP16StartTransactionRequest,
-  OCPP16StopTransactionRequest,
-} from '../../../types/ocpp/1.6/Transaction';
-import { ErrorType } from '../../../types/ocpp/ErrorType';
-import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
-import type { RequestParams } from '../../../types/ocpp/Requests';
-import Constants from '../../../utils/Constants';
-import Utils from '../../../utils/Utils';
-import type ChargingStation from '../../ChargingStation';
-import OCPPConstants from '../OCPPConstants';
-import OCPPRequestService from '../OCPPRequestService';
-import type OCPPResponseService from '../OCPPResponseService';
+  type OCPP16StopTransactionRequest,
+  OCPPVersion,
+  type RequestParams,
+} from '../../../types';
+import { Constants, Utils } from '../../../utils';
+import {
+  OCPP16ServiceUtils,
+  OCPPConstants,
+  OCPPRequestService,
+  type OCPPResponseService,
+} from '../internal';
 
 const moduleName = 'OCPP16RequestService';
 
-export default class OCPP16RequestService extends OCPPRequestService {
+export class OCPP16RequestService extends OCPPRequestService {
   protected jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>;
 
   public constructor(ocppResponseService: OCPPResponseService) {
@@ -47,62 +43,82 @@ export default class OCPP16RequestService extends OCPPRequestService {
     this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([
       [
         OCPP16RequestCommand.AUTHORIZE,
-        this.parseJsonSchemaFile<OCPP16AuthorizeRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/Authorize.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16AuthorizeRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/Authorize.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.BOOT_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16BootNotificationRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/BootNotification.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16BootNotificationRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/BootNotification.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DiagnosticsStatusNotificationRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotification.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.HEARTBEAT,
-        this.parseJsonSchemaFile<OCPP16HeartbeatRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16HeartbeatRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/Heartbeat.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.METER_VALUES,
-        this.parseJsonSchemaFile<OCPP16MeterValuesRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/MeterValues.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16MeterValuesRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/MeterValues.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16StatusNotificationRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StatusNotificationRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/StatusNotification.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.START_TRANSACTION,
-        this.parseJsonSchemaFile<OCPP16StartTransactionRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StartTransactionRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/StartTransaction.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.STOP_TRANSACTION,
-        this.parseJsonSchemaFile<OCPP16StopTransactionRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16StopTransactionRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/StopTransaction.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.DATA_TRANSFER,
-        this.parseJsonSchemaFile<OCPP16DataTransferRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16DataTransferRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json',
+          moduleName,
+          'constructor'
         ),
       ],
       [
         OCPP16RequestCommand.FIRMWARE_STATUS_NOTIFICATION,
-        this.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationRequest>(
-          '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json'
+        OCPP16ServiceUtils.parseJsonSchemaFile<OCPP16FirmwareStatusNotificationRequest>(
+          '../../../assets/json-schemas/ocpp/1.6/FirmwareStatusNotification.json',
+          moduleName,
+          'constructor'
         ),
       ],
     ]);
@@ -122,7 +138,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
         this.buildRequestPayload<RequestType>(chargingStation, commandName, commandParams),
         commandName,
         params
-      )) as unknown as ResponseType;
+      )) as ResponseType;
     }
     // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
     throw new OCPPError(
@@ -202,13 +218,4 @@ export default class OCPP16RequestService extends OCPPRequestService {
         );
     }
   }
-
-  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>;
-  }
 }