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