|
| 1 | +/** |
| 2 | + * Copyright (c) 2024, RTE (http://www.rte-france.com) |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +import { NumberSchema } from 'yup'; |
| 9 | +import { UUID } from 'crypto'; |
| 10 | +import yup from '../../../../utils/yupConfig'; |
| 11 | +import { |
| 12 | + PARAM_SA_FLOW_PROPORTIONAL_THRESHOLD, |
| 13 | + PARAM_SA_HIGH_VOLTAGE_ABSOLUTE_THRESHOLD, |
| 14 | + PARAM_SA_HIGH_VOLTAGE_PROPORTIONAL_THRESHOLD, |
| 15 | + PARAM_SA_LOW_VOLTAGE_ABSOLUTE_THRESHOLD, |
| 16 | + PARAM_SA_LOW_VOLTAGE_PROPORTIONAL_THRESHOLD, |
| 17 | + PARAM_SA_PROVIDER, |
| 18 | +} from '../constant'; |
| 19 | + |
| 20 | +export const LIMIT_REDUCTIONS_FORM = 'limitReductionsForm'; |
| 21 | +export const VOLTAGE_LEVELS_FORM = 'voltageLevelsForm'; |
| 22 | +export const IST_FORM = 'istForm'; |
| 23 | +export const LIMIT_DURATION_FORM = 'limitReductionForm'; |
| 24 | + |
| 25 | +export interface IVoltageLevel { |
| 26 | + nominalV: number; |
| 27 | + lowBound: number; |
| 28 | + highBound: number; |
| 29 | +} |
| 30 | + |
| 31 | +export interface ILimitDuration { |
| 32 | + lowBound: number; |
| 33 | + lowClosed: boolean; |
| 34 | + highBound: number; |
| 35 | + highClosed: boolean; |
| 36 | +} |
| 37 | + |
| 38 | +export interface ITemporaryLimitReduction { |
| 39 | + reduction: number; |
| 40 | + limitDuration: ILimitDuration; |
| 41 | +} |
| 42 | + |
| 43 | +export interface ILimitReductionsByVoltageLevel { |
| 44 | + voltageLevel: IVoltageLevel; |
| 45 | + permanentLimitReduction: number; |
| 46 | + temporaryLimitReductions: ITemporaryLimitReduction[]; |
| 47 | +} |
| 48 | + |
| 49 | +export interface ISAParameters { |
| 50 | + uuid?: UUID; |
| 51 | + [PARAM_SA_PROVIDER]: string; |
| 52 | + limitReductions: ILimitReductionsByVoltageLevel[]; |
| 53 | + [PARAM_SA_FLOW_PROPORTIONAL_THRESHOLD]: number; |
| 54 | + [PARAM_SA_LOW_VOLTAGE_PROPORTIONAL_THRESHOLD]: number; |
| 55 | + [PARAM_SA_LOW_VOLTAGE_ABSOLUTE_THRESHOLD]: number; |
| 56 | + [PARAM_SA_HIGH_VOLTAGE_PROPORTIONAL_THRESHOLD]: number; |
| 57 | + [PARAM_SA_HIGH_VOLTAGE_ABSOLUTE_THRESHOLD]: number; |
| 58 | +} |
| 59 | + |
| 60 | +export enum TabValues { |
| 61 | + 'General' = 0, |
| 62 | + 'LimitReductions' = 1, |
| 63 | +} |
| 64 | + |
| 65 | +export const TAB_INFO = [ |
| 66 | + { label: TabValues[TabValues.General], developerModeOnly: false }, |
| 67 | + { label: TabValues[TabValues.LimitReductions], developerModeOnly: false }, |
| 68 | +]; |
| 69 | + |
| 70 | +export interface IColumnsDef { |
| 71 | + label: string; |
| 72 | + dataKey: string; |
| 73 | + tooltip: string; |
| 74 | + width?: string; |
| 75 | +} |
| 76 | + |
| 77 | +export const COLUMNS_DEFINITIONS_LIMIT_REDUCTIONS: IColumnsDef[] = [ |
| 78 | + { |
| 79 | + label: 'voltageRange', |
| 80 | + dataKey: VOLTAGE_LEVELS_FORM, |
| 81 | + tooltip: 'voltageRange', |
| 82 | + }, |
| 83 | + { |
| 84 | + label: 'IST', |
| 85 | + dataKey: IST_FORM, |
| 86 | + tooltip: 'IST', |
| 87 | + }, |
| 88 | +]; |
| 89 | + |
| 90 | +/* TODO: a cleaner solution can be done by using yup.array() |
| 91 | + Instead of creating a schema for each limit duration individually, |
| 92 | + we can use yup.array() to define an array of limit durations directly. */ |
| 93 | +const getLimitDurationsFormSchema = (nbLimits: number) => { |
| 94 | + const limitDurationsFormSchema: Record<string, NumberSchema> = {}; |
| 95 | + for (let i = 0; i < nbLimits; i++) { |
| 96 | + limitDurationsFormSchema[LIMIT_DURATION_FORM + i] = yup |
| 97 | + .number() |
| 98 | + .min(0, 'RealPercentage') |
| 99 | + .max(1, 'RealPercentage') |
| 100 | + .nullable() |
| 101 | + .required(); |
| 102 | + } |
| 103 | + return limitDurationsFormSchema; |
| 104 | +}; |
| 105 | + |
| 106 | +export const getLimitReductionsFormSchema = (nbTemporaryLimits: number) => { |
| 107 | + return yup |
| 108 | + .object() |
| 109 | + .shape({ |
| 110 | + [LIMIT_REDUCTIONS_FORM]: yup.array().of( |
| 111 | + yup.object().shape({ |
| 112 | + [VOLTAGE_LEVELS_FORM]: yup.string(), |
| 113 | + [IST_FORM]: yup.number().min(0, 'RealPercentage').max(1, 'RealPercentage').nullable().required(), |
| 114 | + ...getLimitDurationsFormSchema(nbTemporaryLimits), |
| 115 | + }) |
| 116 | + ), |
| 117 | + }) |
| 118 | + .required(); |
| 119 | +}; |
| 120 | + |
| 121 | +export const getSAParametersFromSchema = (limitReductions?: ILimitReductionsByVoltageLevel[]) => { |
| 122 | + const providerSchema = yup.object().shape({ |
| 123 | + [PARAM_SA_PROVIDER]: yup.string().required(), |
| 124 | + }); |
| 125 | + |
| 126 | + const limitReductionsSchema = getLimitReductionsFormSchema( |
| 127 | + limitReductions?.length ? limitReductions[0].temporaryLimitReductions.length : 0 |
| 128 | + ); |
| 129 | + |
| 130 | + const thresholdsSchema = yup.object().shape({ |
| 131 | + [PARAM_SA_FLOW_PROPORTIONAL_THRESHOLD]: yup |
| 132 | + .number() |
| 133 | + .min(0, 'NormalizedPercentage') |
| 134 | + .max(100, 'NormalizedPercentage') |
| 135 | + .required(), |
| 136 | + [PARAM_SA_LOW_VOLTAGE_PROPORTIONAL_THRESHOLD]: yup |
| 137 | + .number() |
| 138 | + .min(0, 'NormalizedPercentage') |
| 139 | + .max(100, 'NormalizedPercentage') |
| 140 | + .required(), |
| 141 | + [PARAM_SA_LOW_VOLTAGE_ABSOLUTE_THRESHOLD]: yup.number().required(), |
| 142 | + [PARAM_SA_HIGH_VOLTAGE_PROPORTIONAL_THRESHOLD]: yup |
| 143 | + .number() |
| 144 | + .min(0, 'NormalizedPercentage') |
| 145 | + .max(100, 'NormalizedPercentage') |
| 146 | + .required(), |
| 147 | + [PARAM_SA_HIGH_VOLTAGE_ABSOLUTE_THRESHOLD]: yup.number().required(), |
| 148 | + }); |
| 149 | + |
| 150 | + return yup.object().shape({ |
| 151 | + ...providerSchema.fields, |
| 152 | + ...limitReductionsSchema.fields, |
| 153 | + ...thresholdsSchema.fields, |
| 154 | + }); |
| 155 | +}; |
0 commit comments