fix(simulator): sanity checks to circular array inputs
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationConfigurationUtils.ts
CommitLineData
2896e06d 1import type { ChargingStation } from './internal';
6dad8e21 2import type { ConfigurationKey, ConfigurationKeyType, StandardParametersKey } from '../types';
60a74391 3import { logger } from '../utils';
17ac262c 4
a723e7e9
JB
5type ConfigurationKeyOptions = { readonly?: boolean; visible?: boolean; reboot?: boolean };
6type DeleteConfigurationKeyParams = { save?: boolean; caseInsensitive?: boolean };
7type AddConfigurationKeyParams = { overwrite?: boolean; save?: boolean };
8
17ac262c 9export 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,
17ac262c
JB
17 caseInsensitive = false
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 },
a723e7e9 36 params: AddConfigurationKeyParams = { overwrite: false, save: false }
17ac262c 37 ): void {
a723e7e9 38 options = options ?? ({} as ConfigurationKeyOptions);
17ac262c
JB
39 options.readonly = options?.readonly ?? false;
40 options.visible = options?.visible ?? true;
41 options.reboot = options?.reboot ?? false;
42 let keyFound = ChargingStationConfigurationUtils.getConfigurationKey(chargingStation, key);
43 if (keyFound && params?.overwrite) {
44 ChargingStationConfigurationUtils.deleteConfigurationKey(chargingStation, keyFound.key, {
45 save: false,
46 });
47 keyFound = undefined;
48 }
49 if (!keyFound) {
72092cfc 50 chargingStation.ocppConfiguration?.configurationKey?.push({
17ac262c
JB
51 key,
52 readonly: options.readonly,
53 value,
54 visible: options.visible,
55 reboot: options.reboot,
56 });
57 params?.save && chargingStation.saveOcppConfiguration();
58 } else {
59 logger.error(
60 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
61 keyFound
62 );
63 }
64 }
65
66 public static setConfigurationKeyValue(
67 chargingStation: ChargingStation,
6dad8e21 68 key: ConfigurationKeyType,
17ac262c
JB
69 value: string,
70 caseInsensitive = false
71 ): void {
72 const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
73 chargingStation,
74 key,
75 caseInsensitive
76 );
77 if (keyFound) {
78 chargingStation.ocppConfiguration.configurationKey[
79 chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound)
80 ].value = value;
81 chargingStation.saveOcppConfiguration();
82 } else {
83 logger.error(
84 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
85 { key, value }
86 );
87 }
88 }
89
90 public static deleteConfigurationKey(
91 chargingStation: ChargingStation,
6dad8e21 92 key: ConfigurationKeyType,
a723e7e9 93 params: DeleteConfigurationKeyParams = { save: true, caseInsensitive: false }
1895299d 94 ): ConfigurationKey[] | undefined {
17ac262c
JB
95 const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
96 chargingStation,
97 key,
98 params?.caseInsensitive
99 );
100 if (keyFound) {
72092cfc 101 const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice(
17ac262c
JB
102 chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound),
103 1
104 );
105 params?.save && chargingStation.saveOcppConfiguration();
106 return deletedConfigurationKey;
107 }
108 }
109}