1 import type ChargingStation from
'./ChargingStation';
2 import type { ConfigurationKey
} from
'../types/ChargingStationOcppConfiguration';
3 import type { StandardParametersKey
} from
'../types/ocpp/Configuration';
4 import logger from
'../utils/Logger';
6 type ConfigurationKeyOptions
= { readonly?: boolean; visible
?: boolean; reboot
?: boolean };
7 type DeleteConfigurationKeyParams
= { save
?: boolean; caseInsensitive
?: boolean };
8 type AddConfigurationKeyParams
= { overwrite
?: boolean; save
?: boolean };
10 export class ChargingStationConfigurationUtils
{
11 private constructor() {
12 // This is intentional
15 public static getConfigurationKey(
16 chargingStation
: ChargingStation
,
17 key
: string | StandardParametersKey
,
18 caseInsensitive
= false
19 ): ConfigurationKey
| undefined {
20 return chargingStation
.ocppConfiguration
.configurationKey
.find((configElement
) => {
21 if (caseInsensitive
) {
22 return configElement
.key
.toLowerCase() === key
.toLowerCase();
24 return configElement
.key
=== key
;
28 public static addConfigurationKey(
29 chargingStation
: ChargingStation
,
30 key
: string | StandardParametersKey
,
32 options
: ConfigurationKeyOptions
= {
37 params
: AddConfigurationKeyParams
= { overwrite
: false, save
: false }
39 options
= options
?? ({} as ConfigurationKeyOptions
);
40 options
.readonly = options
?.readonly ?? false;
41 options
.visible
= options
?.visible
?? true;
42 options
.reboot
= options
?.reboot
?? false;
43 let keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(chargingStation
, key
);
44 if (keyFound
&& params
?.overwrite
) {
45 ChargingStationConfigurationUtils
.deleteConfigurationKey(chargingStation
, keyFound
.key
, {
51 chargingStation
.ocppConfiguration
.configurationKey
.push({
53 readonly: options
.readonly,
55 visible
: options
.visible
,
56 reboot
: options
.reboot
,
58 params
?.save
&& chargingStation
.saveOcppConfiguration();
61 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
67 public static setConfigurationKeyValue(
68 chargingStation
: ChargingStation
,
69 key
: string | StandardParametersKey
,
71 caseInsensitive
= false
73 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
79 chargingStation
.ocppConfiguration
.configurationKey
[
80 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
)
82 chargingStation
.saveOcppConfiguration();
85 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
91 public static deleteConfigurationKey(
92 chargingStation
: ChargingStation
,
93 key
: string | StandardParametersKey
,
94 params
: DeleteConfigurationKeyParams
= { save
: true, caseInsensitive
: false }
95 ): ConfigurationKey
[] {
96 const keyFound
= ChargingStationConfigurationUtils
.getConfigurationKey(
99 params
?.caseInsensitive
102 const deletedConfigurationKey
= chargingStation
.ocppConfiguration
.configurationKey
.splice(
103 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
),
106 params
?.save
&& chargingStation
.saveOcppConfiguration();
107 return deletedConfigurationKey
;