From: Jérôme Benoit Date: Sat, 20 Jan 2024 20:09:22 +0000 (+0100) Subject: refactor: cleanup unneeded type casting X-Git-Tag: v1.2.33~44 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ba9a56a613727d96757690a8b52af6731f3fd8a8;p=e-mobility-charging-stations-simulator.git refactor: cleanup unneeded type casting Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 6ee60686..f09864d4 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -4,9 +4,10 @@ import { EventEmitter } from 'node:events' import { dirname, extname, join } from 'node:path' import process, { exit } from 'node:process' import { fileURLToPath } from 'node:url' +import type { Worker } from 'worker_threads' import chalk from 'chalk' -import { availableParallelism } from 'poolifier' +import { type MessageHandler, availableParallelism } from 'poolifier' import { waitChargingStationEvents } from './Helpers.js' import type { AbstractUIServer } from './ui-server/AbstractUIServer.js' @@ -274,7 +275,7 @@ export class Bootstrap extends EventEmitter { poolMinSize: workerConfiguration.poolMinSize!, elementsPerWorker: elementsPerWorker ?? (workerConfiguration.elementsPerWorker as number), poolOptions: { - messageHandler: this.messageHandler.bind(this) as (message: unknown) => void, + messageHandler: this.messageHandler.bind(this) as MessageHandler, workerOptions: { resourceLimits: workerConfiguration.resourceLimits } } } diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 79d2c753..4656444f 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -768,26 +768,21 @@ export class ChargingStation extends EventEmitter { ) // Handle WebSocket message - this.wsConnection.on( - 'message', - this.onMessage.bind(this) as (this: WebSocket, data: RawData, isBinary: boolean) => void - ) + // FIXME + // eslint-disable-next-line @typescript-eslint/no-misused-promises + this.wsConnection.on('message', this.onMessage.bind(this)) // Handle WebSocket error - this.wsConnection.on( - 'error', - this.onError.bind(this) as (this: WebSocket, error: Error) => void - ) + this.wsConnection.on('error', this.onError.bind(this)) // Handle WebSocket close - this.wsConnection.on( - 'close', - this.onClose.bind(this) as (this: WebSocket, code: number, reason: Buffer) => void - ) + this.wsConnection.on('close', this.onClose.bind(this)) // Handle WebSocket open - this.wsConnection.on('open', this.onOpen.bind(this) as (this: WebSocket) => void) + // FIXME + // eslint-disable-next-line @typescript-eslint/no-misused-promises + this.wsConnection.on('open', this.onOpen.bind(this)) // Handle WebSocket ping - this.wsConnection.on('ping', this.onPing.bind(this) as (this: WebSocket, data: Buffer) => void) + this.wsConnection.on('ping', this.onPing.bind(this)) // Handle WebSocket pong - this.wsConnection.on('pong', this.onPong.bind(this) as (this: WebSocket, data: Buffer) => void) + this.wsConnection.on('pong', this.onPong.bind(this)) } public closeWSConnection (): void { @@ -1805,7 +1800,7 @@ export class ChargingStation extends EventEmitter { } } - private async onClose (code: WebSocketCloseEventStatusCode, reason: Buffer): Promise { + private onClose (code: WebSocketCloseEventStatusCode, reason: Buffer): void { switch (code) { // Normal close case WebSocketCloseEventStatusCode.CLOSE_NORMAL: @@ -1824,7 +1819,7 @@ export class ChargingStation extends EventEmitter { code )}' and reason '${reason.toString()}'` ) - this.started && (await this.reconnect()) + this.started && this.reconnect().catch(Constants.EMPTY_FUNCTION) break } this.emit(ChargingStationEvents.updated) diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index 20f347fc..d85430f4 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -256,7 +256,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void } - private async requestHandler (messageEvent: MessageEvent): Promise { + private requestHandler (messageEvent: MessageEvent): void { const validatedMessageEvent = this.validateMessageEvent(messageEvent) if (validatedMessageEvent === false) { return @@ -281,40 +281,42 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne let responsePayload: BroadcastChannelResponsePayload | undefined // eslint-disable-next-line @typescript-eslint/no-invalid-void-type let commandResponse: CommandResponse | void - try { - commandResponse = await this.commandHandler(command, requestPayload) - if (commandResponse == null || isEmptyObject(commandResponse)) { + this.commandHandler(command, requestPayload) + .then(commandResponse => { + if (commandResponse == null || isEmptyObject(commandResponse)) { + responsePayload = { + hashId: this.chargingStation.stationInfo?.hashId, + status: ResponseStatus.SUCCESS + } + } else { + responsePayload = this.commandResponseToResponsePayload( + command, + requestPayload, + commandResponse + ) + } + }) + .catch(error => { + logger.error( + `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`, + error + ) responsePayload = { hashId: this.chargingStation.stationInfo?.hashId, - status: ResponseStatus.SUCCESS - } - } else { - responsePayload = this.commandResponseToResponsePayload( + status: ResponseStatus.FAILURE, command, requestPayload, - commandResponse - ) - } - } catch (error) { - logger.error( - `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`, - error - ) - responsePayload = { - hashId: this.chargingStation.stationInfo?.hashId, - status: ResponseStatus.FAILURE, - command, - requestPayload, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + commandResponse: commandResponse!, + errorMessage: (error as OCPPError).message, + errorStack: (error as OCPPError).stack, + errorDetails: (error as OCPPError).details + } + }) + .finally(() => { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - commandResponse: commandResponse!, - errorMessage: (error as OCPPError).message, - errorStack: (error as OCPPError).stack, - errorDetails: (error as OCPPError).details - } - } finally { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.sendResponse([uuid, responsePayload!]) - } + this.sendResponse([uuid, responsePayload!]) + }) } private messageErrorHandler (messageEvent: MessageEvent): void { diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index a61dedd8..f934d418 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -332,11 +332,7 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ) ] ]) - this.validatePayload = this.validatePayload.bind(this) as ( - chargingStation: ChargingStation, - commandName: OCPP16IncomingRequestCommand, - commandPayload: JsonType - ) => boolean + this.validatePayload = this.validatePayload.bind(this) } public async incomingRequestHandler( diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 297349bd..37a5b2ba 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -121,11 +121,7 @@ export class OCPP16RequestService extends OCPPRequestService { ) ] ]) - this.buildRequestPayload = this.buildRequestPayload.bind(this) as ( - chargingStation: ChargingStation, - commandName: OCPP16RequestCommand, - commandParams?: JsonType - ) => Request + this.buildRequestPayload = this.buildRequestPayload.bind(this) } public async requestHandler( diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 5ceeb38a..c47d2630 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -317,11 +317,7 @@ export class OCPP16ResponseService extends OCPPResponseService { ) ] ]) - this.validatePayload = this.validatePayload.bind(this) as ( - chargingStation: ChargingStation, - commandName: OCPP16RequestCommand, - payload: JsonType - ) => boolean + this.validatePayload = this.validatePayload.bind(this) } public async responseHandler( diff --git a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts index 5480b5a3..66418981 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -43,11 +43,7 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { ) ] ]) - this.validatePayload = this.validatePayload.bind(this) as ( - chargingStation: ChargingStation, - commandName: OCPP20IncomingRequestCommand, - commandPayload: JsonType - ) => boolean + this.validatePayload = this.validatePayload.bind(this) } public async incomingRequestHandler( diff --git a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts index 769064b9..e73b33cc 100644 --- a/src/charging-station/ocpp/2.0/OCPP20RequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20RequestService.ts @@ -57,11 +57,7 @@ export class OCPP20RequestService extends OCPPRequestService { ) ] ]) - this.buildRequestPayload = this.buildRequestPayload.bind(this) as ( - chargingStation: ChargingStation, - commandName: OCPP20RequestCommand, - commandParams?: JsonType - ) => Request + this.buildRequestPayload = this.buildRequestPayload.bind(this) } public async requestHandler( diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index ee4b201f..f35a8898 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -82,11 +82,7 @@ export class OCPP20ResponseService extends OCPPResponseService { ) ] ]) - this.validatePayload = this.validatePayload.bind(this) as ( - chargingStation: ChargingStation, - commandName: OCPP20RequestCommand, - payload: JsonType - ) => boolean + this.validatePayload = this.validatePayload.bind(this) } public async responseHandler( diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 5eaddae9..4285e585 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -35,24 +35,8 @@ export abstract class OCPPIncomingRequestService { }) ajvFormats(this.ajv) this.jsonValidateFunctions = new Map>() - this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as < - ReqType extends JsonType, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - ResType extends JsonType - >( - chargingStation: ChargingStation, - messageId: string, - commandName: IncomingRequestCommand, - commandPayload: ReqType - ) => Promise - this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as < - T extends JsonType - >( - chargingStation: ChargingStation, - commandName: IncomingRequestCommand, - schema: JSONSchemaType, - payload: T - ) => boolean + this.incomingRequestHandler = this.incomingRequestHandler.bind(this) + this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) } public static getInstance(this: new () => T): T { diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index e03feba9..a56f1ac6 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -59,62 +59,15 @@ export abstract class OCPPRequestService { ajvFormats(this.ajv) this.jsonValidateFunctions = new Map>() 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 - this.sendMessage = this.sendMessage.bind(this) as ( - chargingStation: ChargingStation, - messageId: string, - messagePayload: JsonType, - commandName: RequestCommand, - params?: RequestParams - ) => Promise - this.sendResponse = this.sendResponse.bind(this) as ( - chargingStation: ChargingStation, - messageId: string, - messagePayload: JsonType, - commandName: IncomingRequestCommand - ) => Promise - this.sendError = this.sendError.bind(this) as ( - chargingStation: ChargingStation, - messageId: string, - ocppError: OCPPError, - commandName: RequestCommand | IncomingRequestCommand - ) => Promise - this.internalSendMessage = this.internalSendMessage.bind(this) as ( - chargingStation: ChargingStation, - messageId: string, - messagePayload: JsonType | OCPPError, - messageType: MessageType, - commandName: RequestCommand | IncomingRequestCommand, - params?: RequestParams - ) => Promise - 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 ( - chargingStation: ChargingStation, - commandName: RequestCommand | IncomingRequestCommand, - payload: T - ) => boolean - this.validateIncomingRequestResponsePayload = this.validateIncomingRequestResponsePayload.bind( - this - ) as ( - 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( diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index 20cfab6e..e4745089 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -47,21 +47,8 @@ export abstract class OCPPResponseService { IncomingRequestCommand, ValidateFunction >() - this.responseHandler = this.responseHandler.bind(this) as < - ReqType extends JsonType, - ResType extends JsonType - >( - chargingStation: ChargingStation, - commandName: RequestCommand, - payload: ResType, - requestPayload: ReqType - ) => Promise - this.validateResponsePayload = this.validateResponsePayload.bind(this) as ( - chargingStation: ChargingStation, - commandName: RequestCommand, - schema: JSONSchemaType, - payload: T - ) => boolean + this.responseHandler = this.responseHandler.bind(this) + this.validateResponsePayload = this.validateResponsePayload.bind(this) } public static getInstance(this: new () => T): T { diff --git a/src/charging-station/ui-server/UIHttpServer.ts b/src/charging-station/ui-server/UIHttpServer.ts index eb355083..7c102195 100644 --- a/src/charging-station/ui-server/UIHttpServer.ts +++ b/src/charging-station/ui-server/UIHttpServer.ts @@ -1,4 +1,4 @@ -import type { IncomingMessage, RequestListener, ServerResponse } from 'node:http' +import type { IncomingMessage, ServerResponse } from 'node:http' import { StatusCodes } from 'http-status-codes' @@ -33,7 +33,7 @@ export class UIHttpServer extends AbstractUIServer { } public start (): void { - this.httpServer.on('request', this.requestListener.bind(this) as RequestListener) + this.httpServer.on('request', this.requestListener.bind(this)) this.startHttpServer() }