From 4319b42b5c7db23a691b15f69e30b86e92429804 Mon Sep 17 00:00:00 2001 From: oakamil Date: Mon, 24 Nov 2025 20:42:41 -0800 Subject: [PATCH] Set server time and move to operate mode automatically --- server/src/cedar_server.rs | 47 ++++++++++++++++++++++++++++++--- server/src/lx200_server.rs | 53 ++++++++++++++++++++++++++++---------- 2 files changed, 83 insertions(+), 17 deletions(-) diff --git a/server/src/cedar_server.rs b/server/src/cedar_server.rs index 1f33c5a..d6668b6 100644 --- a/server/src/cedar_server.rs +++ b/server/src/cedar_server.rs @@ -168,6 +168,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 @@ -1531,6 +1532,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() { @@ -2917,11 +2944,14 @@ impl MyCedar { cal_data.gyro_zero_bias_z = Some(zb.z); } if let Some(tc) = transform_calibration { - cal_data.gyro_transform_error_fraction = Some(tc.transform_error_fraction); + cal_data.gyro_transform_error_fraction = + Some(tc.transform_error_fraction); cal_data.camera_view_gyro_axis = Some(tc.camera_view_gyro_axis); - cal_data.camera_view_misalignment = Some(tc.camera_view_misalignment); + cal_data.camera_view_misalignment = + Some(tc.camera_view_misalignment); cal_data.camera_up_gyro_axis = Some(tc.camera_up_gyro_axis); - cal_data.camera_up_misalignment = Some(tc.camera_up_misalignment); + cal_data.camera_up_misalignment = + Some(tc.camera_up_misalignment); } } @@ -3998,6 +4028,15 @@ async fn async_main( preferences.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() @@ -4041,6 +4080,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 { @@ -4055,6 +4095,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 beca9c2..3cdd3ae 100644 --- a/server/src/lx200_server.rs +++ b/server/src/lx200_server.rs @@ -50,10 +50,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, + ), } } @@ -157,10 +162,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, + ), } } @@ -218,7 +228,9 @@ struct Lx200Controller { animate_while_invalid: 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 for sync/slew target_ra: Option, @@ -242,7 +254,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) @@ -251,7 +264,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(), @@ -301,7 +315,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; @@ -524,9 +538,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) => { @@ -762,13 +778,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, + )) } }