| 1 | import type { ChargingStation } from './ChargingStation'; |
| 2 | import type { ConfigurationKey, ConfigurationKeyType } from '../types'; |
| 3 | import { logger } from '../utils'; |
| 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 | readonly: false, |
| 38 | visible: true, |
| 39 | reboot: false, |
| 40 | }, |
| 41 | params: AddConfigurationKeyParams = { overwrite: false, save: false }, |
| 42 | ): void => { |
| 43 | options = { |
| 44 | ...{ |
| 45 | readonly: false, |
| 46 | visible: true, |
| 47 | reboot: false, |
| 48 | }, |
| 49 | ...options, |
| 50 | }; |
| 51 | params = { ...{ overwrite: false, save: false }, ...params }; |
| 52 | let keyFound = getConfigurationKey(chargingStation, key); |
| 53 | if (keyFound && params?.overwrite) { |
| 54 | deleteConfigurationKey(chargingStation, keyFound.key, { |
| 55 | save: false, |
| 56 | }); |
| 57 | keyFound = undefined; |
| 58 | } |
| 59 | if (!keyFound) { |
| 60 | chargingStation.ocppConfiguration?.configurationKey?.push({ |
| 61 | key, |
| 62 | readonly: options.readonly!, |
| 63 | value, |
| 64 | visible: options.visible, |
| 65 | reboot: options.reboot, |
| 66 | }); |
| 67 | params?.save && chargingStation.saveOcppConfiguration(); |
| 68 | } else { |
| 69 | logger.error( |
| 70 | `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`, |
| 71 | keyFound, |
| 72 | ); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | export const setConfigurationKeyValue = ( |
| 77 | chargingStation: ChargingStation, |
| 78 | key: ConfigurationKeyType, |
| 79 | value: string, |
| 80 | caseInsensitive = false, |
| 81 | ): void => { |
| 82 | const keyFound = getConfigurationKey(chargingStation, key, caseInsensitive); |
| 83 | if (keyFound) { |
| 84 | chargingStation.ocppConfiguration!.configurationKey![ |
| 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 | }; |
| 95 | |
| 96 | export const deleteConfigurationKey = ( |
| 97 | chargingStation: ChargingStation, |
| 98 | key: ConfigurationKeyType, |
| 99 | params: DeleteConfigurationKeyParams = { save: true, caseInsensitive: false }, |
| 100 | ): ConfigurationKey[] | undefined => { |
| 101 | params = { ...{ save: true, caseInsensitive: false }, ...params }; |
| 102 | const keyFound = getConfigurationKey(chargingStation, key, params?.caseInsensitive); |
| 103 | if (keyFound) { |
| 104 | const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice( |
| 105 | chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound), |
| 106 | 1, |
| 107 | ); |
| 108 | params?.save && chargingStation.saveOcppConfiguration(); |
| 109 | return deletedConfigurationKey; |
| 110 | } |
| 111 | }; |