fa313d6ec54b002e7c676eca395a499b0f215410
[e-mobility-charging-stations-simulator.git] / ui / web / src / views / ChargingStationsView.vue
1 <template>
2 <Container id="charging-stations-container">
3 <Container
4 v-show="Array.isArray(uiServerConfigurations) && uiServerConfigurations.length > 1"
5 id="ui-server-container"
6 >
7 <select
8 id="ui-server-selector"
9 v-model="state.uiServerIndex"
10 @change="
11 () => {
12 if (
13 getFromLocalStorage<number>('uiServerConfigurationIndex', 0) !== state.uiServerIndex
14 ) {
15 app?.appContext.config.globalProperties.$uiClient.setConfiguration(
16 app?.appContext.config.globalProperties.$configuration.uiServer[state.uiServerIndex]
17 )
18 initializeWSEventListeners()
19 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
20 'open',
21 () => {
22 setToLocalStorage<number>('uiServerConfigurationIndex', state.uiServerIndex)
23 for (const key in getLocalStorage()) {
24 if (key.includes('toggle-button')) {
25 removeFromLocalStorage(key)
26 }
27 }
28 $router.currentRoute.value.name !== 'charging-stations' &&
29 $router.push({ name: 'charging-stations' })
30 },
31 { once: true }
32 )
33 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
34 'error',
35 () => {
36 state.uiServerIndex = getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
37 app?.appContext.config.globalProperties.$uiClient.setConfiguration(
38 app?.appContext.config.globalProperties.$configuration.uiServer[
39 getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
40 ]
41 )
42 initializeWSEventListeners()
43 },
44 { once: true }
45 )
46 }
47 }
48 "
49 >
50 <option
51 v-for="uiServerConfiguration in uiServerConfigurations"
52 :value="uiServerConfiguration.index"
53 >
54 {{ uiServerConfiguration.configuration.name ?? uiServerConfiguration.configuration.host }}
55 </option>
56 </select>
57 </Container>
58 <Container id="buttons-container">
59 <Button @click="startSimulator()">Start Simulator</Button>
60 <Button @click="stopSimulator()">Stop Simulator</Button>
61 <ToggleButton
62 :id="'add-charging-stations'"
63 :shared="true"
64 :on="
65 () => {
66 $router.push({ name: 'add-charging-stations' })
67 }
68 "
69 :off="
70 () => {
71 $router.push({ name: 'charging-stations' })
72 }
73 "
74 >
75 Add Charging Stations
76 </ToggleButton>
77 <ReloadButton
78 id="reload-button"
79 :loading="state.loading"
80 @click="loadChargingStations(() => (state.renderChargingStationsList = randomUUID()))"
81 />
82 </Container>
83 <CSTable
84 v-show="
85 Array.isArray(app?.appContext.config.globalProperties.$chargingStations) &&
86 app?.appContext.config.globalProperties.$chargingStations.length > 0
87 "
88 :key="state.renderChargingStationsList"
89 :charging-stations="app?.appContext.config.globalProperties.$chargingStations"
90 />
91 </Container>
92 </template>
93
94 <script setup lang="ts">
95 import { getCurrentInstance, onMounted, ref } from 'vue'
96 import { useToast } from 'vue-toast-notification'
97 import CSTable from '@/components/charging-stations/CSTable.vue'
98 import type { ResponsePayload, UIServerConfigurationSection } from '@/types'
99 import Container from '@/components/Container.vue'
100 import ReloadButton from '@/components/buttons/ReloadButton.vue'
101 import Button from '@/components/buttons/Button.vue'
102 import {
103 getFromLocalStorage,
104 getLocalStorage,
105 randomUUID,
106 removeFromLocalStorage,
107 setToLocalStorage
108 } from '@/composables'
109 import ToggleButton from '@/components/buttons/ToggleButton.vue'
110
111 const app = getCurrentInstance()
112
113 const initializeWSEventListeners = () => {
114 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('open', () => {
115 uiClient
116 .listTemplates()
117 .then((response: ResponsePayload) => {
118 if (app != null) {
119 app.appContext.config.globalProperties.$templates = response.templates
120 }
121 })
122 .catch((error: Error) => {
123 if (app != null) {
124 app.appContext.config.globalProperties.$templates = []
125 }
126 $toast.error('Error at fetching charging station templates')
127 console.error('Error at fetching charging station templates:', error)
128 })
129 loadChargingStations(() => (state.value.renderChargingStationsList = randomUUID()))
130 })
131 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('error', () => {
132 app.appContext.config.globalProperties.$chargingStations = []
133 state.value.renderChargingStationsList = randomUUID()
134 })
135 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('close', () => {
136 app.appContext.config.globalProperties.$chargingStations = []
137 state.value.renderChargingStationsList = randomUUID()
138 })
139 }
140
141 onMounted(() => {
142 initializeWSEventListeners()
143 })
144
145 const state = ref({
146 renderChargingStationsList: randomUUID(),
147 loading: false,
148 uiServerIndex: getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
149 })
150
151 const uiClient = app?.appContext.config.globalProperties.$uiClient
152 const uiServerConfigurations: { configuration: UIServerConfigurationSection; index: number }[] =
153 app?.appContext.config.globalProperties.$configuration.uiServer.map(
154 (configuration: UIServerConfigurationSection, index: number) => ({
155 configuration,
156 index
157 })
158 )
159
160 const $toast = useToast()
161
162 const loadChargingStations = (renderCallback?: () => void): void => {
163 if (state.value.loading === false) {
164 state.value.loading = true
165 uiClient
166 .listChargingStations()
167 .then((response: ResponsePayload) => {
168 if (app != null) {
169 app.appContext.config.globalProperties.$chargingStations = response.chargingStations
170 }
171 })
172 .catch((error: Error) => {
173 if (app != null) {
174 app.appContext.config.globalProperties.$chargingStations = []
175 }
176 $toast.error('Error at fetching charging stations')
177 console.error('Error at fetching charging stations:', error)
178 })
179 .finally(() => {
180 if (renderCallback != null) {
181 renderCallback()
182 }
183 state.value.loading = false
184 })
185 }
186 }
187
188 const startSimulator = (): void => {
189 uiClient
190 .startSimulator()
191 .then(() => {
192 $toast.success('Simulator successfully started')
193 })
194 .catch((error: Error) => {
195 $toast.error('Error at starting simulator')
196 console.error('Error at starting simulator:', error)
197 })
198 }
199 const stopSimulator = (): void => {
200 uiClient
201 .stopSimulator()
202 .then(() => {
203 if (app != null) {
204 app.appContext.config.globalProperties.$chargingStations = []
205 }
206 $toast.success('Simulator successfully stopped')
207 })
208 .catch((error: Error) => {
209 $toast.error('Error at stopping simulator')
210 console.error('Error at stopping simulator:', error)
211 })
212 }
213 </script>
214
215 <style>
216 #charging-stations-container {
217 height: fit-content;
218 width: 100%;
219 display: flex;
220 flex-direction: column;
221 }
222
223 #ui-server-container {
224 display: flex;
225 flex-direction: row;
226 }
227
228 #ui-server-selector {
229 width: 100%;
230 text-align: center;
231 }
232
233 #buttons-container {
234 display: flex;
235 flex-direction: row;
236 }
237
238 #action-button {
239 flex: none;
240 }
241
242 #reload-button {
243 flex: auto;
244 color: white;
245 background-color: blue;
246 font-size: 1.5rem;
247 font-weight: bold;
248 align-items: center;
249 justify-content: center;
250 }
251
252 #reload-button:hover {
253 background-color: rgb(0, 0, 225);
254 }
255
256 #reload-button:active {
257 background-color: red;
258 }
259
260 #action {
261 color: white;
262 background-color: black;
263 padding: 1%;
264 }
265 </style>