refactor: switch eslint configuration to strict type checking
[e-mobility-charging-stations-simulator.git] / src / charging-station / ConfigurationKeyUtils.ts
CommitLineData
66a7748d
JB
1import type { ChargingStation } from './ChargingStation.js'
2import type { ConfigurationKey, ConfigurationKeyType } from '../types/index.js'
3import { logger } from '../utils/index.js'
17ac262c 4
e1d9a0f4 5interface ConfigurationKeyOptions {
66a7748d
JB
6 readonly?: boolean
7 visible?: boolean
8 reboot?: boolean
e1d9a0f4
JB
9}
10interface DeleteConfigurationKeyParams {
66a7748d
JB
11 save?: boolean
12 caseInsensitive?: boolean
e1d9a0f4
JB
13}
14interface AddConfigurationKeyParams {
66a7748d
JB
15 overwrite?: boolean
16 save?: boolean
e1d9a0f4 17}
a723e7e9 18
f2d5e3d9
JB
19export const getConfigurationKey = (
20 chargingStation: ChargingStation,
21 key: ConfigurationKeyType,
66a7748d 22 caseInsensitive = false
f2d5e3d9
JB
23): ConfigurationKey | undefined => {
24 return chargingStation.ocppConfiguration?.configurationKey?.find((configElement) => {
25 if (caseInsensitive) {
66a7748d 26 return configElement.key.toLowerCase() === key.toLowerCase()
f2d5e3d9 27 }
66a7748d
JB
28 return configElement.key === key
29 })
30}
17ac262c 31
f2d5e3d9
JB
32export const addConfigurationKey = (
33 chargingStation: ChargingStation,
34 key: ConfigurationKeyType,
35 value: string,
7f3decca 36 options?: ConfigurationKeyOptions,
66a7748d 37 params?: AddConfigurationKeyParams
f2d5e3d9
JB
38): void => {
39 options = {
40 ...{
17ac262c
JB
41 readonly: false,
42 visible: true,
66a7748d 43 reboot: false
17ac262c 44 },
66a7748d
JB
45 ...options
46 }
47 params = { ...{ overwrite: false, save: false }, ...params }
48 let keyFound = getConfigurationKey(chargingStation, key)
5199f9fd 49 if (keyFound != null && params.overwrite === true) {
f2d5e3d9 50 deleteConfigurationKey(chargingStation, keyFound.key, {
66a7748d
JB
51 save: false
52 })
53 keyFound = undefined
17ac262c 54 }
a807045b 55 if (keyFound == null) {
f2d5e3d9 56 chargingStation.ocppConfiguration?.configurationKey?.push({
17ac262c 57 key,
66a7748d 58 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
f2d5e3d9
JB
59 readonly: options.readonly!,
60 value,
61 visible: options.visible,
66a7748d
JB
62 reboot: options.reboot
63 })
5199f9fd 64 params.save === true && chargingStation.saveOcppConfiguration()
f2d5e3d9
JB
65 } else {
66 logger.error(
67 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
66a7748d
JB
68 keyFound
69 )
17ac262c 70 }
66a7748d 71}
17ac262c 72
f2d5e3d9
JB
73export const setConfigurationKeyValue = (
74 chargingStation: ChargingStation,
75 key: ConfigurationKeyType,
76 value: string,
66a7748d 77 caseInsensitive = false
13b43b2c 78): ConfigurationKey | undefined => {
66a7748d 79 const keyFound = getConfigurationKey(chargingStation, key, caseInsensitive)
a807045b 80 if (keyFound != null) {
66a7748d 81 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
f2d5e3d9 82 chargingStation.ocppConfiguration!.configurationKey![
66a7748d 83 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
f2d5e3d9 84 chargingStation.ocppConfiguration!.configurationKey!.indexOf(keyFound)
66a7748d
JB
85 ].value = value
86 chargingStation.saveOcppConfiguration()
f2d5e3d9
JB
87 } else {
88 logger.error(
89 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
66a7748d
JB
90 { key, value }
91 )
17ac262c 92 }
13b43b2c 93 return keyFound
66a7748d 94}
f2d5e3d9
JB
95
96export const deleteConfigurationKey = (
97 chargingStation: ChargingStation,
98 key: ConfigurationKeyType,
66a7748d 99 params?: DeleteConfigurationKeyParams
f2d5e3d9 100): ConfigurationKey[] | undefined => {
66a7748d 101 params = { ...{ save: true, caseInsensitive: false }, ...params }
5199f9fd 102 const keyFound = getConfigurationKey(chargingStation, key, params.caseInsensitive)
a807045b 103 if (keyFound != null) {
f2d5e3d9
JB
104 const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice(
105 chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound),
66a7748d
JB
106 1
107 )
5199f9fd 108 params.save === true && chargingStation.saveOcppConfiguration()
66a7748d 109 return deletedConfigurationKey
f2d5e3d9 110 }
66a7748d 111}