refactor: silence typing errors
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index e98ebdfa6ab4384eb0b7ee2683cf7cdf3302919b..3a27a1923d7a0234efed3c223337e7e26ba2347f 100644 (file)
@@ -1,9 +1,6 @@
-import _Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv'
+import _Ajv, { type ValidateFunction } from 'ajv'
 import _ajvFormats from 'ajv-formats'
 
-import { OCPPConstants } from './OCPPConstants.js'
-import type { OCPPResponseService } from './OCPPResponseService.js'
-import { OCPPServiceUtils } from './OCPPServiceUtils.js'
 import type { ChargingStation } from '../../charging-station/index.js'
 import { OCPPError } from '../../exception/index.js'
 import { PerformanceStatistics } from '../../performance/index.js'
@@ -24,12 +21,14 @@ import {
   type ResponseType
 } from '../../types/index.js'
 import {
-  cloneObject,
+  clone,
   formatDurationMilliSeconds,
   handleSendMessageError,
-  isNullOrUndefined,
   logger
 } from '../../utils/index.js'
+import { OCPPConstants } from './OCPPConstants.js'
+import type { OCPPResponseService } from './OCPPResponseService.js'
+import { OCPPServiceUtils } from './OCPPServiceUtils.js'
 type Ajv = _Ajv.default
 // eslint-disable-next-line @typescript-eslint/no-redeclare
 const Ajv = _Ajv.default
@@ -46,10 +45,9 @@ const defaultRequestParams: RequestParams = {
 export abstract class OCPPRequestService {
   private static instance: OCPPRequestService | null = null
   private readonly version: OCPPVersion
-  private readonly ajv: Ajv
   private readonly ocppResponseService: OCPPResponseService
-  private readonly jsonValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>>
-  protected abstract jsonSchemas: Map<RequestCommand, JSONSchemaType<JsonType>>
+  protected readonly ajv: Ajv
+  protected abstract payloadValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>>
 
   protected constructor (version: OCPPVersion, ocppResponseService: OCPPResponseService) {
     this.version = version
@@ -58,64 +56,16 @@ export abstract class OCPPRequestService {
       multipleOfPrecision: 2
     })
     ajvFormats(this.ajv)
-    this.jsonValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonType>>()
     this.ocppResponseService = ocppResponseService
-    this.requestHandler = this.requestHandler.bind(this) as <
-      // eslint-disable-next-line @typescript-eslint/no-unused-vars
-      ReqType extends JsonType,
-      ResType extends JsonType,
-    >(
-      chargingStation: ChargingStation,
-      commandName: RequestCommand,
-      commandParams?: JsonType,
-      params?: RequestParams
-    ) => Promise<ResType>
-    this.sendMessage = this.sendMessage.bind(this) as (
-      chargingStation: ChargingStation,
-      messageId: string,
-      messagePayload: JsonType,
-      commandName: RequestCommand,
-      params?: RequestParams
-    ) => Promise<ResponseType>
-    this.sendResponse = this.sendResponse.bind(this) as (
-      chargingStation: ChargingStation,
-      messageId: string,
-      messagePayload: JsonType,
-      commandName: IncomingRequestCommand
-    ) => Promise<ResponseType>
-    this.sendError = this.sendError.bind(this) as (
-      chargingStation: ChargingStation,
-      messageId: string,
-      ocppError: OCPPError,
-      commandName: RequestCommand | IncomingRequestCommand
-    ) => Promise<ResponseType>
-    this.internalSendMessage = this.internalSendMessage.bind(this) as (
-      chargingStation: ChargingStation,
-      messageId: string,
-      messagePayload: JsonType | OCPPError,
-      messageType: MessageType,
-      commandName: RequestCommand | IncomingRequestCommand,
-      params?: RequestParams
-    ) => Promise<ResponseType>
-    this.buildMessageToSend = this.buildMessageToSend.bind(this) as (
-      chargingStation: ChargingStation,
-      messageId: string,
-      messagePayload: JsonType | OCPPError,
-      messageType: MessageType,
-      commandName: RequestCommand | IncomingRequestCommand
-    ) => string
-    this.validateRequestPayload = this.validateRequestPayload.bind(this) as <T extends JsonType>(
-      chargingStation: ChargingStation,
-      commandName: RequestCommand | IncomingRequestCommand,
-      payload: T
-    ) => boolean
-    this.validateIncomingRequestResponsePayload = this.validateIncomingRequestResponsePayload.bind(
-      this
-    ) as <T extends JsonType>(
-      chargingStation: ChargingStation,
-      commandName: RequestCommand | IncomingRequestCommand,
-      payload: T
-    ) => boolean
+    this.requestHandler = this.requestHandler.bind(this)
+    this.sendMessage = this.sendMessage.bind(this)
+    this.sendResponse = this.sendResponse.bind(this)
+    this.sendError = this.sendError.bind(this)
+    this.internalSendMessage = this.internalSendMessage.bind(this)
+    this.buildMessageToSend = this.buildMessageToSend.bind(this)
+    this.validateRequestPayload = this.validateRequestPayload.bind(this)
+    this.validateIncomingRequestResponsePayload =
+      this.validateIncomingRequestResponsePayload.bind(this)
   }
 
   public static getInstance<T extends OCPPRequestService>(
@@ -208,45 +158,31 @@ export abstract class OCPPRequestService {
     if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
       return true
     }
-    if (!this.jsonSchemas.has(commandName as RequestCommand)) {
+    if (!this.payloadValidateFunctions.has(commandName as RequestCommand)) {
       logger.warn(
         `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: No JSON schema found for command '${commandName}' PDU validation`
       )
       return true
     }
-    const validate = this.getJsonRequestValidateFunction<T>(commandName as RequestCommand)
-    payload = cloneObject<T>(payload)
+    const validate = this.payloadValidateFunctions.get(commandName as RequestCommand)
+    payload = clone<T>(payload)
     OCPPServiceUtils.convertDateToISOString<T>(payload)
-    if (validate(payload)) {
+    if (validate?.(payload) === true) {
       return true
     }
     logger.error(
       `${chargingStation.logPrefix()} ${moduleName}.validateRequestPayload: Command '${commandName}' request PDU is invalid: %j`,
-      validate.errors
+      validate?.errors
     )
     // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
     throw new OCPPError(
-      OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
+      OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors),
       'Request PDU is invalid',
       commandName,
-      JSON.stringify(validate.errors, undefined, 2)
+      JSON.stringify(validate?.errors, undefined, 2)
     )
   }
 
-  private getJsonRequestValidateFunction<T extends JsonType>(
-    commandName: RequestCommand
-  ): ValidateFunction<JsonType> {
-    if (!this.jsonValidateFunctions.has(commandName)) {
-      this.jsonValidateFunctions.set(
-        commandName,
-        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-        this.ajv.compile<T>(this.jsonSchemas.get(commandName)!).bind(this)
-      )
-    }
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    return this.jsonValidateFunctions.get(commandName)!
-  }
-
   private validateIncomingRequestResponsePayload<T extends JsonType>(
     chargingStation: ChargingStation,
     commandName: RequestCommand | IncomingRequestCommand,
@@ -256,52 +192,36 @@ export abstract class OCPPRequestService {
       return true
     }
     if (
-      !this.ocppResponseService.jsonIncomingRequestResponseSchemas.has(
+      !this.ocppResponseService.incomingRequestResponsePayloadValidateFunctions.has(
         commandName as IncomingRequestCommand
       )
     ) {
       logger.warn(
-        `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestResponsePayload: No JSON schema found for command '${commandName}' PDU validation`
+        `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestResponsePayload: No JSON schema validation function found for command '${commandName}' PDU validation`
       )
       return true
     }
-    const validate = this.getJsonRequestResponseValidateFunction<T>(
+    const validate = this.ocppResponseService.incomingRequestResponsePayloadValidateFunctions.get(
       commandName as IncomingRequestCommand
     )
-    payload = cloneObject<T>(payload)
+    payload = clone<T>(payload)
     OCPPServiceUtils.convertDateToISOString<T>(payload)
-    if (validate(payload)) {
+    if (validate?.(payload) === true) {
       return true
     }
     logger.error(
-      `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestResponsePayload: Command '${commandName}' reponse PDU is invalid: %j`,
-      validate.errors
+      `${chargingStation.logPrefix()} ${moduleName}.validateIncomingRequestResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
+      validate?.errors
     )
     // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
     throw new OCPPError(
-      OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
+      OCPPServiceUtils.ajvErrorsToErrorType(validate?.errors),
       'Response PDU is invalid',
       commandName,
-      JSON.stringify(validate.errors, undefined, 2)
+      JSON.stringify(validate?.errors, undefined, 2)
     )
   }
 
-  private getJsonRequestResponseValidateFunction<T extends JsonType>(
-    commandName: IncomingRequestCommand
-  ): ValidateFunction<JsonType> {
-    if (!this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.has(commandName)) {
-      this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.set(
-        commandName,
-        this.ajv
-          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-          .compile<T>(this.ocppResponseService.jsonIncomingRequestResponseSchemas.get(commandName)!)
-          .bind(this)
-      )
-    }
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    return this.ocppResponseService.jsonIncomingRequestResponseValidateFunctions.get(commandName)!
-  }
-
   private async internalSendMessage (
     chargingStation: ChargingStation,
     messageId: string,
@@ -325,7 +245,7 @@ export abstract class OCPPRequestService {
       // eslint-disable-next-line @typescript-eslint/no-this-alias
       const self = this
       // Send a message through wsConnection
-      return await new Promise<ResponseType>((resolve, reject) => {
+      return await new Promise<ResponseType>((resolve, reject: (reason?: unknown) => void) => {
         /**
          * Function that will receive the request's response
          *
@@ -383,11 +303,11 @@ export abstract class OCPPRequestService {
         }
 
         const handleSendError = (ocppError: OCPPError): void => {
-          if (params?.skipBufferingOnError === false) {
+          if (params.skipBufferingOnError === false) {
             // Buffer
             chargingStation.bufferMessage(messageToSend)
             if (messageType === MessageType.CALL_MESSAGE) {
-              this.cacheRequestPromise(
+              this.setCachedRequest(
                 chargingStation,
                 messageId,
                 messagePayload as JsonType,
@@ -397,7 +317,7 @@ export abstract class OCPPRequestService {
               )
             }
           } else if (
-            params?.skipBufferingOnError === true &&
+            params.skipBufferingOnError === true &&
             messageType === MessageType.CALL_MESSAGE
           ) {
             // Remove request from the cache
@@ -426,7 +346,7 @@ export abstract class OCPPRequestService {
                 `Timeout ${formatDurationMilliSeconds(
                   OCPPConstants.OCPP_WEBSOCKET_TIMEOUT
                 )} reached for ${
-                  params?.skipBufferingOnError === false ? '' : 'non '
+                  params.skipBufferingOnError === false ? '' : 'non '
                 }buffered message id '${messageId}' with content '${messageToSend}'`,
                 commandName,
                 (messagePayload as OCPPError).details
@@ -436,14 +356,14 @@ export abstract class OCPPRequestService {
           chargingStation.wsConnection?.send(messageToSend, (error?: Error) => {
             PerformanceStatistics.endMeasure(commandName, beginId)
             clearTimeout(sendTimeout)
-            if (isNullOrUndefined(error)) {
+            if (error == null) {
               logger.debug(
                 `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${OCPPServiceUtils.getMessageTypeString(
                   messageType
                 )} payload: ${messageToSend}`
               )
               if (messageType === MessageType.CALL_MESSAGE) {
-                this.cacheRequestPromise(
+                this.setCachedRequest(
                   chargingStation,
                   messageId,
                   messagePayload as JsonType,
@@ -455,12 +375,12 @@ export abstract class OCPPRequestService {
                 // Resolve response
                 resolve(messagePayload)
               }
-            } else if (error != null) {
+            } else {
               handleSendError(
                 new OCPPError(
                   ErrorType.GENERIC_ERROR,
                   `WebSocket errored for ${
-                    params?.skipBufferingOnError === false ? '' : 'non '
+                    params.skipBufferingOnError === false ? '' : 'non '
                   }buffered message id '${messageId}' with content '${messageToSend}'`,
                   commandName,
                   { name: error.name, message: error.message, stack: error.stack }
@@ -473,7 +393,7 @@ export abstract class OCPPRequestService {
             new OCPPError(
               ErrorType.GENERIC_ERROR,
               `WebSocket closed for ${
-                params?.skipBufferingOnError === false ? '' : 'non '
+                params.skipBufferingOnError === false ? '' : 'non '
               }buffered message id '${messageId}' with content '${messageToSend}'`,
               commandName,
               (messagePayload as OCPPError).details
@@ -484,7 +404,7 @@ export abstract class OCPPRequestService {
     }
     throw new OCPPError(
       ErrorType.SECURITY_ERROR,
-      `Cannot send command ${commandName} PDU when the charging station is in ${chargingStation?.bootNotificationResponse?.status} state on the central server`,
+      `Cannot send command ${commandName} PDU when the charging station is in ${chargingStation.bootNotificationResponse?.status} state on the central server`,
       commandName
     )
   }
@@ -506,9 +426,9 @@ export abstract class OCPPRequestService {
         messageToSend = JSON.stringify([
           messageType,
           messageId,
-          commandName,
-          messagePayload
-        ] as OutgoingRequest)
+          commandName as RequestCommand,
+          messagePayload as JsonType
+        ] satisfies OutgoingRequest)
         break
       // Response
       case MessageType.CALL_RESULT_MESSAGE:
@@ -518,7 +438,11 @@ export abstract class OCPPRequestService {
           commandName,
           messagePayload as JsonType
         )
-        messageToSend = JSON.stringify([messageType, messageId, messagePayload] as Response)
+        messageToSend = JSON.stringify([
+          messageType,
+          messageId,
+          messagePayload as JsonType
+        ] satisfies Response)
         break
       // Error Message
       case MessageType.CALL_ERROR_MESSAGE:
@@ -529,15 +453,15 @@ export abstract class OCPPRequestService {
           (messagePayload as OCPPError).code,
           (messagePayload as OCPPError).message,
           (messagePayload as OCPPError).details ?? {
-            command: (messagePayload as OCPPError).command ?? commandName
+            command: (messagePayload as OCPPError).command
           }
-        ] as ErrorResponse)
+        ] satisfies ErrorResponse)
         break
     }
     return messageToSend
   }
 
-  private cacheRequestPromise (
+  private setCachedRequest (
     chargingStation: ChargingStation,
     messageId: string,
     messagePayload: JsonType,
@@ -553,12 +477,10 @@ export abstract class OCPPRequestService {
     ])
   }
 
-  // eslint-disable-next-line @typescript-eslint/no-unused-vars
   public abstract requestHandler<ReqType extends JsonType, ResType extends JsonType>(
     chargingStation: ChargingStation,
     commandName: RequestCommand,
-    // FIXME: should be ReqType
-    commandParams?: JsonType,
+    commandParams?: ReqType,
     params?: RequestParams
   ): Promise<ResType>
 }