build(deps-dev): apply updates
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationConfigurationUtils.ts
1 import type { ChargingStation } from './ChargingStation';
2 import type { ConfigurationKey, ConfigurationKeyType } from '../types';
3 import { logger } from '../utils';
4
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 }
18
19 export class ChargingStationConfigurationUtils {
20 private constructor() {
21 // This is intentional
22 }
23
24 public static getConfigurationKey(
25 chargingStation: ChargingStation,
26 key: ConfigurationKeyType,
27 caseInsensitive = false,
28 ): ConfigurationKey | undefined {
29 return chargingStation.ocppConfiguration?.configurationKey?.find((configElement) => {
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,
39 key: ConfigurationKeyType,
40 value: string,
41 options: ConfigurationKeyOptions = {
42 readonly: false,
43 visible: true,
44 reboot: false,
45 },
46 params: AddConfigurationKeyParams = { overwrite: false, save: false },
47 ): void {
48 options = {
49 ...{
50 readonly: false,
51 visible: true,
52 reboot: false,
53 },
54 ...options,
55 };
56 params = { ...{ overwrite: false, save: false }, ...params };
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) {
65 chargingStation.ocppConfiguration?.configurationKey?.push({
66 key,
67 readonly: options.readonly!,
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`,
76 keyFound,
77 );
78 }
79 }
80
81 public static setConfigurationKeyValue(
82 chargingStation: ChargingStation,
83 key: ConfigurationKeyType,
84 value: string,
85 caseInsensitive = false,
86 ): void {
87 const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
88 chargingStation,
89 key,
90 caseInsensitive,
91 );
92 if (keyFound && chargingStation.ocppConfiguration?.configurationKey) {
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`,
100 { key, value },
101 );
102 }
103 }
104
105 public static deleteConfigurationKey(
106 chargingStation: ChargingStation,
107 key: ConfigurationKeyType,
108 params: DeleteConfigurationKeyParams = { save: true, caseInsensitive: false },
109 ): ConfigurationKey[] | undefined {
110 params = { ...{ save: true, caseInsensitive: false }, ...params };
111 const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
112 chargingStation,
113 key,
114 params?.caseInsensitive,
115 );
116 if (keyFound) {
117 const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice(
118 chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound),
119 1,
120 );
121 params?.save && chargingStation.saveOcppConfiguration();
122 return deletedConfigurationKey;
123 }
124 }
125 }