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
= {
41 params
: AddConfigurationKeyParams
= { overwrite
: false, save
: false },
51 params
= { ...{ overwrite
: false, save
: false }, ...params
};
52 let keyFound
= getConfigurationKey(chargingStation
, key
);
53 if (keyFound
&& params
?.overwrite
) {
54 deleteConfigurationKey(chargingStation
, keyFound
.key
, {
60 chargingStation
.ocppConfiguration
?.configurationKey
?.push({
62 readonly: options
.readonly!,
64 visible
: options
.visible
,
65 reboot
: options
.reboot
,
67 params
?.save
&& chargingStation
.saveOcppConfiguration();
70 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
76 export const setConfigurationKeyValue
= (
77 chargingStation
: ChargingStation
,
78 key
: ConfigurationKeyType
,
80 caseInsensitive
= false,
82 const keyFound
= getConfigurationKey(chargingStation
, key
, caseInsensitive
);
84 chargingStation
.ocppConfiguration
!.configurationKey
![
85 chargingStation
.ocppConfiguration
!.configurationKey
!.indexOf(keyFound
)
87 chargingStation
.saveOcppConfiguration();
90 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
96 export const deleteConfigurationKey
= (
97 chargingStation
: ChargingStation
,
98 key
: ConfigurationKeyType
,
99 params
: DeleteConfigurationKeyParams
= { save
: true, caseInsensitive
: false },
100 ): ConfigurationKey
[] | undefined => {
101 params
= { ...{ save
: true, caseInsensitive
: false }, ...params
};
102 const keyFound
= getConfigurationKey(chargingStation
, key
, params
?.caseInsensitive
);
104 const deletedConfigurationKey
= chargingStation
.ocppConfiguration
?.configurationKey
?.splice(
105 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
),
108 params
?.save
&& chargingStation
.saveOcppConfiguration();
109 return deletedConfigurationKey
;