feat: add template example with evses configuration
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16RequestService.ts
index 9979c70aa8101f41facf955a637b5d3fc2bd5b52..3bc73aaabb38d983f8fce8fd21ac4e743741c0be 100644 (file)
 // 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 {
+  OCPP16Constants,
+  OCPP16ServiceUtils,
+  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) {
-    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_16, ocppResponseService);
     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'
         ),
       ],
     ]);
-    this.buildRequestPayload.bind(this);
+    this.buildRequestPayload = this.buildRequestPayload.bind(this) as <Request extends JsonType>(
+      chargingStation: ChargingStation,
+      commandName: OCPP16RequestCommand,
+      commandParams?: JsonType
+    ) => Request;
   }
 
   public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
@@ -115,19 +135,15 @@ export default class OCPP16RequestService extends OCPPRequestService {
     commandParams?: JsonType,
     params?: RequestParams
   ): Promise<ResponseType> {
+    // FIXME?: add sanity checks on charging station availability, connector availability, connector status, etc.
     if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) {
-      const requestPayload = this.buildRequestPayload<RequestType>(
-        chargingStation,
-        commandName,
-        commandParams
-      );
       return (await this.sendMessage(
         chargingStation,
         Utils.generateUUID(),
-        requestPayload,
+        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(
@@ -160,12 +176,13 @@ export default class OCPP16RequestService extends OCPPRequestService {
           ...commandParams,
         } as unknown as Request;
       case OCPP16RequestCommand.HEARTBEAT:
-        return OCPPConstants.OCPP_REQUEST_EMPTY as unknown as Request;
+        return OCPP16Constants.OCPP_REQUEST_EMPTY as unknown as Request;
       case OCPP16RequestCommand.START_TRANSACTION:
         return {
           idTag: Constants.DEFAULT_IDTAG,
           meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId(
-            commandParams?.connectorId as number
+            commandParams?.connectorId as number,
+            true
           ),
           timestamp: new Date(),
           ...commandParams,
@@ -206,13 +223,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>;
-  }
 }