Ensure the WS connection have not been forcibly closed after opening
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 7 Apr 2022 08:09:38 +0000 (10:09 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 7 Apr 2022 08:09:38 +0000 (10:09 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStation.ts

index 457369ff1ad029f30046e47432cb40c3546cc551..20705ae3d6b9bbb2dc5d597b02c161f5060bae30 100644 (file)
@@ -1390,58 +1390,64 @@ export default class ChargingStation {
   }
 
   private async onOpen(): Promise<void> {
-    logger.info(
-      `${this.logPrefix()} Connected to OCPP server through ${this.wsConnectionUrl.toString()}`
-    );
-    if (!this.isInAcceptedState()) {
-      // Send BootNotification
-      let registrationRetryCount = 0;
-      do {
-        this.bootNotificationResponse = await this.ocppRequestService.sendMessageHandler<
-          BootNotificationRequest,
-          BootNotificationResponse
-        >(
-          RequestCommand.BOOT_NOTIFICATION,
-          {
-            chargePointModel: this.bootNotificationRequest.chargePointModel,
-            chargePointVendor: this.bootNotificationRequest.chargePointVendor,
-            chargeBoxSerialNumber: this.bootNotificationRequest.chargeBoxSerialNumber,
-            firmwareVersion: this.bootNotificationRequest.firmwareVersion,
-            chargePointSerialNumber: this.bootNotificationRequest.chargePointSerialNumber,
-            iccid: this.bootNotificationRequest.iccid,
-            imsi: this.bootNotificationRequest.imsi,
-            meterSerialNumber: this.bootNotificationRequest.meterSerialNumber,
-            meterType: this.bootNotificationRequest.meterType,
-          },
-          { skipBufferingOnError: true }
-        );
-        if (!this.isInAcceptedState()) {
-          this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++;
-          await Utils.sleep(
-            this.bootNotificationResponse?.interval
-              ? this.bootNotificationResponse.interval * 1000
-              : Constants.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
+    if (this.isWebSocketConnectionOpened()) {
+      logger.info(
+        `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} succeeded`
+      );
+      if (!this.isInAcceptedState()) {
+        // Send BootNotification
+        let registrationRetryCount = 0;
+        do {
+          this.bootNotificationResponse = await this.ocppRequestService.sendMessageHandler<
+            BootNotificationRequest,
+            BootNotificationResponse
+          >(
+            RequestCommand.BOOT_NOTIFICATION,
+            {
+              chargePointModel: this.bootNotificationRequest.chargePointModel,
+              chargePointVendor: this.bootNotificationRequest.chargePointVendor,
+              chargeBoxSerialNumber: this.bootNotificationRequest.chargeBoxSerialNumber,
+              firmwareVersion: this.bootNotificationRequest.firmwareVersion,
+              chargePointSerialNumber: this.bootNotificationRequest.chargePointSerialNumber,
+              iccid: this.bootNotificationRequest.iccid,
+              imsi: this.bootNotificationRequest.imsi,
+              meterSerialNumber: this.bootNotificationRequest.meterSerialNumber,
+              meterType: this.bootNotificationRequest.meterType,
+            },
+            { skipBufferingOnError: true }
           );
+          if (!this.isInAcceptedState()) {
+            this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++;
+            await Utils.sleep(
+              this.bootNotificationResponse?.interval
+                ? this.bootNotificationResponse.interval * 1000
+                : Constants.OCPP_DEFAULT_BOOT_NOTIFICATION_INTERVAL
+            );
+          }
+        } while (
+          !this.isInAcceptedState() &&
+          (registrationRetryCount <= this.getRegistrationMaxRetries() ||
+            this.getRegistrationMaxRetries() === -1)
+        );
+      }
+      if (this.isInAcceptedState()) {
+        await this.startMessageSequence();
+        this.stopped && (this.stopped = false);
+        if (this.wsConnectionRestarted) {
+          this.flushMessageBuffer();
         }
-      } while (
-        !this.isInAcceptedState() &&
-        (registrationRetryCount <= this.getRegistrationMaxRetries() ||
-          this.getRegistrationMaxRetries() === -1)
-      );
-    }
-    if (this.isInAcceptedState()) {
-      await this.startMessageSequence();
-      this.stopped && (this.stopped = false);
-      if (this.wsConnectionRestarted && this.isWebSocketConnectionOpened()) {
-        this.flushMessageBuffer();
+      } else {
+        logger.error(
+          `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
+        );
       }
+      this.autoReconnectRetryCount = 0;
+      this.wsConnectionRestarted = false;
     } else {
-      logger.error(
-        `${this.logPrefix()} Registration failure: max retries reached (${this.getRegistrationMaxRetries()}) or retry disabled (${this.getRegistrationMaxRetries()})`
+      logger.warn(
+        `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.toString()} failed`
       );
     }
-    this.autoReconnectRetryCount = 0;
-    this.wsConnectionRestarted = false;
   }
 
   private async onClose(code: number, reason: string): Promise<void> {