From: Jérôme Benoit Date: Wed, 13 Jan 2021 21:35:19 +0000 (+0100) Subject: Type OCPP requests X-Git-Tag: v1.0.1-0~147 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=5b0e583fa396c09bd7bcfce6acf6d69c96ef0610;p=e-mobility-charging-stations-simulator.git Type OCPP requests Signed-off-by: Jérôme Benoit --- 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];