1 import type { ChargingStation
} from
'./internal';
2 import type { ConfigurationKey
, StandardParametersKey
} 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
: string | StandardParametersKey
,
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
: string | StandardParametersKey
,
31 options
: ConfigurationKeyOptions
= {
36 params
: AddConfigurationKeyParams
= { overwrite
: false, save
: false }
38 options
= options
?? ({} as ConfigurationKeyOptions
);
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
, {
50 chargingStation
.ocppConfiguration
?.configurationKey
?.push({
52 readonly: options
.readonly,
54 visible
: options
.visible
,
55 reboot
: options
.reboot
,
57 params
?.save
&& chargingStation
.saveOcppConfiguration();
60 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
66 public static setConfigurationKeyValue(
67 chargingStation
: ChargingStation
,
68 key
: string | StandardParametersKey
,
70 caseInsensitive
= false
72 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
78 chargingStation
.ocppConfiguration
.configurationKey
[
79 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
)
81 chargingStation
.saveOcppConfiguration();
84 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
90 public static deleteConfigurationKey(
91 chargingStation
: ChargingStation
,
92 key
: string | StandardParametersKey
,
93 params
: DeleteConfigurationKeyParams
= { save
: true, caseInsensitive
: false }
94 ): ConfigurationKey
[] | undefined {
95 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
98 params
?.caseInsensitive
101 const deletedConfigurationKey
= chargingStation
.ocppConfiguration
?.configurationKey
?.splice(
102 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
),
105 params
?.save
&& chargingStation
.saveOcppConfiguration();
106 return deletedConfigurationKey
;