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