refactor(simulator): permit to check if connector status transition is
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / 1.6 / OCPP16RequestService.ts
index 98cf531723be791795bd5e1698b3489e4f86bf14..7158e5348251218e3325b4028dcd94fa88762d2e 100644 (file)
@@ -2,43 +2,43 @@
 
 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) {
-    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>>([
       [
@@ -122,7 +122,11 @@ export default class OCPP16RequestService extends OCPPRequestService {
         ),
       ],
     ]);
-    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>(
@@ -131,6 +135,7 @@ 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) {
       return (await this.sendMessage(
         chargingStation,
@@ -138,7 +143,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(