1 import ChargingStation from
'./ChargingStation';
2 import { ConfigurationKey
} from
'../types/ChargingStationOcppConfiguration';
3 import { StandardParametersKey
} from
'../types/ocpp/Configuration';
4 import logger from
'../utils/Logger';
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();
16 return configElement
.key
=== key
;
20 public static addConfigurationKey(
21 chargingStation
: ChargingStation
,
22 key
: string | StandardParametersKey
,
24 options
: { readonly?: boolean; visible
?: boolean; reboot
?: boolean } = {
29 params
: { overwrite
?: boolean; save
?: boolean } = { overwrite
: false, save
: false }
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
, {
43 chargingStation
.ocppConfiguration
.configurationKey
.push({
45 readonly: options
.readonly,
47 visible
: options
.visible
,
48 reboot
: options
.reboot
,
50 params
?.save
&& chargingStation
.saveOcppConfiguration();
53 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
59 public static setConfigurationKeyValue(
60 chargingStation
: ChargingStation
,
61 key
: string | StandardParametersKey
,
63 caseInsensitive
= false
65 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
71 chargingStation
.ocppConfiguration
.configurationKey
[
72 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
)
74 chargingStation
.saveOcppConfiguration();
77 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
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(
91 params
?.caseInsensitive
94 const deletedConfigurationKey
= chargingStation
.ocppConfiguration
.configurationKey
.splice(
95 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
),
98 params
?.save
&& chargingStation
.saveOcppConfiguration();
99 return deletedConfigurationKey
;