-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathflight.py
More file actions
108 lines (88 loc) · 2.85 KB
/
flight.py
File metadata and controls
108 lines (88 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from src.controllers.interface import (
ControllerBase,
controller_exception_handler,
)
from src.views.flight import FlightSimulation
from src.models.flight import FlightModel
from src.models.environment import EnvironmentModel
from src.models.rocket import RocketModel
from src.services.flight import FlightService
class FlightController(ControllerBase):
"""
Controller for the Flight model.
Enables:
- Simulation of a RocketPy Flight.
- CRUD for Flight BaseApiModel.
"""
def __init__(self):
super().__init__(models=[FlightModel])
@controller_exception_handler
async def update_environment_by_flight_id(
self, flight_id: str, *, environment: EnvironmentModel
) -> None:
"""
Update a models.Flight.environment in the database.
Args:
flight_id: str
environment: models.Environment
Returns:
None
Raises:
HTTP 404 Not Found: If the flight is not found in the database.
"""
flight = await self.get_flight_by_id(flight_id)
flight.environment = environment
await self.update_flight_by_id(flight_id, flight)
return
@controller_exception_handler
async def update_rocket_by_flight_id(
self, flight_id: str, *, rocket: RocketModel
) -> None:
"""
Update a models.Flight.rocket in the database.
Args:
flight_id: str
rocket: models.Rocket
Returns:
None
Raises:
HTTP 404 Not Found: If the flight is not found in the database.
"""
flight = await self.get_flight_by_id(flight_id)
flight.rocket = rocket
await self.update_flight_by_id(flight_id, flight)
return
@controller_exception_handler
async def get_rocketpy_flight_binary(
self,
flight_id: str,
) -> bytes:
"""
Get rocketpy.flight as dill binary.
Args:
flight_id: str
Returns:
bytes
Raises:
HTTP 404 Not Found: If the flight is not found in the database.
"""
flight = await self.get_flight_by_id(flight_id)
flight_service = FlightService.from_flight_model(flight.flight)
return flight_service.get_flight_binary()
@controller_exception_handler
async def get_flight_simulation(
self,
flight_id: str,
) -> FlightSimulation:
"""
Simulate a rocket flight.
Args:
flight_id: str
Returns:
Flight simulation view.
Raises:
HTTP 404 Not Found: If the flight does not exist in the database.
"""
flight = await self.get_flight_by_id(flight_id)
flight_service = FlightService.from_flight_model(flight.flight)
return flight_service.get_flight_simulation()