Commit | Line | Data |
---|---|---|
66a7748d JB |
1 | import type { ChargingStation } from './ChargingStation.js' |
2 | import type { ConfigurationKey, ConfigurationKeyType } from '../types/index.js' | |
3 | import { logger } from '../utils/index.js' | |
17ac262c | 4 | |
e1d9a0f4 | 5 | interface ConfigurationKeyOptions { |
66a7748d JB |
6 | readonly?: boolean |
7 | visible?: boolean | |
8 | reboot?: boolean | |
e1d9a0f4 JB |
9 | } |
10 | interface DeleteConfigurationKeyParams { | |
66a7748d JB |
11 | save?: boolean |
12 | caseInsensitive?: boolean | |
e1d9a0f4 JB |
13 | } |
14 | interface AddConfigurationKeyParams { | |
66a7748d JB |
15 | overwrite?: boolean |
16 | save?: boolean | |
e1d9a0f4 | 17 | } |
a723e7e9 | 18 | |
f2d5e3d9 JB |
19 | export 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 |
32 | export 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) | |
a807045b | 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 | }) | |
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 |
73 | export const setConfigurationKeyValue = ( |
74 | chargingStation: ChargingStation, | |
75 | key: ConfigurationKeyType, | |
76 | value: string, | |
66a7748d | 77 | caseInsensitive = false |
f2d5e3d9 | 78 | ): void => { |
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 | } |
66a7748d | 93 | } |
f2d5e3d9 JB |
94 | |
95 | export const deleteConfigurationKey = ( | |
96 | chargingStation: ChargingStation, | |
97 | key: ConfigurationKeyType, | |
66a7748d | 98 | params?: DeleteConfigurationKeyParams |
f2d5e3d9 | 99 | ): ConfigurationKey[] | undefined => { |
66a7748d JB |
100 | params = { ...{ save: true, caseInsensitive: false }, ...params } |
101 | const keyFound = getConfigurationKey(chargingStation, key, params?.caseInsensitive) | |
a807045b | 102 | if (keyFound != null) { |
f2d5e3d9 JB |
103 | const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice( |
104 | chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound), | |
66a7748d JB |
105 | 1 |
106 | ) | |
107 | params?.save === true && chargingStation.saveOcppConfiguration() | |
108 | return deletedConfigurationKey | |
f2d5e3d9 | 109 | } |
66a7748d | 110 | } |