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, | |
7f3decca JB |
36 | options?: ConfigurationKeyOptions, |
37 | params?: AddConfigurationKeyParams, | |
f2d5e3d9 JB |
38 | ): void => { |
39 | options = { | |
40 | ...{ | |
17ac262c JB |
41 | readonly: false, |
42 | visible: true, | |
43 | reboot: false, | |
44 | }, | |
f2d5e3d9 JB |
45 | ...options, |
46 | }; | |
47 | params = { ...{ overwrite: false, save: false }, ...params }; | |
48 | let keyFound = getConfigurationKey(chargingStation, key); | |
4e3b1d6b | 49 | if (keyFound !== undefined && params?.overwrite === true) { |
f2d5e3d9 JB |
50 | deleteConfigurationKey(chargingStation, keyFound.key, { |
51 | save: false, | |
52 | }); | |
53 | keyFound = undefined; | |
17ac262c | 54 | } |
4e3b1d6b | 55 | if (keyFound === undefined) { |
f2d5e3d9 | 56 | chargingStation.ocppConfiguration?.configurationKey?.push({ |
17ac262c | 57 | key, |
f2d5e3d9 JB |
58 | readonly: options.readonly!, |
59 | value, | |
60 | visible: options.visible, | |
61 | reboot: options.reboot, | |
62 | }); | |
63 | params?.save && chargingStation.saveOcppConfiguration(); | |
64 | } else { | |
65 | logger.error( | |
66 | `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`, | |
67 | keyFound, | |
17ac262c | 68 | ); |
17ac262c | 69 | } |
f2d5e3d9 | 70 | }; |
17ac262c | 71 | |
f2d5e3d9 JB |
72 | export const setConfigurationKeyValue = ( |
73 | chargingStation: ChargingStation, | |
74 | key: ConfigurationKeyType, | |
75 | value: string, | |
76 | caseInsensitive = false, | |
77 | ): void => { | |
78 | const keyFound = getConfigurationKey(chargingStation, key, caseInsensitive); | |
4e3b1d6b | 79 | if (keyFound !== undefined) { |
f2d5e3d9 JB |
80 | chargingStation.ocppConfiguration!.configurationKey![ |
81 | chargingStation.ocppConfiguration!.configurationKey!.indexOf(keyFound) | |
82 | ].value = value; | |
83 | chargingStation.saveOcppConfiguration(); | |
84 | } else { | |
85 | logger.error( | |
86 | `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`, | |
87 | { key, value }, | |
17ac262c | 88 | ); |
17ac262c | 89 | } |
f2d5e3d9 JB |
90 | }; |
91 | ||
92 | export const deleteConfigurationKey = ( | |
93 | chargingStation: ChargingStation, | |
94 | key: ConfigurationKeyType, | |
7f3decca | 95 | params?: DeleteConfigurationKeyParams, |
f2d5e3d9 JB |
96 | ): ConfigurationKey[] | undefined => { |
97 | params = { ...{ save: true, caseInsensitive: false }, ...params }; | |
98 | const keyFound = getConfigurationKey(chargingStation, key, params?.caseInsensitive); | |
4e3b1d6b | 99 | if (keyFound !== undefined) { |
f2d5e3d9 JB |
100 | const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice( |
101 | chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound), | |
102 | 1, | |
103 | ); | |
104 | params?.save && chargingStation.saveOcppConfiguration(); | |
105 | return deletedConfigurationKey; | |
106 | } | |
107 | }; |