1 import type { ChargingStation
} from
'./ChargingStation.js'
2 import type { ConfigurationKey
, ConfigurationKeyType
} from
'../types/index.js'
3 import { logger
} from
'../utils/index.js'
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
,
37 params
?: AddConfigurationKeyParams
47 params
= { ...{ overwrite
: false, save
: false }, ...params
}
48 let keyFound
= getConfigurationKey(chargingStation
, key
)
49 if (keyFound
!= null && params
.overwrite
=== true) {
50 deleteConfigurationKey(chargingStation
, keyFound
.key
, {
55 if (keyFound
== null) {
56 chargingStation
.ocppConfiguration
?.configurationKey
?.push({
58 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
59 readonly: options
.readonly!,
61 visible
: options
.visible
,
62 reboot
: options
.reboot
64 params
.save
=== true && chargingStation
.saveOcppConfiguration()
67 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
73 export const setConfigurationKeyValue
= (
74 chargingStation
: ChargingStation
,
75 key
: ConfigurationKeyType
,
77 caseInsensitive
= false
78 ): ConfigurationKey
| undefined => {
79 const keyFound
= getConfigurationKey(chargingStation
, key
, caseInsensitive
)
80 if (keyFound
!= null) {
81 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
82 chargingStation
.ocppConfiguration
!.configurationKey
![
83 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
84 chargingStation
.ocppConfiguration
!.configurationKey
!.indexOf(keyFound
)
86 chargingStation
.saveOcppConfiguration()
89 `${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
100 ): ConfigurationKey
[] | undefined => {
101 params
= { ...{ save
: true, caseInsensitive
: false }, ...params
}
102 const keyFound
= getConfigurationKey(chargingStation
, key
, params
.caseInsensitive
)
103 if (keyFound
!= null) {
104 const deletedConfigurationKey
= chargingStation
.ocppConfiguration
?.configurationKey
?.splice(
105 chargingStation
.ocppConfiguration
.configurationKey
.indexOf(keyFound
),
108 params
.save
=== true && chargingStation
.saveOcppConfiguration()
109 return deletedConfigurationKey