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