Commit | Line | Data |
---|---|---|
32de5a57 | 1 | <template> |
ca1e5439 | 2 | <Container id="charging-stations-container"> |
239bd875 JB |
3 | <Container id="buttons-container"> |
4 | <Container | |
5 | v-show="Array.isArray(uiServerConfigurations) && uiServerConfigurations.length > 1" | |
6 | id="ui-server-container" | |
0344ad2b | 7 | > |
239bd875 JB |
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 | ) { | |
97cd0ef3 | 16 | uiClient.setConfiguration( |
239bd875 JB |
17 | app?.appContext.config.globalProperties.$configuration.uiServer[ |
18 | state.uiServerIndex | |
19 | ] | |
20 | ) | |
aa9b0a1e | 21 | registerWSEventListeners() |
97cd0ef3 | 22 | uiClient.registerWSEventListener( |
239bd875 JB |
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 | ) | |
97cd0ef3 | 32 | uiClient.registerWSEventListener( |
239bd875 JB |
33 | 'error', |
34 | () => { | |
35 | state.uiServerIndex = getFromLocalStorage<number>( | |
36 | 'uiServerConfigurationIndex', | |
37 | 0 | |
38 | ) | |
97cd0ef3 | 39 | uiClient.setConfiguration( |
239bd875 JB |
40 | app?.appContext.config.globalProperties.$configuration.uiServer[ |
41 | getFromLocalStorage<number>('uiServerConfigurationIndex', 0) | |
42 | ] | |
43 | ) | |
aa9b0a1e | 44 | registerWSEventListeners() |
239bd875 JB |
45 | }, |
46 | { once: true } | |
47 | ) | |
48 | } | |
49 | } | |
50 | " | |
0344ad2b | 51 | > |
239bd875 JB |
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> | |
240fa4da JB |
62 | <ToggleButton |
63 | :id="'simulator'" | |
64 | :key="state.renderSimulator" | |
65 | :status="state.simulatorState?.started" | |
66 | :on="() => startSimulator()" | |
67 | :off="() => stopSimulator()" | |
ec7366ed | 68 | :class="simulatorButtonClass" |
240fa4da | 69 | > |
ec7366ed | 70 | {{ simulatorButtonMessage }} |
240fa4da | 71 | </ToggleButton> |
2610da71 JB |
72 | <ToggleButton |
73 | :id="'add-charging-stations'" | |
d64ea57b | 74 | :key="state.renderAddChargingStations" |
2610da71 JB |
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 | " | |
83468764 JB |
86 | @clicked=" |
87 | () => { | |
88 | state.renderChargingStations = randomUUID() | |
89 | } | |
90 | " | |
2610da71 | 91 | > |
1eb5f592 | 92 | Add Charging Stations |
2610da71 | 93 | </ToggleButton> |
b9d447d2 JB |
94 | <ReloadButton |
95 | id="reload-button" | |
26cf7d99 JB |
96 | :loading="state.gettingChargingStations" |
97 | @click="getChargingStations()" | |
b9d447d2 | 98 | /> |
13c19b7b | 99 | </Container> |
5c0e9352 | 100 | <CSTable |
7378b34a | 101 | v-show=" |
5c0e9352 | 102 | Array.isArray(app?.appContext.config.globalProperties.$chargingStations) && |
3eea3ebc | 103 | app.appContext.config.globalProperties.$chargingStations.length > 0 |
5c0e9352 | 104 | " |
3eea3ebc | 105 | :key="state.renderChargingStations" |
5c0e9352 | 106 | :charging-stations="app?.appContext.config.globalProperties.$chargingStations" |
83468764 JB |
107 | @need-refresh=" |
108 | () => { | |
109 | state.renderAddChargingStations = randomUUID() | |
110 | state.renderChargingStations = randomUUID() | |
111 | } | |
112 | " | |
5c0e9352 | 113 | /> |
32de5a57 LM |
114 | </Container> |
115 | </template> | |
116 | ||
117 | <script setup lang="ts"> | |
aa9b0a1e | 118 | import { computed, getCurrentInstance, onMounted, onUnmounted, ref } from 'vue' |
cea23fa0 | 119 | import { useToast } from 'vue-toast-notification' |
66a7748d | 120 | import CSTable from '@/components/charging-stations/CSTable.vue' |
e8237645 | 121 | import type { ResponsePayload, SimulatorState, UIServerConfigurationSection } from '@/types' |
66a7748d JB |
122 | import Container from '@/components/Container.vue' |
123 | import ReloadButton from '@/components/buttons/ReloadButton.vue' | |
2610da71 | 124 | import { |
b767fda7 | 125 | deleteFromLocalStorage, |
2610da71 JB |
126 | getFromLocalStorage, |
127 | getLocalStorage, | |
128 | randomUUID, | |
2610da71 JB |
129 | setToLocalStorage |
130 | } from '@/composables' | |
131 | import ToggleButton from '@/components/buttons/ToggleButton.vue' | |
a4868fd7 | 132 | |
240fa4da JB |
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}` | |
26cf7d99 JB |
137 | gettingSimulatorState: boolean |
138 | gettingTemplates: boolean | |
139 | gettingChargingStations: boolean | |
e8237645 | 140 | simulatorState?: SimulatorState |
240fa4da JB |
141 | uiServerIndex: number |
142 | }>({ | |
143 | renderSimulator: randomUUID(), | |
144 | renderAddChargingStations: randomUUID(), | |
145 | renderChargingStations: randomUUID(), | |
26cf7d99 JB |
146 | gettingSimulatorState: false, |
147 | gettingTemplates: false, | |
148 | gettingChargingStations: false, | |
240fa4da JB |
149 | uiServerIndex: getFromLocalStorage<number>('uiServerConfigurationIndex', 0) |
150 | }) | |
151 | ||
ec7366ed JB |
152 | const simulatorButtonClass = computed<string>(() => |
153 | state.value.simulatorState?.started === true ? 'simulator-stop-button' : 'simulator-start-button' | |
154 | ) | |
155 | const simulatorButtonMessage = computed<string>( | |
156 | () => | |
1fa3425a | 157 | `${state.value.simulatorState?.started === true ? 'Stop' : 'Start'} Simulator${state.value.simulatorState?.version != null ? ` (${state.value.simulatorState.version})` : ''}` |
ec7366ed JB |
158 | ) |
159 | ||
d64ea57b JB |
160 | const clearToggleButtons = (): void => { |
161 | for (const key in getLocalStorage()) { | |
162 | if (key.includes('toggle-button')) { | |
b767fda7 | 163 | deleteFromLocalStorage(key) |
d64ea57b JB |
164 | } |
165 | } | |
26cf7d99 JB |
166 | state.value.renderChargingStations = randomUUID() |
167 | state.value.renderAddChargingStations = randomUUID() | |
d64ea57b JB |
168 | } |
169 | ||
97cd0ef3 JB |
170 | const app = getCurrentInstance() |
171 | ||
d64ea57b | 172 | const clearChargingStations = (): void => { |
26cf7d99 JB |
173 | if (app != null) { |
174 | app.appContext.config.globalProperties.$chargingStations = [] | |
175 | } | |
3eea3ebc | 176 | state.value.renderChargingStations = randomUUID() |
d64ea57b JB |
177 | } |
178 | ||
240fa4da JB |
179 | const uiClient = app?.appContext.config.globalProperties.$uiClient |
180 | ||
97cd0ef3 JB |
181 | const $toast = useToast() |
182 | ||
240fa4da | 183 | const getSimulatorState = (): void => { |
26cf7d99 JB |
184 | if (state.value.gettingSimulatorState === false) { |
185 | state.value.gettingSimulatorState = true | |
3b0c6e17 | 186 | uiClient |
26cf7d99 | 187 | .simulatorState() |
3b0c6e17 | 188 | .then((response: ResponsePayload) => { |
26cf7d99 | 189 | state.value.simulatorState = response.state as SimulatorState |
3b0c6e17 JB |
190 | }) |
191 | .catch((error: Error) => { | |
26cf7d99 JB |
192 | $toast.error('Error at fetching simulator state') |
193 | console.error('Error at fetching simulator state:', error) | |
3b0c6e17 | 194 | }) |
240fa4da | 195 | .finally(() => { |
26cf7d99 JB |
196 | state.value.renderSimulator = randomUUID() |
197 | state.value.gettingSimulatorState = false | |
240fa4da | 198 | }) |
26cf7d99 JB |
199 | } |
200 | } | |
201 | ||
97cd0ef3 JB |
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 | } | |
916fe456 JB |
223 | } |
224 | ||
26cf7d99 JB |
225 | const getChargingStations = (): void => { |
226 | if (state.value.gettingChargingStations === false) { | |
227 | state.value.gettingChargingStations = true | |
57c0ba05 JB |
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) => { | |
916fe456 JB |
236 | if (app != null) { |
237 | app.appContext.config.globalProperties.$chargingStations = [] | |
238 | } | |
cea23fa0 | 239 | $toast.error('Error at fetching charging stations') |
57c0ba05 JB |
240 | console.error('Error at fetching charging stations:', error) |
241 | }) | |
242 | .finally(() => { | |
26cf7d99 JB |
243 | state.value.renderChargingStations = randomUUID() |
244 | state.value.gettingChargingStations = false | |
57c0ba05 | 245 | }) |
2113b3c6 | 246 | } |
32de5a57 | 247 | } |
5a010bf0 | 248 | |
aa9b0a1e JB |
249 | const getData = (): void => { |
250 | getSimulatorState() | |
251 | getTemplates() | |
252 | getChargingStations() | |
253 | } | |
254 | ||
255 | const registerWSEventListeners = () => { | |
256 | uiClient.registerWSEventListener('open', getData) | |
97cd0ef3 JB |
257 | uiClient.registerWSEventListener('error', clearChargingStations) |
258 | uiClient.registerWSEventListener('close', clearChargingStations) | |
259 | } | |
260 | ||
aa9b0a1e JB |
261 | const unregisterWSEventListeners = () => { |
262 | uiClient.unregisterWSEventListener('open', getData) | |
263 | uiClient.unregisterWSEventListener('error', clearChargingStations) | |
264 | uiClient.unregisterWSEventListener('close', clearChargingStations) | |
265 | } | |
266 | ||
97cd0ef3 | 267 | onMounted(() => { |
aa9b0a1e JB |
268 | registerWSEventListeners() |
269 | }) | |
270 | ||
271 | onUnmounted(() => { | |
272 | unregisterWSEventListeners() | |
97cd0ef3 JB |
273 | }) |
274 | ||
275 | const uiServerConfigurations: { index: number; configuration: UIServerConfigurationSection }[] = | |
276 | app?.appContext.config.globalProperties.$configuration.uiServer.map( | |
277 | (configuration: UIServerConfigurationSection, index: number) => ({ | |
278 | index, | |
279 | configuration | |
280 | }) | |
281 | ) | |
282 | ||
fa5d129a | 283 | const startSimulator = (): void => { |
cea23fa0 JB |
284 | uiClient |
285 | .startSimulator() | |
286 | .then(() => { | |
287 | $toast.success('Simulator successfully started') | |
288 | }) | |
289 | .catch((error: Error) => { | |
290 | $toast.error('Error at starting simulator') | |
291 | console.error('Error at starting simulator:', error) | |
292 | }) | |
240fa4da JB |
293 | .finally(() => { |
294 | getSimulatorState() | |
295 | }) | |
5a010bf0 | 296 | } |
fa5d129a | 297 | const stopSimulator = (): void => { |
cea23fa0 JB |
298 | uiClient |
299 | .stopSimulator() | |
300 | .then(() => { | |
5c0e9352 JB |
301 | if (app != null) { |
302 | app.appContext.config.globalProperties.$chargingStations = [] | |
303 | } | |
cea23fa0 JB |
304 | $toast.success('Simulator successfully stopped') |
305 | }) | |
306 | .catch((error: Error) => { | |
307 | $toast.error('Error at stopping simulator') | |
308 | console.error('Error at stopping simulator:', error) | |
309 | }) | |
240fa4da JB |
310 | .finally(() => { |
311 | getSimulatorState() | |
312 | }) | |
5a010bf0 | 313 | } |
32de5a57 LM |
314 | </script> |
315 | ||
316 | <style> | |
ca1e5439 | 317 | #charging-stations-container { |
1d41bc6b | 318 | height: fit-content; |
32de5a57 | 319 | width: 100%; |
5a010bf0 | 320 | display: flex; |
32de5a57 | 321 | flex-direction: column; |
5a010bf0 JB |
322 | } |
323 | ||
0344ad2b JB |
324 | #ui-server-container { |
325 | display: flex; | |
239bd875 | 326 | justify-content: center; |
754d8199 | 327 | border-style: outset; |
0344ad2b JB |
328 | } |
329 | ||
330 | #ui-server-selector { | |
331 | width: 100%; | |
239bd875 | 332 | background-color: rgb(239, 239, 239); |
c6308554 | 333 | font: small-caption; |
0344ad2b JB |
334 | text-align: center; |
335 | } | |
336 | ||
239bd875 | 337 | #ui-server-selector:hover { |
bad93db3 | 338 | background-color: rgb(229, 229, 229); |
239bd875 JB |
339 | } |
340 | ||
13c19b7b JB |
341 | #buttons-container { |
342 | display: flex; | |
343 | flex-direction: row; | |
344 | } | |
345 | ||
6027002f | 346 | .simulator-start-button { |
3802683b JB |
347 | color: ivory; |
348 | background-color: green; | |
349 | } | |
350 | ||
fec03138 | 351 | .simulator-start-button:hover { |
fec03138 JB |
352 | background-color: rgb(0, 98, 0); |
353 | } | |
354 | ||
6027002f | 355 | .simulator-stop-button { |
3802683b JB |
356 | color: ivory; |
357 | background-color: red; | |
358 | } | |
359 | ||
fec03138 JB |
360 | .simulator-stop-button:hover { |
361 | background-color: rgb(225, 0, 0); | |
362 | } | |
363 | ||
14ee627a JB |
364 | #action-button { |
365 | flex: none; | |
878855a2 JB |
366 | } |
367 | ||
32de5a57 | 368 | #reload-button { |
3802683b | 369 | color: ivory; |
9dc8b66f | 370 | background-color: blue; |
c6308554 | 371 | font-size: 2rem; |
32de5a57 LM |
372 | } |
373 | ||
374 | #reload-button:hover { | |
9dc8b66f | 375 | background-color: rgb(0, 0, 225); |
32de5a57 LM |
376 | } |
377 | ||
378 | #reload-button:active { | |
4b10e4f9 | 379 | background-color: darkblue; |
32de5a57 | 380 | } |
229d8c34 JB |
381 | |
382 | #action { | |
916f0920 | 383 | min-width: max-content; |
3802683b | 384 | color: ivory; |
229d8c34 | 385 | background-color: black; |
916f0920 | 386 | padding: 0.8%; |
229d8c34 | 387 | } |
32de5a57 | 388 | </style> |