melvin_ob/util/keychain.rs
1use crate::console_communication::ConsoleMessenger;
2use crate::flight_control::{FlightComputer, Supervisor, orbit::ClosedOrbit};
3use crate::http_handler::http_client::HTTPClient;
4use crate::imaging::CameraController;
5use crate::scheduling::TaskController;
6use crate::objective::{BeaconObjective, KnownImgObjective};
7use std::sync::Arc;
8use tokio::sync::{RwLock, mpsc::Receiver};
9
10/// Struct representing the key components of the application, providing access
11/// to various subsystems such as the HTTP client, camera controller, flight computer,
12/// task controller, and console messenger.
13#[derive(Clone)]
14pub struct Keychain {
15 /// The HTTP client for performing network requests.
16 client: Arc<HTTPClient>,
17 /// The supervisor listening for new objectives, refreshing observation, ...
18 supervisor: Arc<Supervisor>,
19 /// The console messenger for handling console-related operations.
20 con: Arc<ConsoleMessenger>,
21 /// The flight computer responsible for managing satellite operations.
22 f_cont: Arc<RwLock<FlightComputer>>,
23 /// The task controller for scheduling and managing tasks.
24 t_cont: Arc<TaskController>,
25 /// The camera controller for handling camera-related operations.
26 c_cont: Arc<CameraController>,
27}
28
29impl Keychain {
30 /// Creates a new instance of [`Keychain`] asynchronously.
31 ///
32 /// # Arguments
33 /// - `url`: The base URL to initialize the HTTP client.
34 ///
35 /// # Returns
36 /// A new instance of [`Keychain`] containing initialized subsystems.
37 pub async fn new(url: &str) -> (Self, Receiver<KnownImgObjective>, Receiver<BeaconObjective>) {
38 let client = Arc::new(HTTPClient::new(url));
39 let c_cont = Arc::new(CameraController::start(
40 "./".to_string(),
41 Arc::clone(&client),
42 ));
43 let t_cont = Arc::new(TaskController::new());
44
45 let f_cont = Arc::new(RwLock::new(FlightComputer::new(Arc::clone(&client)).await));
46 let (supervisor, obj_rx, beac_rx) = {
47 let (sv, rx_obj, rx_beac) = Supervisor::new(Arc::clone(&f_cont));
48 (Arc::new(sv), rx_obj, rx_beac)
49 };
50 let con = Arc::new(ConsoleMessenger::start(
51 Arc::clone(&c_cont),
52 Arc::clone(&t_cont),
53 Arc::clone(&supervisor),
54 ));
55 (
56 Self { client, supervisor, con, f_cont, t_cont, c_cont },
57 obj_rx,
58 beac_rx,
59 )
60 }
61
62 /// Provides a cloned reference to the HTTP client.
63 pub fn client(&self) -> Arc<HTTPClient> { Arc::clone(&self.client) }
64
65 /// Provides a cloned reference to the supervisor
66 pub fn supervisor(&self) -> Arc<Supervisor> { Arc::clone(&self.supervisor) }
67
68 /// Provides a cloned reference to the flight computer.
69 pub fn f_cont(&self) -> Arc<RwLock<FlightComputer>> { Arc::clone(&self.f_cont) }
70
71 /// Provides a cloned reference to the task controller.
72 pub fn t_cont(&self) -> Arc<TaskController> { Arc::clone(&self.t_cont) }
73
74 /// Provides a cloned reference to the console messenger.
75 pub fn con(&self) -> Arc<ConsoleMessenger> { Arc::clone(&self.con) }
76
77 /// Provides a cloned reference to the camera controller.
78 pub fn c_cont(&self) -> Arc<CameraController> { Arc::clone(&self.c_cont) }
79}
80
81/// Struct representing an enhanced [`Keychain`] that includes a [`ClosedOrbit`].
82/// This struct offers access to various subsystems in addition to holding the orbit
83/// and its related operations.
84#[derive(Clone)]
85pub struct KeychainWithOrbit {
86 /// The HTTP client for performing network requests.
87 client: Arc<HTTPClient>,
88 /// The console messenger for handling console-related operations.
89 con: Arc<ConsoleMessenger>,
90 /// The flight computer responsible for managing satellite operations.
91 f_cont: Arc<RwLock<FlightComputer>>,
92 /// The task controller for scheduling and managing tasks.
93 t_cont: Arc<TaskController>,
94 /// The camera controller for handling camera-related operations.
95 c_cont: Arc<CameraController>,
96 /// The closed orbit object, protected by a read-write lock for thread-safe access.
97 c_orbit: Arc<RwLock<ClosedOrbit>>,
98}
99
100impl KeychainWithOrbit {
101 /// Creates a new instance of `KeychainWithOrbit` by combining an existing
102 /// keychain and a closed orbit.
103 ///
104 /// # Arguments
105 /// - `keychain`: The existing `Keychain` instance to extend.
106 /// - `orbit`: The `ClosedOrbit` object to manage in the new instance.
107 ///
108 /// # Returns
109 /// A new instance of `KeychainWithOrbit` containing the provided keychain subsystems
110 /// and the closed orbit.
111 pub fn new(keychain: Keychain, orbit: ClosedOrbit) -> Self {
112 Self {
113 client: keychain.client,
114 con: keychain.con,
115 f_cont: keychain.f_cont,
116 t_cont: keychain.t_cont,
117 c_cont: keychain.c_cont,
118 c_orbit: Arc::new(RwLock::new(orbit)),
119 }
120 }
121
122 /// Provides a cloned reference to the HTTP client.
123 pub fn client(&self) -> Arc<HTTPClient> { Arc::clone(&self.client) }
124
125 /// Provides a cloned reference to the flight computer.
126 pub fn f_cont(&self) -> Arc<RwLock<FlightComputer>> { Arc::clone(&self.f_cont) }
127
128 /// Provides a cloned reference to the task controller.
129 pub fn t_cont(&self) -> Arc<TaskController> { Arc::clone(&self.t_cont) }
130
131 /// Provides a cloned reference to the camera controller.
132 pub fn c_cont(&self) -> Arc<CameraController> { Arc::clone(&self.c_cont) }
133
134 /// Provides a cloned reference to the closed orbit.
135 pub fn c_orbit(&self) -> Arc<RwLock<ClosedOrbit>> { Arc::clone(&self.c_orbit) }
136
137 /// Provides a cloned reference to the console messenger.
138 pub fn con(&self) -> Arc<ConsoleMessenger> { Arc::clone(&self.con) }
139}