Skip to content

Commit 52e09ca

Browse files
committed
DEV: fixing missing attributes issue
1 parent 26f510d commit 52e09ca

File tree

5 files changed

+119
-14
lines changed

5 files changed

+119
-14
lines changed

src/services/environment.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from rocketpy.environment.environment import Environment as RocketPyEnvironment
66
from src.models.environment import EnvironmentModel
77
from src.views.environment import EnvironmentSimulation
8-
from src.utils import rocketpy_encoder
8+
from src.utils import collect_simulation_attributes
99

1010

1111
class EnvironmentService:
@@ -54,8 +54,11 @@ def get_environment_simulation(self) -> EnvironmentSimulation:
5454
EnvironmentSimulation
5555
"""
5656

57-
attributes = rocketpy_encoder(self.environment)
58-
env_simulation = EnvironmentSimulation(**attributes)
57+
encoded_attributes = collect_simulation_attributes(
58+
self.environment,
59+
EnvironmentSimulation,
60+
)
61+
env_simulation = EnvironmentSimulation(**encoded_attributes)
5962
return env_simulation
6063

6164
def get_environment_binary(self) -> bytes:

src/services/flight.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
from src.services.rocket import RocketService
99
from src.models.flight import FlightModel
1010
from src.views.flight import FlightSimulation
11-
from src.utils import rocketpy_encoder
12-
11+
from src.views.rocket import RocketSimulation
12+
from src.views.motor import MotorSimulation
13+
from src.views.environment import EnvironmentSimulation
14+
from src.utils import collect_simulation_attributes
1315

1416
class FlightService:
1517
_flight: RocketPyFlight
@@ -55,8 +57,14 @@ def get_flight_simulation(self) -> FlightSimulation:
5557
Returns:
5658
FlightSimulation
5759
"""
58-
attributes = rocketpy_encoder(self.flight)
59-
flight_simulation = FlightSimulation(**attributes)
60+
encoded_attributes = collect_simulation_attributes(
61+
self.flight,
62+
FlightSimulation,
63+
RocketSimulation,
64+
MotorSimulation,
65+
EnvironmentSimulation
66+
)
67+
flight_simulation = FlightSimulation(**encoded_attributes)
6068
return flight_simulation
6169

6270
def get_flight_binary(self) -> bytes:

src/services/motor.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from src.models.sub.tanks import TankKinds
1818
from src.models.motor import MotorKinds, MotorModel
1919
from src.views.motor import MotorSimulation
20-
from src.utils import rocketpy_encoder
20+
from src.utils import collect_simulation_attributes
2121

2222

2323
class MotorService:
@@ -140,8 +140,11 @@ def get_motor_simulation(self) -> MotorSimulation:
140140
Returns:
141141
MotorSimulation
142142
"""
143-
attributes = rocketpy_encoder(self.motor)
144-
motor_simulation = MotorSimulation(**attributes)
143+
encoded_attributes = collect_simulation_attributes(
144+
self.motor,
145+
MotorSimulation,
146+
)
147+
motor_simulation = MotorSimulation(**encoded_attributes)
145148
return motor_simulation
146149

147150
def get_motor_binary(self) -> bytes:

src/services/rocket.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
from src.models.sub.aerosurfaces import NoseCone, Tail, Fins
1818
from src.services.motor import MotorService
1919
from src.views.rocket import RocketSimulation
20-
from src.utils import rocketpy_encoder
21-
20+
from src.views.motor import MotorSimulation
21+
from src.utils import collect_simulation_attributes
2222

2323
class RocketService:
2424
_rocket: RocketPyRocket
@@ -107,8 +107,12 @@ def get_rocket_simulation(self) -> RocketSimulation:
107107
Returns:
108108
RocketSimulation
109109
"""
110-
attributes = rocketpy_encoder(self.rocket)
111-
rocket_simulation = RocketSimulation(**attributes)
110+
encoded_attributes = collect_simulation_attributes(
111+
self.rocket,
112+
RocketSimulation,
113+
MotorSimulation,
114+
)
115+
rocket_simulation = RocketSimulation(**encoded_attributes)
112116
return rocket_simulation
113117

114118
def get_rocket_binary(self) -> bytes:

src/utils.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,93 @@ def rocketpy_encoder(obj):
3737
return json.loads(json_str)
3838

3939

40+
def collect_simulation_attributes(flight_obj, flight_simulation_class, rocket_simulation_class, motor_simulation_class, environment_simulation_class):
41+
"""
42+
Collect attributes from various simulation classes and populate them from the flight object.
43+
44+
Args:
45+
flight_obj: RocketPy Flight object
46+
flight_simulation_class: FlightSimulation class
47+
rocket_simulation_class: RocketSimulation class
48+
motor_simulation_class: MotorSimulation class
49+
environment_simulation_class: EnvironmentSimulation class
50+
51+
Returns:
52+
Dictionary with all collected attributes
53+
"""
54+
attributes = rocketpy_encoder(flight_obj)
55+
56+
flight_attributes_list = [
57+
attr for attr in flight_simulation_class.__annotations__.keys()
58+
if attr not in ['message', 'rocket', 'env']
59+
]
60+
61+
rocket_attributes_list = [
62+
attr for attr in rocket_simulation_class.__annotations__.keys()
63+
if attr not in ['message', 'motor']
64+
]
65+
66+
motor_attributes_list = [
67+
attr for attr in motor_simulation_class.__annotations__.keys()
68+
if attr not in ['message']
69+
]
70+
71+
environment_attributes_list = [
72+
attr for attr in environment_simulation_class.__annotations__.keys()
73+
if attr not in ['message']
74+
]
75+
76+
try:
77+
for key in flight_attributes_list:
78+
try:
79+
value = getattr(flight_obj, key)
80+
attributes[key] = value
81+
except AttributeError as e:
82+
logger.warning(f"Flight attribute '{key}' not found: {e}")
83+
except Exception as e:
84+
logger.error(f"Error getting flight attribute '{key}': {type(e).__name__}: {e}")
85+
except Exception as e:
86+
logger.error(f"Error processing flight attributes: {type(e).__name__}: {e}")
87+
88+
try:
89+
for key in rocket_attributes_list:
90+
try:
91+
value = getattr(flight_obj.rocket, key)
92+
attributes["rocket"][key] = value
93+
except AttributeError as e:
94+
logger.warning(f"Rocket attribute '{key}' not found: {e}")
95+
except Exception as e:
96+
logger.error(f"Error getting rocket attribute '{key}': {type(e).__name__}: {e}")
97+
except Exception as e:
98+
logger.error(f"Error processing rocket attributes: {type(e).__name__}: {e}")
99+
100+
try:
101+
for key in motor_attributes_list:
102+
try:
103+
value = getattr(flight_obj.rocket.motor, key)
104+
attributes["rocket"]["motor"][key] = value
105+
except AttributeError as e:
106+
logger.warning(f"Motor attribute '{key}' not found: {e}")
107+
except Exception as e:
108+
logger.error(f"Error getting motor attribute '{key}': {type(e).__name__}: {e}")
109+
except Exception as e:
110+
logger.error(f"Error processing motor attributes: {type(e).__name__}: {e}")
111+
112+
try:
113+
for key in environment_attributes_list:
114+
try:
115+
value = getattr(flight_obj.env, key)
116+
attributes["env"][key] = value
117+
except AttributeError as e:
118+
logger.warning(f"Environment attribute '{key}' not found: {e}")
119+
except Exception as e:
120+
logger.error(f"Error getting environment attribute '{key}': {type(e).__name__}: {e}")
121+
except Exception as e:
122+
logger.error(f"Error processing environment attributes: {type(e).__name__}: {e}")
123+
124+
return rocketpy_encoder(attributes)
125+
126+
40127
class RocketPyGZipMiddleware:
41128
def __init__(
42129
self, app: ASGIApp, minimum_size: int = 500, compresslevel: int = 9

0 commit comments

Comments
 (0)