- public async sendMessage(messageId: string, commandParams: any, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand,
- skipBufferingOnError = false): Promise<unknown> {
- // eslint-disable-next-line @typescript-eslint/no-this-alias
- const self = this;
- // Send a message through wsConnection
- 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);
- }
- // Check if wsConnection opened
- if (this.chargingStation.isWebSocketConnectionOpened()) {
- // Yes: Send Message
- const beginId = PerformanceStatistics.beginMeasure(commandName);
- this.chargingStation.wsConnection.send(messageToSend);
- PerformanceStatistics.endMeasure(commandName, beginId);
- } else if (!skipBufferingOnError) {
- // Buffer it
- this.chargingStation.addToMessageQueue(messageToSend);
- // Reject it
- return rejectCallback(new OCPPError(commandParams?.code ?? ErrorType.GENERIC_ERROR, commandParams?.message ?? `WebSocket closed for message id '${messageId}' with content '${messageToSend}', message buffered`, commandParams?.details ?? {}));
- }
- // Response?
- if (messageType === MessageType.CALL_RESULT_MESSAGE) {
- // Yes: send Ok
- 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);
- }
+ public async sendMessage(messageId: string, messageData: JsonType | OCPPError, messageType: MessageType, commandName: RequestCommand | IncomingRequestCommand,
+ params: SendParams = {
+ skipBufferingOnError: false,
+ triggerMessage: false
+ }): Promise<JsonType | OCPPError | string> {
+ 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, messageData, 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, messageData?.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, messageData?.details as JsonType ?? {}), false);
+ }
+ // Response?
+ if (messageType !== MessageType.CALL_MESSAGE) {
+ // Yes: send Ok
+ return resolve(messageData);
+ }