Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions server/src/cedar_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Vec<String>, tonic::Status> {
let dir = Path::new("./demo_images");
if !dir.exists() {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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<Result<(), tonic::Status>> =
Expand Down
53 changes: 39 additions & 14 deletions server/src/lx200_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ impl Lx200Telescope for Lx200WifiTelescope {
impl Lx200WifiTelescope {
pub fn new(
telescope_position: Arc<tokio::sync::Mutex<TelescopePosition>>,
cb: Box<dyn Fn() + Send + Sync>,
get_ra_cb: Box<dyn Fn() + Send + Sync>,
set_date_cb: Box<dyn Fn() + Send + Sync>,
) -> Self {
Lx200WifiTelescope {
controller: Lx200Controller::new(telescope_position, cb),
controller: Lx200Controller::new(
telescope_position,
get_ra_cb,
set_date_cb,
),
}
}

Expand Down Expand Up @@ -145,10 +150,15 @@ impl Lx200Telescope for Lx200BtTelescope {
impl Lx200BtTelescope {
pub fn new(
telescope_position: Arc<tokio::sync::Mutex<TelescopePosition>>,
cb: Box<dyn Fn() + Send + Sync>,
get_ra_cb: Box<dyn Fn() + Send + Sync>,
set_date_cb: Box<dyn Fn() + Send + Sync>,
) -> Self {
Lx200BtTelescope {
controller: Lx200Controller::new(telescope_position, cb),
controller: Lx200Controller::new(
telescope_position,
get_ra_cb,
set_date_cb,
),
}
}

Expand Down Expand Up @@ -198,7 +208,9 @@ struct Lx200Controller {
wiggle_phase: bool,

// Called whenever client obtains our right ascension
callback: Option<Callback>,
get_ra_callback: Option<Callback>,
// Called whenever client updates time/date
set_date_callback: Option<Callback>,

// Pending target coordinates (in jnow_epoch) for sync/slew
target_ra: Option<f64>,
Expand Down Expand Up @@ -228,7 +240,8 @@ struct Lx200Controller {
impl Lx200Controller {
pub fn new(
telescope_position: Arc<tokio::sync::Mutex<TelescopePosition>>,
cb: Box<dyn Fn() + Send + Sync>,
get_ra_cb: Box<dyn Fn() + Send + Sync>,
set_date_cb: Box<dyn Fn() + Send + Sync>,
) -> Self {
let dt = Local::now();
let jnow = ((dt.year() as f64 + dt.ordinal0() as f64 / 365.0) * 10.0)
Expand All @@ -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(),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -754,13 +770,22 @@ impl Lx200Controller {

pub fn create_lx200_server(
telescope_position: Arc<tokio::sync::Mutex<TelescopePosition>>,
cb: Box<dyn Fn() + Send + Sync>,
get_ra_cb: Box<dyn Fn() + Send + Sync>,
set_date_cb: Box<dyn Fn() + Send + Sync>,
use_bluetooth: bool,
) -> Box<dyn Lx200Telescope + Send> {
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,
))
}
}

Expand Down