refactor: add sanity checks at adding charging station(s)
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / ui-services / AbstractUIService.ts
index f8a147895414112bd9f774179092d8d7a89a0a73..07bb2cdb150888d9623d60eb1355531a67c24e71 100644 (file)
@@ -76,6 +76,7 @@ export abstract class AbstractUIService {
       [ProcedureName.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)],
       [ProcedureName.ADD_CHARGING_STATIONS, this.handleAddChargingStations.bind(this)],
       [ProcedureName.PERFORMANCE_STATISTICS, this.handlePerformanceStatistics.bind(this)],
+      [ProcedureName.SIMULATOR_STATE, this.handleSimulatorState.bind(this)],
       [ProcedureName.START_SIMULATOR, this.handleStartSimulator.bind(this)],
       [ProcedureName.STOP_SIMULATOR, this.handleStopSimulator.bind(this)]
     ])
@@ -98,7 +99,7 @@ export abstract class AbstractUIService {
 
       if (!this.requestHandlers.has(command)) {
         throw new BaseError(
-          `${command} is not implemented to handle message payload ${JSON.stringify(
+          `'${command}' is not implemented to handle message payload ${JSON.stringify(
             requestPayload,
             undefined,
             2
@@ -232,8 +233,8 @@ export abstract class AbstractUIService {
   }
 
   private async handleAddChargingStations (
-    messageId?: string,
-    procedureName?: ProcedureName,
+    _messageId?: string,
+    _procedureName?: ProcedureName,
     requestPayload?: RequestPayload
   ): Promise<ResponsePayload> {
     const { template, numberOfStations, options } = requestPayload as {
@@ -241,6 +242,19 @@ export abstract class AbstractUIService {
       numberOfStations: number
       options?: ChargingStationOptions
     }
+    if (!Bootstrap.getInstance().getState().started) {
+      return {
+        status: ResponseStatus.FAILURE,
+        errorMessage:
+          'Cannot add charging station(s) while the charging stations simulator is not started'
+      } satisfies ResponsePayload
+    }
+    if (typeof template !== 'string' || typeof numberOfStations !== 'number') {
+      return {
+        status: ResponseStatus.FAILURE,
+        errorMessage: 'Invalid request payload'
+      } satisfies ResponsePayload
+    }
     if (!this.uiServer.chargingStationTemplates.has(template)) {
       return {
         status: ResponseStatus.FAILURE,
@@ -295,6 +309,21 @@ export abstract class AbstractUIService {
     }
   }
 
+  private handleSimulatorState (): ResponsePayload {
+    try {
+      return {
+        status: ResponseStatus.SUCCESS,
+        state: Bootstrap.getInstance().getState()
+      } satisfies ResponsePayload
+    } catch (error) {
+      return {
+        status: ResponseStatus.FAILURE,
+        errorMessage: (error as Error).message,
+        errorStack: (error as Error).stack
+      } satisfies ResponsePayload
+    }
+  }
+
   private async handleStartSimulator (): Promise<ResponsePayload> {
     try {
       await Bootstrap.getInstance().start()