109803f4beb8c2827c058cccddd9ae1235cbec8c
[e-mobility-charging-stations-simulator.git] / ui / web / src / views / ChargingStationsView.vue
1 <template>
2 <Container id="charging-stations-container">
3 <Container id="buttons-container">
4 <Container
5 v-show="Array.isArray(uiServerConfigurations) && uiServerConfigurations.length > 1"
6 id="ui-server-container"
7 >
8 <select
9 id="ui-server-selector"
10 v-model="state.uiServerIndex"
11 @change="
12 () => {
13 if (
14 getFromLocalStorage<number>('uiServerConfigurationIndex', 0) !== state.uiServerIndex
15 ) {
16 uiClient.setConfiguration(
17 app?.appContext.config.globalProperties.$configuration.uiServer[
18 state.uiServerIndex
19 ]
20 )
21 initializeWSEventListeners()
22 uiClient.registerWSEventListener(
23 'open',
24 () => {
25 setToLocalStorage<number>('uiServerConfigurationIndex', state.uiServerIndex)
26 clearToggleButtons()
27 $router.currentRoute.value.name !== 'charging-stations' &&
28 $router.push({ name: 'charging-stations' })
29 },
30 { once: true }
31 )
32 uiClient.registerWSEventListener(
33 'error',
34 () => {
35 state.uiServerIndex = getFromLocalStorage<number>(
36 'uiServerConfigurationIndex',
37 0
38 )
39 uiClient.setConfiguration(
40 app?.appContext.config.globalProperties.$configuration.uiServer[
41 getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
42 ]
43 )
44 initializeWSEventListeners()
45 },
46 { once: true }
47 )
48 }
49 }
50 "
51 >
52 <option
53 v-for="uiServerConfiguration in uiServerConfigurations"
54 :value="uiServerConfiguration.index"
55 >
56 {{
57 uiServerConfiguration.configuration.name ?? uiServerConfiguration.configuration.host
58 }}
59 </option>
60 </select>
61 </Container>
62 <ToggleButton
63 :id="'simulator'"
64 :key="state.renderSimulator"
65 :status="state.simulatorState?.started"
66 :on="() => startSimulator()"
67 :off="() => stopSimulator()"
68 :class="simulatorButtonClass"
69 >
70 {{ simulatorButtonMessage }}
71 </ToggleButton>
72 <ToggleButton
73 :id="'add-charging-stations'"
74 :key="state.renderAddChargingStations"
75 :shared="true"
76 :on="
77 () => {
78 $router.push({ name: 'add-charging-stations' })
79 }
80 "
81 :off="
82 () => {
83 $router.push({ name: 'charging-stations' })
84 }
85 "
86 @clicked="
87 () => {
88 state.renderChargingStations = randomUUID()
89 }
90 "
91 >
92 Add Charging Stations
93 </ToggleButton>
94 <ReloadButton
95 id="reload-button"
96 :loading="state.gettingChargingStations"
97 @click="getChargingStations()"
98 />
99 </Container>
100 <CSTable
101 v-show="
102 Array.isArray(app?.appContext.config.globalProperties.$chargingStations) &&
103 app.appContext.config.globalProperties.$chargingStations.length > 0
104 "
105 :key="state.renderChargingStations"
106 :charging-stations="app?.appContext.config.globalProperties.$chargingStations"
107 @need-refresh="
108 () => {
109 state.renderAddChargingStations = randomUUID()
110 state.renderChargingStations = randomUUID()
111 }
112 "
113 />
114 </Container>
115 </template>
116
117 <script setup lang="ts">
118 import { computed, getCurrentInstance, onMounted, ref } from 'vue'
119 import { useToast } from 'vue-toast-notification'
120 import CSTable from '@/components/charging-stations/CSTable.vue'
121 import type { ResponsePayload, SimulatorState, UIServerConfigurationSection } from '@/types'
122 import Container from '@/components/Container.vue'
123 import ReloadButton from '@/components/buttons/ReloadButton.vue'
124 import {
125 deleteFromLocalStorage,
126 getFromLocalStorage,
127 getLocalStorage,
128 randomUUID,
129 setToLocalStorage
130 } from '@/composables'
131 import ToggleButton from '@/components/buttons/ToggleButton.vue'
132
133 const state = ref<{
134 renderSimulator: `${string}-${string}-${string}-${string}-${string}`
135 renderAddChargingStations: `${string}-${string}-${string}-${string}-${string}`
136 renderChargingStations: `${string}-${string}-${string}-${string}-${string}`
137 gettingSimulatorState: boolean
138 gettingTemplates: boolean
139 gettingChargingStations: boolean
140 simulatorState?: SimulatorState
141 uiServerIndex: number
142 }>({
143 renderSimulator: randomUUID(),
144 renderAddChargingStations: randomUUID(),
145 renderChargingStations: randomUUID(),
146 gettingSimulatorState: false,
147 gettingTemplates: false,
148 gettingChargingStations: false,
149 uiServerIndex: getFromLocalStorage<number>('uiServerConfigurationIndex', 0)
150 })
151
152 const simulatorButtonClass = computed<string>(() =>
153 state.value.simulatorState?.started === true ? 'simulator-stop-button' : 'simulator-start-button'
154 )
155 const simulatorButtonMessage = computed<string>(
156 () =>
157 `${state.value.simulatorState?.started === true ? 'Stop' : 'Start'} Simulator${state.value.simulatorState?.version != null ? ` (${state.value.simulatorState.version})` : ''}`
158 )
159
160 const clearToggleButtons = (): void => {
161 for (const key in getLocalStorage()) {
162 if (key.includes('toggle-button')) {
163 deleteFromLocalStorage(key)
164 }
165 }
166 state.value.renderChargingStations = randomUUID()
167 state.value.renderAddChargingStations = randomUUID()
168 }
169
170 const app = getCurrentInstance()
171
172 const clearChargingStations = (): void => {
173 if (app != null) {
174 app.appContext.config.globalProperties.$chargingStations = []
175 }
176 state.value.renderChargingStations = randomUUID()
177 }
178
179 const uiClient = app?.appContext.config.globalProperties.$uiClient
180
181 const $toast = useToast()
182
183 const getSimulatorState = (): void => {
184 if (state.value.gettingSimulatorState === false) {
185 state.value.gettingSimulatorState = true
186 uiClient
187 .simulatorState()
188 .then((response: ResponsePayload) => {
189 state.value.simulatorState = response.state as SimulatorState
190 })
191 .catch((error: Error) => {
192 $toast.error('Error at fetching simulator state')
193 console.error('Error at fetching simulator state:', error)
194 })
195 .finally(() => {
196 state.value.renderSimulator = randomUUID()
197 state.value.gettingSimulatorState = false
198 })
199 }
200 }
201
202 const getTemplates = (): void => {
203 if (state.value.gettingTemplates === false) {
204 state.value.gettingTemplates = true
205 uiClient
206 .listTemplates()
207 .then((response: ResponsePayload) => {
208 if (app != null) {
209 app.appContext.config.globalProperties.$templates = response.templates
210 }
211 })
212 .catch((error: Error) => {
213 if (app != null) {
214 app.appContext.config.globalProperties.$templates = []
215 }
216 $toast.error('Error at fetching charging station templates')
217 console.error('Error at fetching charging station templates:', error)
218 })
219 .finally(() => {
220 state.value.gettingTemplates = false
221 })
222 }
223 }
224
225 const getChargingStations = (): void => {
226 if (state.value.gettingChargingStations === false) {
227 state.value.gettingChargingStations = true
228 uiClient
229 .listChargingStations()
230 .then((response: ResponsePayload) => {
231 if (app != null) {
232 app.appContext.config.globalProperties.$chargingStations = response.chargingStations
233 }
234 })
235 .catch((error: Error) => {
236 if (app != null) {
237 app.appContext.config.globalProperties.$chargingStations = []
238 }
239 $toast.error('Error at fetching charging stations')
240 console.error('Error at fetching charging stations:', error)
241 })
242 .finally(() => {
243 state.value.renderChargingStations = randomUUID()
244 state.value.gettingChargingStations = false
245 })
246 }
247 }
248
249 const initializeWSEventListeners = () => {
250 uiClient.registerWSEventListener('open', () => {
251 getSimulatorState()
252 getTemplates()
253 getChargingStations()
254 })
255 uiClient.registerWSEventListener('error', clearChargingStations)
256 uiClient.registerWSEventListener('close', clearChargingStations)
257 }
258
259 onMounted(() => {
260 initializeWSEventListeners()
261 })
262
263 const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] =
264 app?.appContext.config.globalProperties.$configuration.uiServer.map(
265 (configuration: UIServerConfigurationSection, index: number) => ({
266 index,
267 configuration
268 })
269 )
270
271 const startSimulator = (): void => {
272 uiClient
273 .startSimulator()
274 .then(() => {
275 $toast.success('Simulator successfully started')
276 })
277 .catch((error: Error) => {
278 $toast.error('Error at starting simulator')
279 console.error('Error at starting simulator:', error)
280 })
281 .finally(() => {
282 getSimulatorState()
283 })
284 }
285 const stopSimulator = (): void => {
286 uiClient
287 .stopSimulator()
288 .then(() => {
289 if (app != null) {
290 app.appContext.config.globalProperties.$chargingStations = []
291 }
292 $toast.success('Simulator successfully stopped')
293 })
294 .catch((error: Error) => {
295 $toast.error('Error at stopping simulator')
296 console.error('Error at stopping simulator:', error)
297 })
298 .finally(() => {
299 getSimulatorState()
300 })
301 }
302 </script>
303
304 <style>
305 #charging-stations-container {
306 height: fit-content;
307 width: 100%;
308 display: flex;
309 flex-direction: column;
310 }
311
312 #ui-server-container {
313 display: flex;
314 justify-content: center;
315 border-style: outset;
316 }
317
318 #ui-server-selector {
319 width: 100%;
320 background-color: rgb(239, 239, 239);
321 font: small-caption;
322 text-align: center;
323 }
324
325 #ui-server-selector:hover {
326 background-color: rgb(229, 229, 229);
327 }
328
329 #buttons-container {
330 display: flex;
331 flex-direction: row;
332 }
333
334 .simulator-start-button {
335 color: ivory;
336 background-color: green;
337 }
338
339 .simulator-start-button:hover {
340 background-color: rgb(0, 98, 0);
341 }
342
343 .simulator-stop-button {
344 color: ivory;
345 background-color: red;
346 }
347
348 .simulator-stop-button:hover {
349 background-color: rgb(225, 0, 0);
350 }
351
352 #action-button {
353 flex: none;
354 }
355
356 #reload-button {
357 color: ivory;
358 background-color: blue;
359 font-size: 2rem;
360 }
361
362 #reload-button:hover {
363 background-color: rgb(0, 0, 225);
364 }
365
366 #reload-button:active {
367 background-color: darkblue;
368 }
369
370 #action {
371 min-width: max-content;
372 color: ivory;
373 background-color: black;
374 padding: 0.8%;
375 }
376 </style>