Reduce charging station instance memory footprint
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStationConfigurationUtils.ts
1 import ChargingStation from './ChargingStation';
2 import { ConfigurationKey } from '../types/ChargingStationOcppConfiguration';
3 import { StandardParametersKey } from '../types/ocpp/Configuration';
4 import logger from '../utils/Logger';
5
6 export class ChargingStationConfigurationUtils {
7 public static getConfigurationKey(
8 chargingStation: ChargingStation,
9 key: string | StandardParametersKey,
10 caseInsensitive = false
11 ): ConfigurationKey | undefined {
12 return chargingStation.ocppConfiguration.configurationKey.find((configElement) => {
13 if (caseInsensitive) {
14 return configElement.key.toLowerCase() === key.toLowerCase();
15 }
16 return configElement.key === key;
17 });
18 }
19
20 public static addConfigurationKey(
21 chargingStation: ChargingStation,
22 key: string | StandardParametersKey,
23 value: string,
24 options: { readonly?: boolean; visible?: boolean; reboot?: boolean } = {
25 readonly: false,
26 visible: true,
27 reboot: false,
28 },
29 params: { overwrite?: boolean; save?: boolean } = { overwrite: false, save: false }
30 ): void {
31 options = options ?? ({} as { readonly?: boolean; visible?: boolean; reboot?: boolean });
32 options.readonly = options?.readonly ?? false;
33 options.visible = options?.visible ?? true;
34 options.reboot = options?.reboot ?? false;
35 let keyFound = ChargingStationConfigurationUtils.getConfigurationKey(chargingStation, key);
36 if (keyFound && params?.overwrite) {
37 ChargingStationConfigurationUtils.deleteConfigurationKey(chargingStation, keyFound.key, {
38 save: false,
39 });
40 keyFound = undefined;
41 }
42 if (!keyFound) {
43 chargingStation.ocppConfiguration.configurationKey.push({
44 key,
45 readonly: options.readonly,
46 value,
47 visible: options.visible,
48 reboot: options.reboot,
49 });
50 params?.save && chargingStation.saveOcppConfiguration();
51 } else {
52 logger.error(
53 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
54 keyFound
55 );
56 }
57 }
58
59 public static setConfigurationKeyValue(
60 chargingStation: ChargingStation,
61 key: string | StandardParametersKey,
62 value: string,
63 caseInsensitive = false
64 ): void {
65 const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
66 chargingStation,
67 key,
68 caseInsensitive
69 );
70 if (keyFound) {
71 chargingStation.ocppConfiguration.configurationKey[
72 chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound)
73 ].value = value;
74 chargingStation.saveOcppConfiguration();
75 } else {
76 logger.error(
77 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
78 { key, value }
79 );
80 }
81 }
82
83 public static deleteConfigurationKey(
84 chargingStation: ChargingStation,
85 key: string | StandardParametersKey,
86 params: { save?: boolean; caseInsensitive?: boolean } = { save: true, caseInsensitive: false }
87 ): ConfigurationKey[] {
88 const keyFound = ChargingStationConfigurationUtils.getConfigurationKey(
89 chargingStation,
90 key,
91 params?.caseInsensitive
92 );
93 if (keyFound) {
94 const deletedConfigurationKey = chargingStation.ocppConfiguration.configurationKey.splice(
95 chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound),
96 1
97 );
98 params?.save && chargingStation.saveOcppConfiguration();
99 return deletedConfigurationKey;
100 }
101 }
102 }