Skip to content

Commit

Permalink
implements generic motor; removes rocket_options; fixes binary output
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBarberini committed Sep 11, 2024
1 parent 95119f1 commit 6015453
Show file tree
Hide file tree
Showing 31 changed files with 220 additions and 719 deletions.
8 changes: 4 additions & 4 deletions lib/controllers/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async def get_rocketpy_env_binary(
"""
try:
env = await cls.get_env_by_id(env_id)
rocketpy_env = EnvironmentService.from_env_model(env)
env_service = EnvironmentService.from_env_model(env)
except HTTPException as e:
raise e from e
except Exception as e:
Expand All @@ -151,7 +151,7 @@ async def get_rocketpy_env_binary(
detail=f"Failed to read environment: {exc_str}",
) from e
else:
return rocketpy_env.get_env_binary()
return env_service.get_env_binary()
finally:
logger.info(
f"Call to controllers.environment.get_rocketpy_env_binary completed for Env {env_id}"
Expand Down Expand Up @@ -260,8 +260,8 @@ async def simulate_env(
"""
try:
env = await cls.get_env_by_id(env_id)
rocketpy_env = EnvironmentService.from_env_model(env)
env_summary = rocketpy_env.get_env_summary()
env_service = EnvironmentService.from_env_model(env)
env_summary = env_service.get_env_summary()
except HTTPException as e:
raise e from e
except Exception as e:
Expand Down
15 changes: 10 additions & 5 deletions lib/controllers/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


from lib import logger, parse_error
from lib.controllers.rocket import RocketController
from lib.models.environment import Env
from lib.models.rocket import Rocket
from lib.models.flight import Flight
Expand Down Expand Up @@ -42,6 +43,7 @@ def __init__(
self,
flight: Flight,
):
self.guard(flight)
self._flight = flight

@property
Expand All @@ -52,6 +54,10 @@ def flight(self) -> Flight:
def flight(self, flight: Flight):
self._flight = flight

@staticmethod
def guard(flight: Flight):
RocketController.guard(flight.rocket)

async def create_flight(self) -> Union[FlightCreated, HTTPException]:
"""
Create a flight in the database.
Expand Down Expand Up @@ -131,7 +137,6 @@ async def get_flight_by_id(
updated_rocket.update(motor=motor_view)
rocket_view = RocketView(
**updated_rocket,
selected_rocket_option=flight.rocket.rocket_option,
)
updated_flight = flight.dict()
updated_flight.update(rocket=rocket_view)
Expand Down Expand Up @@ -165,7 +170,7 @@ async def get_rocketpy_flight_binary(
"""
try:
flight = await cls.get_flight_by_id(flight_id)
rocketpy_flight = FlightService.from_flight_model(flight)
flight_service = FlightService.from_flight_model(flight)
except HTTPException as e:
raise e from e
except Exception as e:
Expand All @@ -178,7 +183,7 @@ async def get_rocketpy_flight_binary(
detail=f"Failed to read flight: {exc_str}",
) from e
else:
return rocketpy_flight.get_flight_binary()
return flight_service.get_flight_binary()
finally:
logger.info(
f"Call to controllers.flight.get_rocketpy_flight_binary completed for Flight {flight_id}"
Expand Down Expand Up @@ -376,8 +381,8 @@ async def simulate_flight(
"""
try:
flight = await cls.get_flight_by_id(flight_id=flight_id)
rocketpy_flight = FlightService.from_flight_model(flight)
flight_summary = rocketpy_flight.get_flight_summary()
flight_service = FlightService.from_flight_model(flight)
flight_summary = flight_service.get_flight_summary()
except HTTPException as e:
raise e from e
except Exception as e:
Expand Down
18 changes: 12 additions & 6 deletions lib/controllers/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ def motor(self) -> Motor:
def motor(self, motor: Motor):
self._motor = motor

def guard(self, motor: Motor):
if motor.motor_kind != MotorKinds.SOLID and motor.tanks is None:
@staticmethod
def guard(motor: Motor):
if (
motor.motor_kind not in (MotorKinds.SOLID, MotorKinds.GENERIC)
and motor.tanks is None
):
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail="Tanks must be provided for liquid and hybrid motors.",
)

# TODO: extend guard to check motor kinds and tank kinds specifics

async def create_motor(self) -> Union[MotorCreated, HTTPException]:
"""
Create a models.Motor in the database.
Expand Down Expand Up @@ -148,7 +154,7 @@ async def get_rocketpy_motor_binary(
"""
try:
motor = await cls.get_motor_by_id(motor_id)
rocketpy_motor = MotorService.from_motor_model(motor)
motor_service = MotorService.from_motor_model(motor)
except HTTPException as e:
raise e from e
except Exception as e:
Expand All @@ -161,7 +167,7 @@ async def get_rocketpy_motor_binary(
detail=f"Failed to read motor: {exc_str}",
) from e
else:
return rocketpy_motor.get_motor_binary()
return motor_service.get_motor_binary()
finally:
logger.info(
f"Call to controllers.motor.get_rocketpy_motor_binary completed for Motor {motor_id}"
Expand Down Expand Up @@ -266,8 +272,8 @@ async def simulate_motor(
"""
try:
motor = await cls.get_motor_by_id(motor_id)
rocketpy_motor = MotorService.from_motor_model(motor)
motor_summary = rocketpy_motor.get_motor_summary()
motor_service = MotorService.from_motor_model(motor)
motor_summary = motor_service.get_motor_summary()
except HTTPException as e:
raise e from e
except Exception as e:
Expand Down
15 changes: 10 additions & 5 deletions lib/controllers/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from lib import logger, parse_error
from lib.services.rocket import RocketService
from lib.models.rocket import Rocket
from lib.controllers.motor import MotorController
from lib.repositories.rocket import RocketRepository
from lib.views.motor import MotorView
from lib.views.rocket import (
Expand All @@ -32,6 +33,7 @@ def __init__(
self,
rocket: Rocket,
):
self.guard(rocket)
self._rocket = rocket

@property
Expand All @@ -42,6 +44,10 @@ def rocket(self) -> Rocket:
def rocket(self, rocket: Rocket):
self._rocket = rocket

@staticmethod
def guard(rocket: Rocket):
MotorController.guard(rocket.motor)

async def create_rocket(self) -> Union[RocketCreated, HTTPException]:
"""
Create a models.Rocket in the database.
Expand Down Expand Up @@ -121,7 +127,6 @@ async def get_rocket_by_id(
updated_rocket.update(motor=motor_view)
rocket_view = RocketView(
**updated_rocket,
selected_rocket_option=rocket.rocket_option,
)
return rocket_view
raise HTTPException(
Expand Down Expand Up @@ -151,7 +156,7 @@ async def get_rocketpy_rocket_binary(
"""
try:
rocket = await cls.get_rocket_by_id(rocket_id)
rocketpy_rocket = RocketService.from_rocket_model(rocket)
rocket_service = RocketService.from_rocket_model(rocket)
except HTTPException as e:
raise e from e
except Exception as e:
Expand All @@ -164,7 +169,7 @@ async def get_rocketpy_rocket_binary(
detail=f"Failed to read rocket: {exc_str}",
) from e
else:
return rocketpy_rocket.get_rocket_binary()
return rocket_service.get_rocket_binary()
finally:
logger.info(
f"Call to controllers.rocket.get_rocketpy_rocket_binary completed for Rocket {rocket_id}"
Expand Down Expand Up @@ -270,8 +275,8 @@ async def simulate_rocket(
"""
try:
rocket = await cls.get_rocket_by_id(rocket_id)
rocketpy_rocket = RocketService.from_rocket_model(rocket)
rocket_summary = rocketpy_rocket.get_rocket_summary()
rocket_service = RocketService.from_rocket_model(rocket)
rocket_summary = rocket_service.get_rocket_summary()
except HTTPException as e:
raise e from e
except Exception as e:
Expand Down
Loading

0 comments on commit 6015453

Please sign in to comment.