diff --git a/server/src/cedar_server.rs b/server/src/cedar_server.rs index 69763cc..49dea04 100644 --- a/server/src/cedar_server.rs +++ b/server/src/cedar_server.rs @@ -172,6 +172,7 @@ macro_rules! logged_status { }}; } +#[derive(Clone)] struct MyCedar { // We organize our state as a sub-object so update_operation_settings() can // spawn a sub-task for the SETUP -> OPERATE mode transition; the sub-task @@ -1607,6 +1608,32 @@ impl Cedar for MyCedar { } // impl Cedar for MyCedar. impl MyCedar { + async fn move_to_operating_mode(&self) { + let (mode, utc_date) = { + let state = self.state.lock().await; + let date = state.telescope_position.lock().await.utc_date; + (state.operation_settings.operating_mode, date) + }; + if mode.unwrap() != OperatingMode::Operate as i32 && utc_date.is_some() + { + info!("Moving to operate mode"); + let fixed_settings = FixedSettings { + current_time: Some(utc_date.unwrap().into()), + ..Default::default() + }; + let _ = self + .update_fixed_settings(tonic::Request::new(fixed_settings)) + .await; + let settings = OperationSettings { + operating_mode: Some(OperatingMode::Operate as i32), + ..Default::default() + }; + let _ = self + .update_operation_settings(tonic::Request::new(settings)) + .await; + } + } + fn get_demo_images() -> Result, tonic::Status> { let dir = Path::new("./demo_images"); if !dir.exists() { @@ -4079,6 +4106,15 @@ async fn async_main( let use_lx200_bt = cedar.state.lock().await.preferences.lock().await.use_bluetooth; + let cedar_clone = cedar.clone(); + let operate_callback = Box::new(move || { + tokio::task::block_in_place(|| { + tokio::runtime::Handle::current().block_on(async { + cedar_clone.move_to_operating_mode().await; + }); + }); + }); + let cedar_server = CedarServer::new(cedar); let grpc = tonic::transport::Server::builder() @@ -4122,6 +4158,7 @@ async fn async_main( let mut lx200_server_bt = create_lx200_server( shared_telescope_position.clone(), async_callback.clone(), + operate_callback.clone(), true, ); Some(tokio::task::spawn(async move { @@ -4136,6 +4173,7 @@ async fn async_main( let mut lx200_server = create_lx200_server( shared_telescope_position.clone(), async_callback.clone(), + operate_callback, false, ); let _task_handle: tokio::task::JoinHandle> = diff --git a/server/src/lx200_server.rs b/server/src/lx200_server.rs index 0c468ad..b7dbaa4 100644 --- a/server/src/lx200_server.rs +++ b/server/src/lx200_server.rs @@ -56,10 +56,15 @@ impl Lx200Telescope for Lx200WifiTelescope { impl Lx200WifiTelescope { pub fn new( telescope_position: Arc>, - cb: Box, + get_ra_cb: Box, + set_date_cb: Box, ) -> Self { Lx200WifiTelescope { - controller: Lx200Controller::new(telescope_position, cb), + controller: Lx200Controller::new( + telescope_position, + get_ra_cb, + set_date_cb, + ), } } @@ -145,10 +150,15 @@ impl Lx200Telescope for Lx200BtTelescope { impl Lx200BtTelescope { pub fn new( telescope_position: Arc>, - cb: Box, + get_ra_cb: Box, + set_date_cb: Box, ) -> Self { Lx200BtTelescope { - controller: Lx200Controller::new(telescope_position, cb), + controller: Lx200Controller::new( + telescope_position, + get_ra_cb, + set_date_cb, + ), } } @@ -198,7 +208,9 @@ struct Lx200Controller { wiggle_phase: bool, // Called whenever client obtains our right ascension - callback: Option, + get_ra_callback: Option, + // Called whenever client updates time/date + set_date_callback: Option, // Pending target coordinates (in jnow_epoch) for sync/slew target_ra: Option, @@ -228,7 +240,8 @@ struct Lx200Controller { impl Lx200Controller { pub fn new( telescope_position: Arc>, - cb: Box, + get_ra_cb: Box, + set_date_cb: Box, ) -> Self { let dt = Local::now(); let jnow = ((dt.year() as f64 + dt.ordinal0() as f64 / 365.0) * 10.0) @@ -237,7 +250,8 @@ impl Lx200Controller { info!("Using now epoch: {}", jnow); Lx200Controller { telescope_position, - callback: Some(Callback(cb)), + get_ra_callback: Some(Callback(get_ra_cb)), + set_date_callback: Some(Callback(set_date_cb)), datetime: dt.with_timezone(dt.offset()), // Ideally these should be the current location in Cedar's settings latitude: "+00:00".to_string(), @@ -291,7 +305,7 @@ impl Lx200Controller { } async fn get_ra(&self) -> String { - if let Some(ref cb) = self.callback { + if let Some(ref cb) = self.get_ra_callback { cb.0(); } let mut locked_position = self.telescope_position.lock().await; @@ -514,9 +528,11 @@ impl Lx200Controller { * 10.0) .round() / 10.0; - let mut locked_position = - self.telescope_position.lock().await; - locked_position.utc_date = Some(SystemTime::from(dt)); + self.telescope_position.lock().await.utc_date = + Some(SystemTime::from(dt)); + if let Some(ref cb) = self.set_date_callback { + cb.0(); + } return "1Updating Planetary Data# #".to_string(); } Err(e) => { @@ -754,13 +770,22 @@ impl Lx200Controller { pub fn create_lx200_server( telescope_position: Arc>, - cb: Box, + get_ra_cb: Box, + set_date_cb: Box, use_bluetooth: bool, ) -> Box { if use_bluetooth { - Box::new(Lx200BtTelescope::new(telescope_position, cb)) + Box::new(Lx200BtTelescope::new( + telescope_position, + get_ra_cb, + set_date_cb, + )) } else { - Box::new(Lx200WifiTelescope::new(telescope_position, cb)) + Box::new(Lx200WifiTelescope::new( + telescope_position, + get_ra_cb, + set_date_cb, + )) } }