+ 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);
+ }
+
+ /**
+ * Function that will receive the request's response
+ *
+ * @param payload
+ * @param requestPayload
+ */
+ async function responseCallback(payload: JsonType | string, requestPayload: JsonType): Promise<void> {
+ if (self.chargingStation.getEnableStatistics()) {
+ self.chargingStation.performanceStatistics.addRequestStatistic(commandName, MessageType.CALL_RESULT_MESSAGE);
+ }
+ // Handle the request's response
+ try {
+ await self.ocppResponseService.handleResponse(commandName as RequestCommand, payload, requestPayload);
+ resolve(payload);
+ } catch (error) {
+ reject(error);
+ throw error;
+ } finally {
+ self.chargingStation.requests.delete(messageId);
+ }