From 5b0e583fa396c09bd7bcfce6acf6d69c96ef0610 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 13 Jan 2021 22:35:19 +0100 Subject: [PATCH] Type OCPP requests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 18 ++++++++---------- src/charging-station/OcppError.ts | 2 +- src/types/ocpp/Requests.ts | 4 ++-- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index ce036f7a..cdf87cdd 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -714,8 +714,8 @@ export default class ChargingStation { } async onMessage(messageEvent: MessageEvent): Promise { - let [messageType, messageId, commandName, commandPayload, errorDetails]: IncomingRequest = [0, '', '' as IncomingRequestCommand, '', '']; - let responseCallback: (payload?, requestPayload?) => void; + let [messageType, messageId, commandName, commandPayload, errorDetails]: IncomingRequest = [0, '', '' as IncomingRequestCommand, '', {}]; + let responseCallback: (payload?: Record | string, requestPayload?: Record) => void; let rejectCallback: (error: OCPPError) => void; let requestPayload: Record; let errMsg: string; @@ -746,7 +746,7 @@ export default class ChargingStation { throw new Error(`Response request for unknown message id ${messageId}`); } delete this._requests[messageId]; - responseCallback(commandName, requestPayload); + responseCallback(commandName.toString(), requestPayload); break; // Error Message case MessageType.CALL_ERROR_MESSAGE: @@ -760,7 +760,7 @@ export default class ChargingStation { throw new Error(`Error request for message id ${messageId} is not iterable`); } delete this._requests[messageId]; - rejectCallback(new OCPPError(commandName, commandPayload, errorDetails)); + rejectCallback(new OCPPError(commandName, commandPayload.toString(), errorDetails)); break; // Error default: @@ -837,14 +837,12 @@ export default class ChargingStation { } } - async sendError(messageId: string, err: Error | OCPPError, commandName: RequestCommand | IncomingRequestCommand): Promise { - // Check exception type: only OCPP error are accepted - const error = err instanceof OCPPError ? err : new OCPPError(ErrorType.INTERNAL_ERROR, err.message, err.stack && err.stack); + async sendError(messageId: string, error: OCPPError, commandName: RequestCommand | IncomingRequestCommand): Promise { // Send error return this.sendMessage(messageId, error, MessageType.CALL_ERROR_MESSAGE, commandName); } - async sendMessage(messageId: string, commandParams, messageType: MessageType = MessageType.CALL_RESULT_MESSAGE, commandName: RequestCommand | IncomingRequestCommand): Promise { + async sendMessage(messageId: string, commandParams: any, messageType: MessageType = MessageType.CALL_RESULT_MESSAGE, commandName: RequestCommand | IncomingRequestCommand): Promise { // eslint-disable-next-line @typescript-eslint/no-this-alias const self = this; // Send a message through wsConnection @@ -927,7 +925,7 @@ export default class ChargingStation { }); } - async handleResponse(commandName: RequestCommand, payload, requestPayload): Promise { + async handleResponse(commandName: RequestCommand, payload: Record, requestPayload: Record): Promise { const responseCallbackFn = 'handleResponse' + commandName; if (typeof this[responseCallbackFn] === 'function') { await this[responseCallbackFn](payload, requestPayload); @@ -1038,7 +1036,7 @@ export default class ChargingStation { logger.debug(this._logPrefix() + ' Heartbeat response received: %j to Heartbeat request: %j', payload, requestPayload); } - async handleRequest(messageId: string, commandName: IncomingRequestCommand, commandPayload): Promise { + async handleRequest(messageId: string, commandName: IncomingRequestCommand, commandPayload: Record | string): Promise { let response; // Call if (typeof this['handleRequest' + commandName] === 'function') { diff --git a/src/charging-station/OcppError.ts b/src/charging-station/OcppError.ts index d39cdef1..ae86c0a1 100644 --- a/src/charging-station/OcppError.ts +++ b/src/charging-station/OcppError.ts @@ -2,7 +2,7 @@ import { ErrorType } from '../types/ocpp/ErrorType'; export default class OCPPError extends Error { code: string; - details: any; + details?: any; constructor(code: string, message: string, details?: any) { super(message); diff --git a/src/types/ocpp/Requests.ts b/src/types/ocpp/Requests.ts index 48643619..7ce4979e 100644 --- a/src/types/ocpp/Requests.ts +++ b/src/types/ocpp/Requests.ts @@ -6,6 +6,6 @@ export default interface Requests { [id: string]: Request; } -export type Request = [(payload?, requestPayload?) => void, (error?: OCPPError) => void, Record]; +export type Request = [(payload?: Record, requestPayload?: Record) => void, (error?: OCPPError) => void, Record]; -export type IncomingRequest = [MessageType, string, IncomingRequestCommand, string, string]; +export type IncomingRequest = [MessageType, string, IncomingRequestCommand, Record | string, Record]; -- 2.34.1