From e7accadb398c418c41571c53e2c70b1cca3e83d5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 14 Sep 2021 23:39:26 +0200 Subject: [PATCH] Cleanup OCPP message sending and error code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ocpp/OCPPError.ts | 9 +--- .../ocpp/OCPPRequestService.ts | 50 +++++++++++-------- src/exception/BaseError.ts | 8 +++ 3 files changed, 38 insertions(+), 29 deletions(-) create mode 100644 src/exception/BaseError.ts diff --git a/src/charging-station/ocpp/OCPPError.ts b/src/charging-station/ocpp/OCPPError.ts index cd449b10..cdb75027 100644 --- a/src/charging-station/ocpp/OCPPError.ts +++ b/src/charging-station/ocpp/OCPPError.ts @@ -2,9 +2,10 @@ import { IncomingRequestCommand, RequestCommand } from '../../types/ocpp/Requests'; +import BaseError from '../../exception/BaseError'; import { ErrorType } from '../../types/ocpp/ErrorType'; -export default class OCPPError extends Error { +export default class OCPPError extends BaseError { code: ErrorType | IncomingRequestCommand; command?: RequestCommand | IncomingRequestCommand; details?: Record; @@ -12,14 +13,8 @@ export default class OCPPError extends Error { constructor(code: ErrorType | IncomingRequestCommand, message: string, command?: RequestCommand | IncomingRequestCommand, details?: Record) { super(message); - this.name = new.target.name; this.code = code ?? ErrorType.GENERIC_ERROR; - this.message = message ?? ''; this.command = command; this.details = details ?? {}; - - Object.setPrototypeOf(this, new.target.prototype); - - Error.captureStackTrace ? Error.captureStackTrace(this, this.constructor) : (this.stack = (new Error()).stack); } } diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 172a61a2..c35f78de 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -28,27 +28,8 @@ export default abstract class OCPPRequestService { // eslint-disable-next-line @typescript-eslint/no-this-alias const self = this; // Send a message through wsConnection - return new Promise((resolve: (value?: any | PromiseLike) => void, reject: (reason?: any) => void) => { - let messageToSend: string; - // Type of message - switch (messageType) { - // Request - case MessageType.CALL_MESSAGE: - // Build request - this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandParams as Record]); - messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]); - break; - // Response - case MessageType.CALL_RESULT_MESSAGE: - // Build response - messageToSend = JSON.stringify([messageType, messageId, commandParams]); - break; - // Error Message - case MessageType.CALL_ERROR_MESSAGE: - // Build Error Message - messageToSend = JSON.stringify([messageType, messageId, commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? '', commandParams?.details ?? {}]); - break; - } + return new Promise((resolve, reject) => { + const messageToSend = this.buildMessageToSend(messageId, commandParams, messageType, commandName, responseCallback, rejectCallback); if (this.chargingStation.getEnableStatistics()) { this.chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType); } @@ -67,7 +48,7 @@ export default abstract class OCPPRequestService { // Response? if (messageType === MessageType.CALL_RESULT_MESSAGE) { // Yes: send Ok - resolve(); + resolve(commandName); } else if (messageType === MessageType.CALL_ERROR_MESSAGE) { // Send timeout setTimeout(() => rejectCallback(new OCPPError(commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? `Timeout for message id '${messageId}' with content '${messageToSend}'`, commandParams?.details ?? {})), Constants.OCPP_ERROR_TIMEOUT); @@ -112,6 +93,31 @@ export default abstract class OCPPRequestService { throw error; } + private buildMessageToSend(messageId: string, commandParams: any, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand, + responseCallback: (payload: Record | string, requestPayload: Record) => Promise, rejectCallback: (error: OCPPError) => void): string { + let messageToSend: string; + // Type of message + switch (messageType) { + // Request + case MessageType.CALL_MESSAGE: + // Build request + this.chargingStation.requests.set(messageId, [responseCallback, rejectCallback, commandParams as Record]); + messageToSend = JSON.stringify([messageType, messageId, commandName, commandParams]); + break; + // Response + case MessageType.CALL_RESULT_MESSAGE: + // Build response + messageToSend = JSON.stringify([messageType, messageId, commandParams]); + break; + // Error Message + case MessageType.CALL_ERROR_MESSAGE: + // Build Error Message + messageToSend = JSON.stringify([messageType, messageId, commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? '', commandParams?.details ?? {}]); + break; + } + return messageToSend; + } + public abstract sendHeartbeat(): Promise; public abstract sendBootNotification(chargePointModel: string, chargePointVendor: string, chargeBoxSerialNumber?: string, firmwareVersion?: string, chargePointSerialNumber?: string, iccid?: string, imsi?: string, meterSerialNumber?: string, meterType?: string): Promise; public abstract sendStatusNotification(connectorId: number, status: ChargePointStatus, errorCode?: ChargePointErrorCode): Promise; diff --git a/src/exception/BaseError.ts b/src/exception/BaseError.ts new file mode 100644 index 00000000..e1a82c00 --- /dev/null +++ b/src/exception/BaseError.ts @@ -0,0 +1,8 @@ +export default class BaseError extends Error { + public constructor(message?: string) { + super(message); + this.name = new.target.name; + Object.setPrototypeOf(this, new.target.prototype); + Error.captureStackTrace ? (Error.captureStackTrace(this, this.constructor)) : (this.stack = (new Error()).stack); + } +} -- 2.34.1