+const uiClient = useUIClient()
+
+const $toast = useToast()
+
+const getSimulatorState = (): void => {
+ if (state.value.gettingSimulatorState === false) {
+ state.value.gettingSimulatorState = true
+ uiClient
+ .simulatorState()
+ .then((response: ResponsePayload) => {
+ simulatorState.value = response.state as SimulatorState
+ })
+ .catch((error: Error) => {
+ $toast.error('Error at fetching simulator state')
+ console.error('Error at fetching simulator state:', error)
+ })
+ .finally(() => {
+ state.value.gettingSimulatorState = false
+ })
+ }
+}
+
+const getTemplates = (): void => {
+ if (state.value.gettingTemplates === false) {
+ state.value.gettingTemplates = true
+ uiClient
+ .listTemplates()
+ .then((response: ResponsePayload) => {
+ if (app != null) {
+ app.appContext.config.globalProperties.$templates.value = response.templates as string[]
+ }
+ })
+ .catch((error: Error) => {
+ clearTemplates()
+ $toast.error('Error at fetching charging station templates')
+ console.error('Error at fetching charging station templates:', error)
+ })
+ .finally(() => {
+ state.value.gettingTemplates = false
+ })
+ }
+}
+
+const getChargingStations = (): void => {
+ if (state.value.gettingChargingStations === false) {
+ state.value.gettingChargingStations = true
+ uiClient
+ .listChargingStations()
+ .then((response: ResponsePayload) => {
+ if (app != null) {
+ app.appContext.config.globalProperties.$chargingStations.value =
+ response.chargingStations as ChargingStationData[]
+ }
+ })
+ .catch((error: Error) => {
+ clearChargingStations()
+ $toast.error('Error at fetching charging stations')
+ console.error('Error at fetching charging stations:', error)
+ })
+ .finally(() => {
+ state.value.gettingChargingStations = false
+ })
+ }
+}
+
+const getData = (): void => {
+ getSimulatorState()
+ getTemplates()
+ getChargingStations()
+}
+
+const registerWSEventListeners = () => {
+ uiClient.registerWSEventListener('open', getData)
+ uiClient.registerWSEventListener('error', clearChargingStations)
+ uiClient.registerWSEventListener('close', clearChargingStations)
+}
+
+const unregisterWSEventListeners = () => {
+ uiClient.unregisterWSEventListener('open', getData)
+ uiClient.unregisterWSEventListener('error', clearChargingStations)
+ uiClient.unregisterWSEventListener('close', clearChargingStations)
+}
+
+onMounted(() => {
+ registerWSEventListeners()
+})
+
+onUnmounted(() => {
+ unregisterWSEventListeners()
+})
+
+const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] = (
+ app?.appContext.config.globalProperties.$configuration.value
+ .uiServer as UIServerConfigurationSection[]
+).map((configuration: UIServerConfigurationSection, index: number) => ({
+ index,
+ configuration
+}))
+
+const startSimulator = (): void => {
+ uiClient
+ .startSimulator()
+ .then(() => {
+ $toast.success('Simulator successfully started')
+ })
+ .catch((error: Error) => {
+ $toast.error('Error at starting simulator')
+ console.error('Error at starting simulator:', error)
+ })
+ .finally(() => {
+ getSimulatorState()
+ })