// Send a message through wsConnection
return Utils.promiseWithTimeout(
new Promise((resolve, reject) => {
+ if (chargingStation.getEnableStatistics() === true) {
+ chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
+ }
const messageToSend = this.buildMessageToSend(
chargingStation,
messageId,
responseCallback,
errorCallback
);
- if (chargingStation.getEnableStatistics() === true) {
- chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
- }
let sendError = false;
// Check if wsConnection opened
- if (chargingStation.isWebSocketConnectionOpened() === true) {
+ const wsOpened = chargingStation.isWebSocketConnectionOpened() === true;
+ if (wsOpened) {
const beginId = PerformanceStatistics.beginMeasure(commandName as string);
try {
chargingStation.wsConnection.send(messageToSend);
}
PerformanceStatistics.endMeasure(commandName as string, beginId);
}
- const wsClosedOrErrored =
- chargingStation.isWebSocketConnectionOpened() === false || sendError === true;
+ const wsClosedOrErrored = !wsOpened || sendError === true;
if (wsClosedOrErrored && params.skipBufferingOnError === false) {
// Buffer
chargingStation.bufferMessage(messageToSend);
import crypto from 'node:crypto';
+import util from 'node:util';
import clone from 'just-clone';
return delay + randomSum;
}
+ public static isPromisePending(promise: Promise<unknown>): boolean {
+ return util.inspect(promise).includes('pending');
+ }
+
public static async promiseWithTimeout<T>(
promise: Promise<T>,
timeoutMs: number,
// Create a timeout promise that rejects in timeout milliseconds
const timeoutPromise = new Promise<never>((_, reject) => {
setTimeout(() => {
- timeoutCallback();
+ if (Utils.isPromisePending(promise)) {
+ timeoutCallback();
+ }
reject(timeoutError);
}, timeoutMs);
});