1 import { ConfigurationKey
} from
'../types/ChargingStationOcppConfiguration';
2 import { StandardParametersKey
} from
'../types/ocpp/Configuration';
3 import logger from
'../utils/Logger';
4 import ChargingStation from
'./ChargingStation';
6 export class ChargingStationConfigurationUtils
{
7 private constructor() {
11 public static getConfigurationKey(
12 chargingStation
: ChargingStation
,
13 key
: string | StandardParametersKey
,
14 caseInsensitive
= false
15 ): ConfigurationKey
| undefined {
16 return chargingStation
.ocppConfiguration
.configurationKey
.find((configElement
) => {
17 if (caseInsensitive
) {
18 return configElement
.key
.toLowerCase() === key
.toLowerCase();
20 return configElement
.key
=== key
;
24 public static addConfigurationKey(
25 chargingStation
: ChargingStation
,
26 key
: string | StandardParametersKey
,
28 options
: { readonly?: boolean; visible
?: boolean; reboot
?: boolean } = {
33 params
: { overwrite
?: boolean; save
?: boolean } = { overwrite
: false, save
: false }
35 options
= options
?? ({} as { readonly?: boolean; visible
?: boolean; reboot
?: boolean });
36 options
.readonly = options
?.readonly ?? false;
37 options
.visible
= options
?.visible
?? true;
38 options
.reboot
= options
?.reboot
?? false;
39 let keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(chargingStation
, key
);
40 if (keyFound
&& params
?.overwrite
) {
41 ChargingStationConfigurationUtils
.deleteConfigurationKey(chargingStation
, keyFound
.key
, {
47 chargingStation
.ocppConfiguration
.configurationKey
.push({
49 readonly: options
.readonly,
51 visible
: options
.visible
,
52 reboot
: options
.reboot
,
54 params
?.save
&& chargingStation
.saveOcppConfiguration();
57 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
63 public static setConfigurationKeyValue(
64 chargingStation
: ChargingStation
,
65 key
: string | StandardParametersKey
,
67 caseInsensitive
= false
69 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
75 chargingStation
.ocppConfiguration
.configurationKey
[
76 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
)
78 chargingStation
.saveOcppConfiguration();
81 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
87 public static deleteConfigurationKey(
88 chargingStation
: ChargingStation
,
89 key
: string | StandardParametersKey
,
90 params
: { save
?: boolean; caseInsensitive
?: boolean } = { save
: true, caseInsensitive
: false }
91 ): ConfigurationKey
[] {
92 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
95 params
?.caseInsensitive
98 const deletedConfigurationKey
= chargingStation
.ocppConfiguration
.configurationKey
.splice(
99 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
),
102 params
?.save
&& chargingStation
.saveOcppConfiguration();
103 return deletedConfigurationKey
;