1 import type { ConfigurationKey
, ConfigurationKeyType
} from
'../types/index.js'
2 import type { ChargingStation
} from
'./ChargingStation.js'
4 import { logger
} from
'../utils/index.js'
6 interface ConfigurationKeyOptions
{
11 interface DeleteConfigurationKeyParams
{
12 caseInsensitive
?: boolean
15 interface AddConfigurationKeyParams
{
20 export const getConfigurationKey
= (
21 chargingStation
: ChargingStation
,
22 key
: ConfigurationKeyType
,
23 caseInsensitive
= false
24 ): ConfigurationKey
| undefined => {
25 return chargingStation
.ocppConfiguration
?.configurationKey
?.find(configElement
=> {
26 if (caseInsensitive
) {
27 return configElement
.key
.toLowerCase() === key
.toLowerCase()
29 return configElement
.key
=== key
33 export const addConfigurationKey
= (
34 chargingStation
: ChargingStation
,
35 key
: ConfigurationKeyType
,
37 options
?: ConfigurationKeyOptions
,
38 params
?: AddConfigurationKeyParams
48 params
= { ...{ overwrite
: false, save
: false }, ...params
}
49 let keyFound
= getConfigurationKey(chargingStation
, key
)
50 if (keyFound
!= null && params
.overwrite
) {
51 deleteConfigurationKey(chargingStation
, keyFound
.key
, {
56 if (keyFound
== null) {
57 chargingStation
.ocppConfiguration
?.configurationKey
?.push({
59 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
60 readonly: options
.readonly!,
61 reboot
: options
.reboot
,
63 visible
: options
.visible
,
65 params
.save
&& chargingStation
.saveOcppConfiguration()
68 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
74 export const setConfigurationKeyValue
= (
75 chargingStation
: ChargingStation
,
76 key
: ConfigurationKeyType
,
78 caseInsensitive
= false
79 ): ConfigurationKey
| undefined => {
80 const keyFound
= getConfigurationKey(chargingStation
, key
, caseInsensitive
)
81 if (keyFound
!= null) {
82 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
83 chargingStation
.ocppConfiguration
!.configurationKey
![
84 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
85 chargingStation
.ocppConfiguration
!.configurationKey
!.indexOf(keyFound
)
87 chargingStation
.saveOcppConfiguration()
90 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
97 export const deleteConfigurationKey
= (
98 chargingStation
: ChargingStation
,
99 key
: ConfigurationKeyType
,
100 params
?: DeleteConfigurationKeyParams
101 ): ConfigurationKey
[] | undefined => {
102 params
= { ...{ caseInsensitive
: false, save
: true }, ...params
}
103 const keyFound
= getConfigurationKey(chargingStation
, key
, params
.caseInsensitive
)
104 if (keyFound
!= null) {
105 const deletedConfigurationKey
= chargingStation
.ocppConfiguration
?.configurationKey
?.splice(
106 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
),
109 params
.save
&& chargingStation
.saveOcppConfiguration()
110 return deletedConfigurationKey