X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FChargingStationConfigurationUtils.ts;h=39606b66e1ea8d08cacc61b53d58607d53d9c73d;hb=e30379693404ad623548b856606a689ff92c4c84;hp=8a046715393a2e9565714aad7f4f2d6eefba74c5;hpb=8114d10e3893e96bb725ce2fca9744429ee4b75b;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ChargingStationConfigurationUtils.ts b/src/charging-station/ChargingStationConfigurationUtils.ts index 8a046715..39606b66 100644 --- a/src/charging-station/ChargingStationConfigurationUtils.ts +++ b/src/charging-station/ChargingStationConfigurationUtils.ts @@ -1,106 +1,111 @@ -import { ConfigurationKey } from '../types/ChargingStationOcppConfiguration'; -import { StandardParametersKey } from '../types/ocpp/Configuration'; -import logger from '../utils/Logger'; -import ChargingStation from './ChargingStation'; +import type { ChargingStation } from './ChargingStation'; +import type { ConfigurationKey, ConfigurationKeyType } from '../types'; +import { logger } from '../utils'; -export class ChargingStationConfigurationUtils { - private constructor() { - // This is intentional - } +interface ConfigurationKeyOptions { + readonly?: boolean; + visible?: boolean; + reboot?: boolean; +} +interface DeleteConfigurationKeyParams { + save?: boolean; + caseInsensitive?: boolean; +} +interface AddConfigurationKeyParams { + overwrite?: boolean; + save?: boolean; +} - public static getConfigurationKey( - chargingStation: ChargingStation, - key: string | StandardParametersKey, - caseInsensitive = false - ): ConfigurationKey | undefined { - return chargingStation.ocppConfiguration.configurationKey.find((configElement) => { - if (caseInsensitive) { - return configElement.key.toLowerCase() === key.toLowerCase(); - } - return configElement.key === key; - }); - } +export const getConfigurationKey = ( + chargingStation: ChargingStation, + key: ConfigurationKeyType, + caseInsensitive = false, +): ConfigurationKey | undefined => { + return chargingStation.ocppConfiguration?.configurationKey?.find((configElement) => { + if (caseInsensitive) { + return configElement.key.toLowerCase() === key.toLowerCase(); + } + return configElement.key === key; + }); +}; - public static addConfigurationKey( - chargingStation: ChargingStation, - key: string | StandardParametersKey, - value: string, - options: { readonly?: boolean; visible?: boolean; reboot?: boolean } = { +export const addConfigurationKey = ( + chargingStation: ChargingStation, + key: ConfigurationKeyType, + value: string, + options: ConfigurationKeyOptions = { + readonly: false, + visible: true, + reboot: false, + }, + params: AddConfigurationKeyParams = { overwrite: false, save: false }, +): void => { + options = { + ...{ readonly: false, visible: true, reboot: false, }, - params: { overwrite?: boolean; save?: boolean } = { overwrite: false, save: false } - ): void { - options = options ?? ({} as { readonly?: boolean; visible?: boolean; reboot?: boolean }); - options.readonly = options?.readonly ?? false; - options.visible = options?.visible ?? true; - options.reboot = options?.reboot ?? false; - let keyFound = ChargingStationConfigurationUtils.getConfigurationKey(chargingStation, key); - if (keyFound && params?.overwrite) { - ChargingStationConfigurationUtils.deleteConfigurationKey(chargingStation, keyFound.key, { - save: false, - }); - keyFound = undefined; - } - if (!keyFound) { - chargingStation.ocppConfiguration.configurationKey.push({ - key, - readonly: options.readonly, - value, - visible: options.visible, - reboot: options.reboot, - }); - params?.save && chargingStation.saveOcppConfiguration(); - } else { - logger.error( - `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`, - keyFound - ); - } + ...options, + }; + params = { ...{ overwrite: false, save: false }, ...params }; + let keyFound = getConfigurationKey(chargingStation, key); + if (keyFound && params?.overwrite) { + deleteConfigurationKey(chargingStation, keyFound.key, { + save: false, + }); + keyFound = undefined; } - - public static setConfigurationKeyValue( - chargingStation: ChargingStation, - key: string | StandardParametersKey, - value: string, - caseInsensitive = false - ): void { - const keyFound = ChargingStationConfigurationUtils.getConfigurationKey( - chargingStation, + if (!keyFound) { + chargingStation.ocppConfiguration?.configurationKey?.push({ key, - caseInsensitive + readonly: options.readonly!, + value, + visible: options.visible, + reboot: options.reboot, + }); + params?.save && chargingStation.saveOcppConfiguration(); + } else { + logger.error( + `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`, + keyFound, ); - if (keyFound) { - chargingStation.ocppConfiguration.configurationKey[ - chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound) - ].value = value; - chargingStation.saveOcppConfiguration(); - } else { - logger.error( - `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`, - { key, value } - ); - } } +}; - public static deleteConfigurationKey( - chargingStation: ChargingStation, - key: string | StandardParametersKey, - params: { save?: boolean; caseInsensitive?: boolean } = { save: true, caseInsensitive: false } - ): ConfigurationKey[] { - const keyFound = ChargingStationConfigurationUtils.getConfigurationKey( - chargingStation, - key, - params?.caseInsensitive +export const setConfigurationKeyValue = ( + chargingStation: ChargingStation, + key: ConfigurationKeyType, + value: string, + caseInsensitive = false, +): void => { + const keyFound = getConfigurationKey(chargingStation, key, caseInsensitive); + if (keyFound) { + chargingStation.ocppConfiguration!.configurationKey![ + chargingStation.ocppConfiguration!.configurationKey!.indexOf(keyFound) + ].value = value; + chargingStation.saveOcppConfiguration(); + } else { + logger.error( + `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`, + { key, value }, ); - if (keyFound) { - const deletedConfigurationKey = chargingStation.ocppConfiguration.configurationKey.splice( - chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound), - 1 - ); - params?.save && chargingStation.saveOcppConfiguration(); - return deletedConfigurationKey; - } } -} +}; + +export const deleteConfigurationKey = ( + chargingStation: ChargingStation, + key: ConfigurationKeyType, + params: DeleteConfigurationKeyParams = { save: true, caseInsensitive: false }, +): ConfigurationKey[] | undefined => { + params = { ...{ save: true, caseInsensitive: false }, ...params }; + const keyFound = getConfigurationKey(chargingStation, key, params?.caseInsensitive); + if (keyFound) { + const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice( + chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound), + 1, + ); + params?.save && chargingStation.saveOcppConfiguration(); + return deletedConfigurationKey; + } +};