Falsy/truthy test fixes
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPRequestService.ts
index 023d5f95174bc07df34f3f5dbc49e214f1191d1a..6a7729950de33b2532fb1b8c0153dc170280a957 100644 (file)
@@ -211,7 +211,7 @@ export default abstract class OCPPRequestService {
     messageId: string,
     messagePayload: JsonType | OCPPError,
     messageType: MessageType,
-    commandName?: RequestCommand | IncomingRequestCommand,
+    commandName: RequestCommand | IncomingRequestCommand,
     params: RequestParams = {
       skipBufferingOnError: false,
       triggerMessage: false,
@@ -231,6 +231,9 @@ export default abstract class OCPPRequestService {
       // 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,
@@ -240,15 +243,13 @@ export default abstract class OCPPRequestService {
             responseCallback,
             errorCallback
           );
-          if (chargingStation.getEnableStatistics() === true) {
-            chargingStation.performanceStatistics.addRequestStatistic(commandName, messageType);
-          }
           let sendError = false;
           // Check if wsConnection opened
-          if (chargingStation.isWebSocketConnectionOpened() === true) {
-            const beginId = PerformanceStatistics.beginMeasure(commandName as string);
+          const wsOpened = chargingStation.isWebSocketConnectionOpened() === true;
+          if (wsOpened) {
+            const beginId = PerformanceStatistics.beginMeasure(commandName);
             try {
-              chargingStation.wsConnection.send(messageToSend);
+              chargingStation.wsConnection?.send(messageToSend);
               logger.debug(
                 `${chargingStation.logPrefix()} >> Command '${commandName}' sent ${OCPPServiceUtils.getMessageTypeString(
                   messageType
@@ -263,10 +264,9 @@ export default abstract class OCPPRequestService {
               );
               sendError = true;
             }
-            PerformanceStatistics.endMeasure(commandName as string, beginId);
+            PerformanceStatistics.endMeasure(commandName, beginId);
           }
-          const wsClosedOrErrored =
-            chargingStation.isWebSocketConnectionOpened() === false || sendError === true;
+          const wsClosedOrErrored = !wsOpened || sendError === true;
           if (wsClosedOrErrored && params.skipBufferingOnError === false) {
             // Buffer
             chargingStation.bufferMessage(messageToSend);
@@ -306,7 +306,7 @@ export default abstract class OCPPRequestService {
            */
           function responseCallback(payload: JsonType, requestPayload: JsonType): void {
             if (chargingStation.getEnableStatistics() === true) {
-              chargingStation.performanceStatistics.addRequestStatistic(
+              chargingStation.performanceStatistics?.addRequestStatistic(
                 commandName,
                 MessageType.CALL_RESULT_MESSAGE
               );
@@ -338,7 +338,7 @@ export default abstract class OCPPRequestService {
            */
           function errorCallback(error: OCPPError, requestStatistic = true): void {
             if (requestStatistic === true && chargingStation.getEnableStatistics() === true) {
-              chargingStation.performanceStatistics.addRequestStatistic(
+              chargingStation.performanceStatistics?.addRequestStatistic(
                 commandName,
                 MessageType.CALL_ERROR_MESSAGE
               );
@@ -378,9 +378,9 @@ export default abstract class OCPPRequestService {
     messageId: string,
     messagePayload: JsonType | OCPPError,
     messageType: MessageType,
-    commandName?: RequestCommand | IncomingRequestCommand,
-    responseCallback?: ResponseCallback,
-    errorCallback?: ErrorCallback
+    commandName: RequestCommand | IncomingRequestCommand,
+    responseCallback: ResponseCallback,
+    errorCallback: ErrorCallback
   ): string {
     let messageToSend: string;
     // Type of message