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'
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<Worker>,
workerOptions: { resourceLimits: workerConfiguration.resourceLimits }
}
}
)
// 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 {
}
}
- private async onClose (code: WebSocketCloseEventStatusCode, reason: Buffer): Promise<void> {
+ private onClose (code: WebSocketCloseEventStatusCode, reason: Buffer): void {
switch (code) {
// Normal close
case WebSocketCloseEventStatusCode.CLOSE_NORMAL:
code
)}' and reason '${reason.toString()}'`
)
- this.started && (await this.reconnect())
+ this.started && this.reconnect().catch(Constants.EMPTY_FUNCTION)
break
}
this.emit(ChargingStationEvents.updated)
this.onmessageerror = this.messageErrorHandler.bind(this) as (message: unknown) => void
}
- private async requestHandler (messageEvent: MessageEvent): Promise<void> {
+ private requestHandler (messageEvent: MessageEvent): void {
const validatedMessageEvent = this.validateMessageEvent(messageEvent)
if (validatedMessageEvent === false) {
return
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 {
)
]
])
- this.validatePayload = this.validatePayload.bind(this) as (
- chargingStation: ChargingStation,
- commandName: OCPP16IncomingRequestCommand,
- commandPayload: JsonType
- ) => boolean
+ this.validatePayload = this.validatePayload.bind(this)
}
public async incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
)
]
])
- this.buildRequestPayload = this.buildRequestPayload.bind(this) as <Request extends JsonType>(
- chargingStation: ChargingStation,
- commandName: OCPP16RequestCommand,
- commandParams?: JsonType
- ) => Request
+ this.buildRequestPayload = this.buildRequestPayload.bind(this)
}
public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
)
]
])
- this.validatePayload = this.validatePayload.bind(this) as (
- chargingStation: ChargingStation,
- commandName: OCPP16RequestCommand,
- payload: JsonType
- ) => boolean
+ this.validatePayload = this.validatePayload.bind(this)
}
public async responseHandler<ReqType extends JsonType, ResType extends JsonType>(
)
]
])
- this.validatePayload = this.validatePayload.bind(this) as (
- chargingStation: ChargingStation,
- commandName: OCPP20IncomingRequestCommand,
- commandPayload: JsonType
- ) => boolean
+ this.validatePayload = this.validatePayload.bind(this)
}
public async incomingRequestHandler<ReqType extends JsonType, ResType extends JsonType>(
)
]
])
- this.buildRequestPayload = this.buildRequestPayload.bind(this) as <Request extends JsonType>(
- chargingStation: ChargingStation,
- commandName: OCPP20RequestCommand,
- commandParams?: JsonType
- ) => Request
+ this.buildRequestPayload = this.buildRequestPayload.bind(this)
}
public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
)
]
])
- this.validatePayload = this.validatePayload.bind(this) as (
- chargingStation: ChargingStation,
- commandName: OCPP20RequestCommand,
- payload: JsonType
- ) => boolean
+ this.validatePayload = this.validatePayload.bind(this)
}
public async responseHandler<ReqType extends JsonType, ResType extends JsonType>(
})
ajvFormats(this.ajv)
this.jsonValidateFunctions = new Map<IncomingRequestCommand, ValidateFunction<JsonType>>()
- 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<void>
- this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as <
- T extends JsonType
- >(
- chargingStation: ChargingStation,
- commandName: IncomingRequestCommand,
- schema: JSONSchemaType<T>,
- payload: T
- ) => boolean
+ this.incomingRequestHandler = this.incomingRequestHandler.bind(this)
+ this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this)
}
public static getInstance<T extends OCPPIncomingRequestService>(this: new () => T): T {
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>(
IncomingRequestCommand,
ValidateFunction<JsonType>
>()
- this.responseHandler = this.responseHandler.bind(this) as <
- ReqType extends JsonType,
- ResType extends JsonType
- >(
- chargingStation: ChargingStation,
- commandName: RequestCommand,
- payload: ResType,
- requestPayload: ReqType
- ) => Promise<void>
- this.validateResponsePayload = this.validateResponsePayload.bind(this) as <T extends JsonType>(
- chargingStation: ChargingStation,
- commandName: RequestCommand,
- schema: JSONSchemaType<T>,
- payload: T
- ) => boolean
+ this.responseHandler = this.responseHandler.bind(this)
+ this.validateResponsePayload = this.validateResponsePayload.bind(this)
}
public static getInstance<T extends OCPPResponseService>(this: new () => T): T {
-import type { IncomingMessage, RequestListener, ServerResponse } from 'node:http'
+import type { IncomingMessage, ServerResponse } from 'node:http'
import { StatusCodes } from 'http-status-codes'
}
public start (): void {
- this.httpServer.on('request', this.requestListener.bind(this) as RequestListener)
+ this.httpServer.on('request', this.requestListener.bind(this))
this.startHttpServer()
}