fix: clear UI server cache at simulator stop
authorJérôme Benoit <jerome.benoit@sap.com>
Tue, 20 Feb 2024 16:37:38 +0000 (17:37 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Tue, 20 Feb 2024 16:37:38 +0000 (17:37 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/Bootstrap.ts
src/charging-station/ChargingStation.ts
src/charging-station/ui-server/AbstractUIServer.ts
ui/web/src/views/ChargingStationsView.vue

index 3993ae1f2a34a23ea3113b04b447512deb6474b9..5ee1f2c0ccf8b3fb2fa1ae94282f8c47fc244bf7 100644 (file)
@@ -261,6 +261,7 @@ export class Bootstrap extends EventEmitter {
         await this.workerImplementation?.stop()
         delete this.workerImplementation
         this.removeAllListeners()
+        this.uiServer?.chargingStations.clear()
         await this.storage?.close()
         delete this.storage
         this.started = false
index 11098648497d57e0981ab1dcac31e64a0d2ca09e..4ee9ff6783e2aecfbdb148e4a855bad1b602254c 100644 (file)
@@ -1832,6 +1832,7 @@ export class ChargingStation extends EventEmitter {
 
   private async onOpen (): Promise<void> {
     if (this.isWebSocketConnectionOpened()) {
+      this.emit(ChargingStationEvents.updated)
       logger.info(
         `${this.logPrefix()} Connection to OCPP server through ${this.wsConnectionUrl.href} succeeded`
       )
@@ -1894,6 +1895,7 @@ export class ChargingStation extends EventEmitter {
 
   private onClose (code: WebSocketCloseEventStatusCode, reason: Buffer): void {
     this.emit(ChargingStationEvents.disconnected)
+    this.emit(ChargingStationEvents.updated)
     switch (code) {
       // Normal close
       case WebSocketCloseEventStatusCode.CLOSE_NORMAL:
@@ -1913,12 +1915,13 @@ export class ChargingStation extends EventEmitter {
           )}' and reason '${reason.toString()}'`
         )
         this.started &&
-          this.reconnect().catch(error =>
-            logger.error(`${this.logPrefix()} Error while reconnecting:`, error)
-          )
+          this.reconnect()
+            .then(() => {
+              this.emit(ChargingStationEvents.updated)
+            })
+            .catch(error => logger.error(`${this.logPrefix()} Error while reconnecting:`, error))
         break
     }
-    this.emit(ChargingStationEvents.updated)
   }
 
   private getCachedRequest (
index b17dec30110aea46abae4ec911782c6eb8f690d5..e24a5769956cb40163aa29b2d05f727694029bed 100644 (file)
@@ -63,6 +63,10 @@ export abstract class AbstractUIServer {
     for (const uiService of this.uiServices.values()) {
       uiService.stop()
     }
+    this.clearCaches()
+  }
+
+  private clearCaches (): void {
     this.chargingStations.clear()
     this.chargingStationTemplates.clear()
   }
index 8df77d92429646b638ce576b7e2df4085b22f6db..50851055cf25f924300840e4ebcd6b2ebce23fb9 100644 (file)
         @click="loadChargingStations(() => $router.go(0))"
       />
     </Container>
-    <CSTable :charging-stations="app?.appContext.config.globalProperties.$chargingStations" />
+    <CSTable
+      v-if="
+        Array.isArray(app?.appContext.config.globalProperties.$chargingStations) &&
+        app?.appContext.config.globalProperties.$chargingStations.length > 0
+      "
+      :charging-stations="app?.appContext.config.globalProperties.$chargingStations"
+    />
   </Container>
 </template>
 
@@ -72,6 +78,9 @@ const stopSimulator = (): void => {
   uiClient
     .stopSimulator()
     .then(() => {
+      if (app != null) {
+        app.appContext.config.globalProperties.$chargingStations = []
+      }
       $toast.success('Simulator successfully stopped')
     })
     .catch((error: Error) => {
@@ -85,6 +94,7 @@ const stopSimulator = (): void => {
 #charging-stations-container {
   height: fit-content;
   width: 100%;
+  position: absolute;
   display: flex;
   flex-direction: column;
 }