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