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