build: switch to NodeNext module resolution
[e-mobility-charging-stations-simulator.git] / src / charging-station / ConfigurationKeyUtils.ts
1 import type { ChargingStation } from './ChargingStation.js';
2 import type { ConfigurationKey, ConfigurationKeyType } from '../types/index.js';
3 import { logger } from '../utils/index.js';
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 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 };
31
32 export const addConfigurationKey = (
33 chargingStation: ChargingStation,
34 key: ConfigurationKeyType,
35 value: string,
36 options?: ConfigurationKeyOptions,
37 params?: AddConfigurationKeyParams,
38 ): void => {
39 options = {
40 ...{
41 readonly: false,
42 visible: true,
43 reboot: false,
44 },
45 ...options,
46 };
47 params = { ...{ overwrite: false, save: false }, ...params };
48 let keyFound = getConfigurationKey(chargingStation, key);
49 if (keyFound !== undefined && params?.overwrite === true) {
50 deleteConfigurationKey(chargingStation, keyFound.key, {
51 save: false,
52 });
53 keyFound = undefined;
54 }
55 if (keyFound === undefined) {
56 chargingStation.ocppConfiguration?.configurationKey?.push({
57 key,
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,
68 );
69 }
70 };
71
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);
79 if (keyFound !== undefined) {
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 },
88 );
89 }
90 };
91
92 export const deleteConfigurationKey = (
93 chargingStation: ChargingStation,
94 key: ConfigurationKeyType,
95 params?: DeleteConfigurationKeyParams,
96 ): ConfigurationKey[] | undefined => {
97 params = { ...{ save: true, caseInsensitive: false }, ...params };
98 const keyFound = getConfigurationKey(chargingStation, key, params?.caseInsensitive);
99 if (keyFound !== undefined) {
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 };