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