X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FConfigurationKeyUtils.ts;h=075a497988ae2f5375a665568be67bd7fe8dae82;hb=3000c1258ccda0c8828f4e4fffb988e5ffaa84da;hp=d1d093f7e978d832453b1bc1e585460d2e09f227;hpb=4e3b1d6bdc767ec63949b998f0375fe2b5bc2b28;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ConfigurationKeyUtils.ts b/src/charging-station/ConfigurationKeyUtils.ts index d1d093f7..075a4979 100644 --- a/src/charging-station/ConfigurationKeyUtils.ts +++ b/src/charging-station/ConfigurationKeyUtils.ts @@ -1,107 +1,111 @@ -import type { ChargingStation } from './ChargingStation'; -import type { ConfigurationKey, ConfigurationKeyType } from '../types'; -import { logger } from '../utils'; +import type { ChargingStation } from './ChargingStation.js' +import type { ConfigurationKey, ConfigurationKeyType } from '../types/index.js' +import { logger } from '../utils/index.js' interface ConfigurationKeyOptions { - readonly?: boolean; - visible?: boolean; - reboot?: boolean; + readonly?: boolean + visible?: boolean + reboot?: boolean } interface DeleteConfigurationKeyParams { - save?: boolean; - caseInsensitive?: boolean; + save?: boolean + caseInsensitive?: boolean } interface AddConfigurationKeyParams { - overwrite?: boolean; - save?: boolean; + overwrite?: boolean + save?: boolean } export const getConfigurationKey = ( chargingStation: ChargingStation, key: ConfigurationKeyType, - caseInsensitive = false, + caseInsensitive = false ): ConfigurationKey | undefined => { - return chargingStation.ocppConfiguration?.configurationKey?.find((configElement) => { + return chargingStation.ocppConfiguration?.configurationKey?.find(configElement => { if (caseInsensitive) { - return configElement.key.toLowerCase() === key.toLowerCase(); + return configElement.key.toLowerCase() === key.toLowerCase() } - return configElement.key === key; - }); -}; + return configElement.key === key + }) +} export const addConfigurationKey = ( chargingStation: ChargingStation, key: ConfigurationKeyType, value: string, options?: ConfigurationKeyOptions, - params?: AddConfigurationKeyParams, + params?: AddConfigurationKeyParams ): void => { options = { ...{ readonly: false, visible: true, - reboot: false, + reboot: false }, - ...options, - }; - params = { ...{ overwrite: false, save: false }, ...params }; - let keyFound = getConfigurationKey(chargingStation, key); - if (keyFound !== undefined && params?.overwrite === true) { + ...options + } + params = { ...{ overwrite: false, save: false }, ...params } + let keyFound = getConfigurationKey(chargingStation, key) + if (keyFound != null && params.overwrite === true) { deleteConfigurationKey(chargingStation, keyFound.key, { - save: false, - }); - keyFound = undefined; + save: false + }) + keyFound = undefined } - if (keyFound === undefined) { + if (keyFound == null) { chargingStation.ocppConfiguration?.configurationKey?.push({ key, + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion readonly: options.readonly!, value, visible: options.visible, - reboot: options.reboot, - }); - params?.save && chargingStation.saveOcppConfiguration(); + reboot: options.reboot + }) + params.save === true && chargingStation.saveOcppConfiguration() } else { logger.error( `${chargingStation.logPrefix()} Trying to add an already existing configuration key: %j`, - keyFound, - ); + keyFound + ) } -}; +} export const setConfigurationKeyValue = ( chargingStation: ChargingStation, key: ConfigurationKeyType, value: string, - caseInsensitive = false, -): void => { - const keyFound = getConfigurationKey(chargingStation, key, caseInsensitive); - if (keyFound !== undefined) { + caseInsensitive = false +): ConfigurationKey | undefined => { + const keyFound = getConfigurationKey(chargingStation, key, caseInsensitive) + if (keyFound != null) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion chargingStation.ocppConfiguration!.configurationKey![ + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion chargingStation.ocppConfiguration!.configurationKey!.indexOf(keyFound) - ].value = value; - chargingStation.saveOcppConfiguration(); + ].value = value + chargingStation.saveOcppConfiguration() } else { logger.error( `${chargingStation.logPrefix()} Trying to set a value on a non existing configuration key: %j`, - { key, value }, - ); + { key, value } + ) } -}; + return keyFound +} export const deleteConfigurationKey = ( chargingStation: ChargingStation, key: ConfigurationKeyType, - params?: DeleteConfigurationKeyParams, + params?: DeleteConfigurationKeyParams ): ConfigurationKey[] | undefined => { - params = { ...{ save: true, caseInsensitive: false }, ...params }; - const keyFound = getConfigurationKey(chargingStation, key, params?.caseInsensitive); - if (keyFound !== undefined) { + params = { ...{ save: true, caseInsensitive: false }, ...params } + const keyFound = getConfigurationKey(chargingStation, key, params.caseInsensitive) + if (keyFound != null) { const deletedConfigurationKey = chargingStation.ocppConfiguration?.configurationKey?.splice( chargingStation.ocppConfiguration.configurationKey.indexOf(keyFound), - 1, - ); - params?.save && chargingStation.saveOcppConfiguration(); - return deletedConfigurationKey; + 1 + ) + params.save === true && chargingStation.saveOcppConfiguration() + return deletedConfigurationKey } -}; +}