+ protected async sendMessage(
+ messageId: string,
+ messagePayload: JsonType,
+ commandName: RequestCommand,
+ params: RequestParams = {
+ 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: RequestParams = {
+ 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 || messageType === MessageType.CALL_RESULT_MESSAGE))
+ ) {
+ // 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,
+ errorCallback
+ );
+ 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);
+ logger.debug(
+ `${this.chargingStation.logPrefix()} >> Command '${commandName}' sent ${this.getMessageTypeString(
+ messageType
+ )} payload: ${messageToSend}`
+ );
+ } 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 as JsonObject)?.details ?? {}
+ );
+ if (messageType === MessageType.CALL_MESSAGE) {
+ // Reject it but keep the request in the cache
+ return reject(ocppError);
+ }
+ return errorCallback(ocppError, false);
+ } else {
+ // Reject it
+ return errorCallback(
+ new OCPPError(
+ ErrorType.GENERIC_ERROR,
+ `WebSocket closed for non buffered message id '${messageId}' with content '${messageToSend}'`,
+ commandName,
+ (messagePayload as JsonObject)?.details ?? {}
+ ),
+ 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,
+ 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.responseHandler(
+ commandName as RequestCommand,
+ payload,
+ requestPayload
+ );
+ resolve(payload);
+ } catch (error) {
+ reject(error);
+ } finally {
+ self.chargingStation.requests.delete(messageId);
+ }
+ }
+
+ /**
+ * Function that will receive the request's error response
+ *
+ * @param error
+ * @param requestStatistic
+ */
+ function errorCallback(error: OCPPError, requestStatistic = true): void {
+ if (requestStatistic && self.chargingStation.getEnableStatistics()) {
+ self.chargingStation.performanceStatistics.addRequestStatistic(
+ commandName,
+ MessageType.CALL_ERROR_MESSAGE
+ );
+ }
+ logger.error(
+ `${self.chargingStation.logPrefix()} Error %j occurred when calling command %s with message data %j`,
+ error,
+ commandName,
+ messagePayload
+ );
+ self.chargingStation.requests.delete(messageId);
+ reject(error);
+ }
+ }),
+ Constants.OCPP_WEBSOCKET_TIMEOUT,
+ new OCPPError(
+ ErrorType.GENERIC_ERROR,
+ `Timeout for message id '${messageId}'`,
+ commandName,
+ (messagePayload as JsonObject)?.details ?? {}
+ ),
+ () => {
+ messageType === MessageType.CALL_MESSAGE &&
+ this.chargingStation.requests.delete(messageId);