From 9429aa42d725c4b42c30e0134d75b363aed412f0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 16 Jul 2023 18:09:44 +0200 Subject: [PATCH] refactor: add type parameter to OCPP stack for request and response MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 10 +++++----- .../ocpp/1.6/OCPP16ResponseService.ts | 6 +++--- .../ocpp/2.0/OCPP20IncomingRequestService.ts | 10 +++++----- .../ocpp/2.0/OCPP20ResponseService.ts | 6 +++--- .../ocpp/OCPPIncomingRequestService.ts | 13 +++++++++---- src/charging-station/ocpp/OCPPResponseService.ts | 15 +++++++++------ 6 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 6e583095..eaa84704 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -305,13 +305,13 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { ) => boolean; } - public async incomingRequestHandler( + public async incomingRequestHandler( chargingStation: ChargingStation, messageId: string, commandName: OCPP16IncomingRequestCommand, - commandPayload: JsonType, + commandPayload: ReqType, ): Promise { - let response: JsonType; + let response: ResType; if ( chargingStation.getOcppStrictCompliance() === true && chargingStation.inPendingState() === true && @@ -341,10 +341,10 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { try { this.validatePayload(chargingStation, commandName, commandPayload); // Call the method to build the response - response = await this.incomingRequestHandlers.get(commandName)!( + response = (await this.incomingRequestHandlers.get(commandName)!( chargingStation, commandPayload, - ); + )) as ResType; } catch (error) { // Log logger.error( diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 8ebce4f8..66f1f6b9 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -314,11 +314,11 @@ export class OCPP16ResponseService extends OCPPResponseService { ) => boolean; } - public async responseHandler( + public async responseHandler( chargingStation: ChargingStation, commandName: OCPP16RequestCommand, - payload: JsonType, - requestPayload: JsonType, + payload: ResType, + requestPayload: ReqType, ): Promise { if ( chargingStation.isRegistered() === true || diff --git a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts index 23ea8c81..bda78d54 100644 --- a/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20IncomingRequestService.ts @@ -48,13 +48,13 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { ) => boolean; } - public async incomingRequestHandler( + public async incomingRequestHandler( chargingStation: ChargingStation, messageId: string, commandName: OCPP20IncomingRequestCommand, - commandPayload: JsonType, + commandPayload: ReqType, ): Promise { - let response: JsonType; + let response: ResType; if ( chargingStation.getOcppStrictCompliance() === true && chargingStation.inPendingState() === true && @@ -84,10 +84,10 @@ export class OCPP20IncomingRequestService extends OCPPIncomingRequestService { try { this.validatePayload(chargingStation, commandName, commandPayload); // Call the method to build the response - response = await this.incomingRequestHandlers.get(commandName)!( + response = (await this.incomingRequestHandlers.get(commandName)!( chargingStation, commandPayload, - ); + )) as ResType; } catch (error) { // Log logger.error( diff --git a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts index 1eec885f..4984b115 100644 --- a/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts +++ b/src/charging-station/ocpp/2.0/OCPP20ResponseService.ts @@ -87,11 +87,11 @@ export class OCPP20ResponseService extends OCPPResponseService { ) => boolean; } - public async responseHandler( + public async responseHandler( chargingStation: ChargingStation, commandName: OCPP20RequestCommand, - payload: JsonType, - requestPayload: JsonType, + payload: ResType, + requestPayload: ReqType, ): Promise { if ( chargingStation.isRegistered() === true || diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 0ea55a63..b7ab8ef6 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -33,11 +33,15 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { multipleOfPrecision: 2, }); ajvFormats(this.ajv); - this.incomingRequestHandler = this.incomingRequestHandler.bind(this) as ( + 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: JsonType, + commandPayload: ReqType, ) => Promise; this.validateIncomingRequestPayload = this.validateIncomingRequestPayload.bind(this) as < T extends JsonType, @@ -110,10 +114,11 @@ export abstract class OCPPIncomingRequestService extends AsyncResource { return OCPPConstants.OCPP_RESPONSE_REJECTED; } - public abstract incomingRequestHandler( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public abstract incomingRequestHandler( chargingStation: ChargingStation, messageId: string, commandName: IncomingRequestCommand, - commandPayload: JsonType, + commandPayload: ReqType, ): Promise; } diff --git a/src/charging-station/ocpp/OCPPResponseService.ts b/src/charging-station/ocpp/OCPPResponseService.ts index b720c0f1..7934500e 100644 --- a/src/charging-station/ocpp/OCPPResponseService.ts +++ b/src/charging-station/ocpp/OCPPResponseService.ts @@ -31,11 +31,14 @@ export abstract class OCPPResponseService { multipleOfPrecision: 2, }); ajvFormats(this.ajv); - this.responseHandler = this.responseHandler.bind(this) as ( + this.responseHandler = this.responseHandler.bind(this) as < + ReqType extends JsonType, + ResType extends JsonType, + >( chargingStation: ChargingStation, commandName: RequestCommand, - payload: JsonType, - requestPayload: JsonType, + payload: ResType, + requestPayload: ReqType, ) => Promise; this.validateResponsePayload = this.validateResponsePayload.bind(this) as ( chargingStation: ChargingStation, @@ -81,10 +84,10 @@ export abstract class OCPPResponseService { /* This is intentional */ } - public abstract responseHandler( + public abstract responseHandler( chargingStation: ChargingStation, commandName: RequestCommand, - payload: JsonType, - requestPayload: JsonType, + payload: ResType, + requestPayload: ReqType, ): Promise; } -- 2.34.1