Commit | Line | Data |
---|---|---|
4c3c0d59 | 1 | import type { ChargingStation } from './ChargingStation'; |
02ff3d27 | 2 | import type { ConfigurationKey, ConfigurationKeyType } from '../types'; |
692f2f64 | 3 | import { logger } from '../utils'; |
17ac262c | 4 | |
e1d9a0f4 JB |
5 | interface ConfigurationKeyOptions { |
6 | readonly?: boolean; | |
7 | visible?: boolean; | |
8 | reboot?: boolean; | |
9 | } | |
10 | interface DeleteConfigurationKeyParams { | |
11 | save?: boolean; | |
12 | caseInsensitive?: boolean; | |
13 | } | |
14 | interface AddConfigurationKeyParams { | |
15 | overwrite?: boolean; | |
16 | save?: boolean; | |
17 | } | |
a723e7e9 | 18 | |
f2d5e3d9 JB |
19 | export const getConfigurationKey = ( |
20 | chargingStation: ChargingStation, | |
21 | key: ConfigurationKeyType, | |
22 | caseInsensitive = false, | |
23 | ): ConfigurationKey | undefined => { | |
24 | return chargingStation.ocppConfiguration?.configurationKey?.find((configElement) => { | |
25 | if (caseInsensitive) { | |
26 | return configElement.key.toLowerCase() === key.toLowerCase(); | |
27 | } | |
28 | return configElement.key === key; | |
29 | }); | |
30 | }; | |
17ac262c | 31 | |
f2d5e3d9 JB |
32 | export const addConfigurationKey = ( |
33 | chargingStation: ChargingStation, | |
34 | key: ConfigurationKeyType, | |
35 | value: string, | |
36 | options: ConfigurationKeyOptions = { | |
37 | readonly: false, | |
38 | visible: true, | |
39 | reboot: false, | |
40 | }, | |
41 | params: AddConfigurationKeyParams = { overwrite: false, save: false }, | |
42 | ): void => { | |
43 | options = { | |
44 | ...{ | |
17ac262c JB |
45 | readonly: false, |
46 | visible: true, | |
47 | reboot: false, | |
48 | }, | |
f2d5e3d9 JB |
49 | ...options, |
50 | }; | |
51 | params = { ...{ overwrite: false, save: false }, ...params }; | |
52 | let keyFound = getConfigurationKey(chargingStation, key); | |
53 | if (keyFound && params?.overwrite) { | |
54 | deleteConfigurationKey(chargingStation, keyFound.key, { | |
55 | save: false, | |
56 | }); | |
57 | keyFound = undefined; | |
17ac262c | 58 | } |
f2d5e3d9 JB |
59 | if (!keyFound) { |
60 | chargingStation.ocppConfiguration?.configurationKey?.push({ | |
17ac262c | 61 | key, |
f2d5e3d9 JB |
62 | readonly: options.readonly!, |
63 | value, | |
64 | visible: options.visible, | |
65 | reboot: options.reboot, | |
66 | }); | |
67 | params?.save && chargingStation.saveOcppConfiguration(); | |
68 | } else { | |
69 | logger.error( | |
70 | `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`, | |
71 | keyFound, | |
17ac262c | 72 | ); |
17ac262c | 73 | } |
f2d5e3d9 | 74 | }; |
17ac262c | 75 | |
f2d5e3d9 JB |
76 | export const setConfigurationKeyValue = ( |
77 | chargingStation: ChargingStation, | |
78 | key: ConfigurationKeyType, | |
79 | value: string, | |
80 | caseInsensitive = false, | |
81 | ): void => { | |
82 | const keyFound = getConfigurationKey(chargingStation, key, caseInsensitive); | |
83 | if (keyFound) { | |
84 | chargingStation.ocppConfiguration!.configurationKey![ | |
85 | chargingStation.ocppConfiguration!.configurationKey!.indexOf(keyFound) | |
86 | ].value = value; | |
87 | chargingStation.saveOcppConfiguration(); | |
88 | } else { | |
89 | logger.error( | |
90 | `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`, | |
91 | { key, value }, | |
17ac262c | 92 | ); |
17ac262c | 93 | } |
f2d5e3d9 JB |
94 | }; |
95 | ||
96 | export const deleteConfigurationKey = ( | |
97 | chargingStation: ChargingStation, | |
98 | key: ConfigurationKeyType, | |
99 | params: DeleteConfigurationKeyParams = { save: true, caseInsensitive: false }, | |
100 | ): ConfigurationKey[] | undefined => { | |
101 | params = { ...{ save: true, caseInsensitive: false }, ...params }; | |
102 | const keyFound = getConfigurationKey(chargingStation, key, params?.caseInsensitive); | |
103 | if (keyFound) { | |
104 | const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice( | |
105 | chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound), | |
106 | 1, | |
107 | ); | |
108 | params?.save && chargingStation.saveOcppConfiguration(); | |
109 | return deletedConfigurationKey; | |
110 | } | |
111 | }; |