+ public async sendError(messageId: string, ocppError: OCPPError, commandName: IncomingRequestCommand): Promise<ResponseType> {
+ try {
+ // Send error message
+ return await this.internalSendMessage(messageId, ocppError, MessageType.CALL_ERROR_MESSAGE, commandName);
+ } catch (error) {
+ this.handleRequestError(commandName, error as Error);
+ }
+ }
+
+ protected async sendMessage(messageId: string, messagePayload: JsonType, commandName: RequestCommand, params: SendParams = {
+ skipBufferingOnError: false,
+ triggerMessage: false
+ }): Promise<ResponseType> {
+ try {
+ return await this.internalSendMessage(messageId, messagePayload, MessageType.CALL_MESSAGE, commandName, params);
+ } catch (error) {
+ this.handleRequestError(commandName, error as Error, { throwError: false });
+ }
+ }
+
+ private async internalSendMessage(messageId: string, messagePayload: JsonType | OCPPError, messageType: MessageType, commandName?: RequestCommand | IncomingRequestCommand,
+ params: SendParams = {
+ skipBufferingOnError: false,
+ triggerMessage: false
+ }): Promise<ResponseType> {
+ if ((this.chargingStation.isInUnknownState() && commandName === RequestCommand.BOOT_NOTIFICATION)
+ || (!this.chargingStation.getOcppStrictCompliance() && this.chargingStation.isInUnknownState())
+ || this.chargingStation.isInAcceptedState() || (this.chargingStation.isInPendingState() && params.triggerMessage)) {
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
+ const self = this;
+ // Send a message through wsConnection
+ return Utils.promiseWithTimeout(new Promise((resolve, reject) => {
+ const messageToSend = this.buildMessageToSend(messageId, messagePayload, messageType, commandName, responseCallback, rejectCallback);
+ if (this.chargingStation.getEnableStatistics()) {
+ this.chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
+ }
+ // Check if wsConnection opened
+ if (this.chargingStation.isWebSocketConnectionOpened()) {
+ // Yes: Send Message
+ const beginId = PerformanceStatistics.beginMeasure(commandName);
+ // FIXME: Handle sending error
+ this.chargingStation.wsConnection.send(messageToSend);
+ PerformanceStatistics.endMeasure(commandName, beginId);
+ } else if (!params.skipBufferingOnError) {
+ // Buffer it
+ this.chargingStation.bufferMessage(messageToSend);
+ const ocppError = new OCPPError(ErrorType.GENERIC_ERROR, `WebSocket closed for buffered message id '${messageId}' with content '${messageToSend}'`, commandName, messagePayload?.details as JsonType ?? {});
+ if (messageType === MessageType.CALL_MESSAGE) {
+ // Reject it but keep the request in the cache
+ return reject(ocppError);
+ }
+ return rejectCallback(ocppError, false);
+ } else {
+ // Reject it
+ return rejectCallback(new OCPPError(ErrorType.GENERIC_ERROR, `WebSocket closed for non buffered message id '${messageId}' with content '${messageToSend}'`, commandName, messagePayload?.details as JsonType ?? {}), false);
+ }
+ // Response?
+ if (messageType !== MessageType.CALL_MESSAGE) {
+ // Yes: send Ok
+ return resolve(messagePayload);