1 import type { ChargingStation
} from
'./ChargingStation';
2 import type { ConfigurationKey
, ConfigurationKeyType
} from
'../types';
3 import { logger
} from
'../utils';
5 interface ConfigurationKeyOptions
{
10 interface DeleteConfigurationKeyParams
{
12 caseInsensitive
?: boolean;
14 interface AddConfigurationKeyParams
{
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();
28 return configElement
.key
=== key
;
32 export const addConfigurationKey
= (
33 chargingStation
: ChargingStation
,
34 key
: ConfigurationKeyType
,
36 options
?: ConfigurationKeyOptions
,
37 params
?: AddConfigurationKeyParams
,
47 params
= { ...{ overwrite
: false, save
: false }, ...params
};
48 let keyFound
= getConfigurationKey(chargingStation
, key
);
49 if (keyFound
&& params
?.overwrite
) {
50 deleteConfigurationKey(chargingStation
, keyFound
.key
, {
56 chargingStation
.ocppConfiguration
?.configurationKey
?.push({
58 readonly: options
.readonly!,
60 visible
: options
.visible
,
61 reboot
: options
.reboot
,
63 params
?.save
&& chargingStation
.saveOcppConfiguration();
66 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
72 export const setConfigurationKeyValue
= (
73 chargingStation
: ChargingStation
,
74 key
: ConfigurationKeyType
,
76 caseInsensitive
= false,
78 const keyFound
= getConfigurationKey(chargingStation
, key
, caseInsensitive
);
80 chargingStation
.ocppConfiguration
!.configurationKey
![
81 chargingStation
.ocppConfiguration
!.configurationKey
!.indexOf(keyFound
)
83 chargingStation
.saveOcppConfiguration();
86 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
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
);
100 const deletedConfigurationKey
= chargingStation
.ocppConfiguration
?.configurationKey
?.splice(
101 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
),
104 params
?.save
&& chargingStation
.saveOcppConfiguration();
105 return deletedConfigurationKey
;