From fe64b1b8028cffb8046ca9a026a23841e509a95c Mon Sep 17 00:00:00 2001 From: Frode Helgetun Krogh <70878501+frodehk@users.noreply.github.com> Date: Mon, 16 Dec 2024 09:33:06 +0100 Subject: [PATCH] refactor: move electricity consumer --- .../domain/infrastructure/components.py | 68 ------------------- .../electricity_consumer.py | 68 +++++++++++++++++++ 2 files changed, 68 insertions(+), 68 deletions(-) create mode 100644 src/libecalc/domain/infrastructure/energy_components/electricity_consumer/electricity_consumer.py diff --git a/src/libecalc/domain/infrastructure/components.py b/src/libecalc/domain/infrastructure/components.py index 1618badab..687d21aec 100644 --- a/src/libecalc/domain/infrastructure/components.py +++ b/src/libecalc/domain/infrastructure/components.py @@ -150,74 +150,6 @@ def validate_fuel_exist(cls, fuel, info: ValidationInfo): return fuel -class ElectricityConsumer(BaseConsumer, EnergyComponent): - component_type: Literal[ - ComponentType.COMPRESSOR, - ComponentType.PUMP, - ComponentType.GENERIC, - ComponentType.PUMP_SYSTEM, - ComponentType.COMPRESSOR_SYSTEM, - ] - consumes: Literal[ConsumptionType.ELECTRICITY] = ConsumptionType.ELECTRICITY - energy_usage_model: dict[ - Period, - ElectricEnergyUsageModel, - ] - - _validate_el_consumer_temporal_model = field_validator("energy_usage_model")(validate_temporal_model) - - _check_model_energy_usage = field_validator("energy_usage_model")( - lambda data: check_model_energy_usage_type(data, EnergyUsageType.POWER) - ) - - def is_fuel_consumer(self) -> bool: - return False - - def is_electricity_consumer(self) -> bool: - return True - - def is_provider(self) -> bool: - return False - - def is_container(self) -> bool: - return False - - def get_component_process_type(self) -> ComponentType: - return self.component_type - - def get_name(self) -> str: - return self.name - - def evaluate_energy_usage( - self, expression_evaluator: ExpressionEvaluator, context: ComponentEnergyContext - ) -> dict[str, EcalcModelResult]: - consumer_results: dict[str, EcalcModelResult] = {} - consumer = ConsumerEnergyComponent( - id=self.id, - name=self.name, - component_type=self.component_type, - regularity=TemporalModel(self.regularity), - consumes=self.consumes, - energy_usage_model=TemporalModel( - { - period: EnergyModelMapper.from_dto_to_domain(model) - for period, model in self.energy_usage_model.items() - } - ), - ) - consumer_results[self.id] = consumer.evaluate(expression_evaluator=expression_evaluator) - - return consumer_results - - @field_validator("energy_usage_model", mode="before") - @classmethod - def check_energy_usage_model(cls, energy_usage_model): - """ - Make sure that temporal models are converted to Period objects if they are strings - """ - if isinstance(energy_usage_model, dict) and len(energy_usage_model.values()) > 0: - energy_usage_model = _convert_keys_in_dictionary_from_str_to_periods(energy_usage_model) - return energy_usage_model Consumer = Annotated[Union[FuelConsumer, ElectricityConsumer], Field(discriminator="consumes")] diff --git a/src/libecalc/domain/infrastructure/energy_components/electricity_consumer/electricity_consumer.py b/src/libecalc/domain/infrastructure/energy_components/electricity_consumer/electricity_consumer.py new file mode 100644 index 000000000..e70d61113 --- /dev/null +++ b/src/libecalc/domain/infrastructure/energy_components/electricity_consumer/electricity_consumer.py @@ -0,0 +1,68 @@ +class ElectricityConsumer(BaseConsumer, EnergyComponent): + component_type: Literal[ + ComponentType.COMPRESSOR, + ComponentType.PUMP, + ComponentType.GENERIC, + ComponentType.PUMP_SYSTEM, + ComponentType.COMPRESSOR_SYSTEM, + ] + consumes: Literal[ConsumptionType.ELECTRICITY] = ConsumptionType.ELECTRICITY + energy_usage_model: dict[ + Period, + ElectricEnergyUsageModel, + ] + + _validate_el_consumer_temporal_model = field_validator("energy_usage_model")(validate_temporal_model) + + _check_model_energy_usage = field_validator("energy_usage_model")( + lambda data: check_model_energy_usage_type(data, EnergyUsageType.POWER) + ) + + def is_fuel_consumer(self) -> bool: + return False + + def is_electricity_consumer(self) -> bool: + return True + + def is_provider(self) -> bool: + return False + + def is_container(self) -> bool: + return False + + def get_component_process_type(self) -> ComponentType: + return self.component_type + + def get_name(self) -> str: + return self.name + + def evaluate_energy_usage( + self, expression_evaluator: ExpressionEvaluator, context: ComponentEnergyContext + ) -> dict[str, EcalcModelResult]: + consumer_results: dict[str, EcalcModelResult] = {} + consumer = ConsumerEnergyComponent( + id=self.id, + name=self.name, + component_type=self.component_type, + regularity=TemporalModel(self.regularity), + consumes=self.consumes, + energy_usage_model=TemporalModel( + { + period: EnergyModelMapper.from_dto_to_domain(model) + for period, model in self.energy_usage_model.items() + } + ), + ) + consumer_results[self.id] = consumer.evaluate(expression_evaluator=expression_evaluator) + + return consumer_results + + @field_validator("energy_usage_model", mode="before") + @classmethod + def check_energy_usage_model(cls, energy_usage_model): + """ + Make sure that temporal models are converted to Period objects if they are strings + """ + if isinstance(energy_usage_model, dict) and len(energy_usage_model.values()) > 0: + energy_usage_model = _convert_keys_in_dictionary_from_str_to_periods(energy_usage_model) + return energy_usage_model