| 1 | import type { ChargingStation } from './ChargingStation'; |
| 2 | import type { ConfigurationKey, ConfigurationKeyType } from '../types'; |
| 3 | import { logger } from '../utils'; |
| 4 | |
| 5 | type ConfigurationKeyOptions = { readonly?: boolean; visible?: boolean; reboot?: boolean }; |
| 6 | type DeleteConfigurationKeyParams = { save?: boolean; caseInsensitive?: boolean }; |
| 7 | type AddConfigurationKeyParams = { overwrite?: boolean; save?: boolean }; |
| 8 | |
| 9 | export class ChargingStationConfigurationUtils { |
| 10 | private constructor() { |
| 11 | // This is intentional |
| 12 | } |
| 13 | |
| 14 | public static getConfigurationKey( |
| 15 | chargingStation: ChargingStation, |
| 16 | key: ConfigurationKeyType, |
| 17 | caseInsensitive = false |
| 18 | ): ConfigurationKey | undefined { |
| 19 | return chargingStation.ocppConfiguration?.configurationKey?.find((configElement) => { |
| 20 | if (caseInsensitive) { |
| 21 | return configElement.key.toLowerCase() === key.toLowerCase(); |
| 22 | } |
| 23 | return configElement.key === key; |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | public static addConfigurationKey( |
| 28 | chargingStation: ChargingStation, |
| 29 | key: ConfigurationKeyType, |
| 30 | value: string, |
| 31 | options: ConfigurationKeyOptions = { |
| 32 | readonly: false, |
| 33 | visible: true, |
| 34 | reboot: false, |
| 35 | }, |
| 36 | params: AddConfigurationKeyParams = { overwrite: false, save: false } |
| 37 | ): void { |
| 38 | options = { |
| 39 | ...{ |
| 40 | readonly: false, |
| 41 | visible: true, |
| 42 | reboot: false, |
| 43 | }, |
| 44 | ...options, |
| 45 | }; |
| 46 | params = { ...{ overwrite: false, save: false }, ...params }; |
| 47 | let keyFound = ChargingStationConfigurationUtils.getConfigurationKey(chargingStation, key); |
| 48 | if (keyFound && params?.overwrite) { |
| 49 | ChargingStationConfigurationUtils.deleteConfigurationKey(chargingStation, keyFound.key, { |
| 50 | save: false, |
| 51 | }); |
| 52 | keyFound = undefined; |
| 53 | } |
| 54 | if (!keyFound) { |
| 55 | chargingStation.ocppConfiguration?.configurationKey?.push({ |
| 56 | key, |
| 57 | readonly: options.readonly, |
| 58 | value, |
| 59 | visible: options.visible, |
| 60 | reboot: options.reboot, |
| 61 | }); |
| 62 | params?.save && chargingStation.saveOcppConfiguration(); |
| 63 | } else { |
| 64 | logger.error( |
| 65 | `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`, |
| 66 | keyFound |
| 67 | ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public static setConfigurationKeyValue( |
| 72 | chargingStation: ChargingStation, |
| 73 | key: ConfigurationKeyType, |
| 74 | value: string, |
| 75 | caseInsensitive = false |
| 76 | ): void { |
| 77 | const keyFound = ChargingStationConfigurationUtils.getConfigurationKey( |
| 78 | chargingStation, |
| 79 | key, |
| 80 | caseInsensitive |
| 81 | ); |
| 82 | if (keyFound) { |
| 83 | chargingStation.ocppConfiguration.configurationKey[ |
| 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 | } |
| 94 | |
| 95 | public static deleteConfigurationKey( |
| 96 | chargingStation: ChargingStation, |
| 97 | key: ConfigurationKeyType, |
| 98 | params: DeleteConfigurationKeyParams = { save: true, caseInsensitive: false } |
| 99 | ): ConfigurationKey[] | undefined { |
| 100 | params = { ...{ save: true, caseInsensitive: false }, ...params }; |
| 101 | const keyFound = ChargingStationConfigurationUtils.getConfigurationKey( |
| 102 | chargingStation, |
| 103 | key, |
| 104 | params?.caseInsensitive |
| 105 | ); |
| 106 | if (keyFound) { |
| 107 | const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice( |
| 108 | chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound), |
| 109 | 1 |
| 110 | ); |
| 111 | params?.save && chargingStation.saveOcppConfiguration(); |
| 112 | return deletedConfigurationKey; |
| 113 | } |
| 114 | } |
| 115 | } |