0468c597dae8fdc3997974aeb9dc3311d51b7f5c
[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 app?.appContext.config.globalProperties.$uiClient.setConfiguration(
17 app?.appContext.config.globalProperties.$configuration.uiServer[
18 state.uiServerIndex
19 ]
20 )
21 initializeWSEventListeners()
22 app?.appContext.config.globalProperties.$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 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
33 'error',
34 () => {
35 state.uiServerIndex = getFromLocalStorage<number>(
36 'uiServerConfigurationIndex',
37 0
38 )
39 app?.appContext.config.globalProperties.$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 app = getCurrentInstance()
161
162 const clearToggleButtons = (): void => {
163 for (const key in getLocalStorage()) {
164 if (key.includes('toggle-button')) {
165 deleteFromLocalStorage(key)
166 }
167 }
168 state.value.renderChargingStations = randomUUID()
169 state.value.renderAddChargingStations = randomUUID()
170 }
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 getSimulatorState = (): void => {
182 if (state.value.gettingSimulatorState === false) {
183 state.value.gettingSimulatorState = true
184 uiClient
185 .simulatorState()
186 .then((response: ResponsePayload) => {
187 state.value.simulatorState = response.state as SimulatorState
188 })
189 .catch((error: Error) => {
190 $toast.error('Error at fetching simulator state')
191 console.error('Error at fetching simulator state:', error)
192 })
193 .finally(() => {
194 state.value.renderSimulator = randomUUID()
195 state.value.gettingSimulatorState = false
196 })
197 }
198 }
199
200 const initializeWSEventListeners = () => {
201 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener('open', () => {
202 getSimulatorState()
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 getChargingStations()
224 })
225 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
226 'error',
227 clearChargingStations
228 )
229 app?.appContext.config.globalProperties.$uiClient.registerWSEventListener(
230 'close',
231 clearChargingStations
232 )
233 }
234
235 onMounted(() => {
236 initializeWSEventListeners()
237 })
238
239 const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] =
240 app?.appContext.config.globalProperties.$configuration.uiServer.map(
241 (configuration: UIServerConfigurationSection, index: number) => ({
242 index,
243 configuration
244 })
245 )
246
247 const $toast = useToast()
248
249 const getChargingStations = (): void => {
250 if (state.value.gettingChargingStations === false) {
251 state.value.gettingChargingStations = true
252 uiClient
253 .listChargingStations()
254 .then((response: ResponsePayload) => {
255 if (app != null) {
256 app.appContext.config.globalProperties.$chargingStations = response.chargingStations
257 }
258 })
259 .catch((error: Error) => {
260 if (app != null) {
261 app.appContext.config.globalProperties.$chargingStations = []
262 }
263 $toast.error('Error at fetching charging stations')
264 console.error('Error at fetching charging stations:', error)
265 })
266 .finally(() => {
267 state.value.renderChargingStations = randomUUID()
268 state.value.gettingChargingStations = false
269 })
270 }
271 }
272
273 const startSimulator = (): void => {
274 uiClient
275 .startSimulator()
276 .then(() => {
277 $toast.success('Simulator successfully started')
278 })
279 .catch((error: Error) => {
280 $toast.error('Error at starting simulator')
281 console.error('Error at starting simulator:', error)
282 })
283 .finally(() => {
284 getSimulatorState()
285 })
286 }
287 const stopSimulator = (): void => {
288 uiClient
289 .stopSimulator()
290 .then(() => {
291 if (app != null) {
292 app.appContext.config.globalProperties.$chargingStations = []
293 }
294 $toast.success('Simulator successfully stopped')
295 })
296 .catch((error: Error) => {
297 $toast.error('Error at stopping simulator')
298 console.error('Error at stopping simulator:', error)
299 })
300 .finally(() => {
301 getSimulatorState()
302 })
303 }
304 </script>
305
306 <style>
307 #charging-stations-container {
308 height: fit-content;
309 width: 100%;
310 display: flex;
311 flex-direction: column;
312 }
313
314 #ui-server-container {
315 display: flex;
316 justify-content: center;
317 border-style: outset;
318 }
319
320 #ui-server-selector {
321 width: 100%;
322 background-color: rgb(239, 239, 239);
323 font: small-caption;
324 text-align: center;
325 }
326
327 #ui-server-selector:hover {
328 background-color: rgb(229, 229, 229);
329 }
330
331 #buttons-container {
332 display: flex;
333 flex-direction: row;
334 }
335
336 .simulator-start-button {
337 color: ivory;
338 background-color: green;
339 }
340
341 .simulator-start-button:hover {
342 background-color: rgb(0, 98, 0);
343 }
344
345 .simulator-stop-button {
346 color: ivory;
347 background-color: red;
348 }
349
350 .simulator-stop-button:hover {
351 background-color: rgb(225, 0, 0);
352 }
353
354 #action-button {
355 flex: none;
356 }
357
358 #reload-button {
359 color: ivory;
360 background-color: blue;
361 font-size: 2rem;
362 }
363
364 #reload-button:hover {
365 background-color: rgb(0, 0, 225);
366 }
367
368 #reload-button:active {
369 background-color: darkblue;
370 }
371
372 #action {
373 min-width: max-content;
374 color: ivory;
375 background-color: black;
376 padding: 0.8%;
377 }
378 </style>