1 import type { ChargingStation
} from
'./ChargingStation';
2 import type { ConfigurationKey
, ConfigurationKeyType
} from
'../types';
3 import { logger
} from
'../utils';
5 type ConfigurationKeyOptions
= { readonly?: boolean; visible
?: boolean; reboot
?: boolean };
6 type DeleteConfigurationKeyParams
= { save
?: boolean; caseInsensitive
?: boolean };
7 type AddConfigurationKeyParams
= { overwrite
?: boolean; save
?: boolean };
9 export class ChargingStationConfigurationUtils
{
10 private constructor() {
11 // This is intentional
14 public static getConfigurationKey(
15 chargingStation
: ChargingStation
,
16 key
: ConfigurationKeyType
,
17 caseInsensitive
= false
18 ): ConfigurationKey
| undefined {
19 return chargingStation
.ocppConfiguration
?.configurationKey
?.find((configElement
) => {
20 if (caseInsensitive
) {
21 return configElement
.key
.toLowerCase() === key
.toLowerCase();
23 return configElement
.key
=== key
;
27 public static addConfigurationKey(
28 chargingStation
: ChargingStation
,
29 key
: ConfigurationKeyType
,
31 options
: ConfigurationKeyOptions
= {
36 params
: AddConfigurationKeyParams
= { overwrite
: false, save
: false }
46 params
= { ...{ overwrite
: false, save
: false }, ...params
};
47 let keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(chargingStation
, key
);
48 if (keyFound
&& params
?.overwrite
) {
49 ChargingStationConfigurationUtils
.deleteConfigurationKey(chargingStation
, keyFound
.key
, {
55 chargingStation
.ocppConfiguration
?.configurationKey
?.push({
57 readonly: options
.readonly,
59 visible
: options
.visible
,
60 reboot
: options
.reboot
,
62 params
?.save
&& chargingStation
.saveOcppConfiguration();
65 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
71 public static setConfigurationKeyValue(
72 chargingStation
: ChargingStation
,
73 key
: ConfigurationKeyType
,
75 caseInsensitive
= false
77 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
83 chargingStation
.ocppConfiguration
.configurationKey
[
84 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
)
86 chargingStation
.saveOcppConfiguration();
89 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
95 public static deleteConfigurationKey(
96 chargingStation
: ChargingStation
,
97 key
: ConfigurationKeyType
,
98 params
: DeleteConfigurationKeyParams
= { save
: true, caseInsensitive
: false }
99 ): ConfigurationKey
[] | undefined {
100 params
= { ...{ save
: true, caseInsensitive
: false }, ...params
};
101 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
104 params
?.caseInsensitive
107 const deletedConfigurationKey
= chargingStation
.ocppConfiguration
?.configurationKey
?.splice(
108 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
),
111 params
?.save
&& chargingStation
.saveOcppConfiguration();
112 return deletedConfigurationKey
;