bb0d9978cc7d110330b271ca685160fcdf2ecd1a
[e-mobility-charging-stations-simulator.git] / src / charging-station / ConfigurationKeyUtils.ts
1 import type { ChargingStation } from './ChargingStation.js'
2 import type { ConfigurationKey, ConfigurationKeyType } from '../types/index.js'
3 import { logger } from '../utils/index.js'
4
5 interface ConfigurationKeyOptions {
6 readonly?: boolean
7 visible?: boolean
8 reboot?: boolean
9 }
10 interface DeleteConfigurationKeyParams {
11 save?: boolean
12 caseInsensitive?: boolean
13 }
14 interface AddConfigurationKeyParams {
15 overwrite?: boolean
16 save?: boolean
17 }
18
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()
27 }
28 return configElement.key === key
29 })
30 }
31
32 export const addConfigurationKey = (
33 chargingStation: ChargingStation,
34 key: ConfigurationKeyType,
35 value: string,
36 options?: ConfigurationKeyOptions,
37 params?: AddConfigurationKeyParams
38 ): void => {
39 options = {
40 ...{
41 readonly: false,
42 visible: true,
43 reboot: false
44 },
45 ...options
46 }
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, {
51 save: false
52 })
53 keyFound = undefined
54 }
55 if (keyFound == null) {
56 chargingStation.ocppConfiguration?.configurationKey?.push({
57 key,
58 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
59 readonly: options.readonly!,
60 value,
61 visible: options.visible,
62 reboot: options.reboot
63 })
64 params.save === true && chargingStation.saveOcppConfiguration()
65 } else {
66 logger.error(
67 `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`,
68 keyFound
69 )
70 }
71 }
72
73 export const setConfigurationKeyValue = (
74 chargingStation: ChargingStation,
75 key: ConfigurationKeyType,
76 value: string,
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)
85 ].value = value
86 chargingStation.saveOcppConfiguration()
87 } else {
88 logger.error(
89 `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`,
90 { key, value }
91 )
92 }
93 return keyFound
94 }
95
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),
106 1
107 )
108 params.save === true && chargingStation.saveOcppConfiguration()
109 return deletedConfigurationKey
110 }
111 }