use std::time::Duration;
use serde::{Deserialize, Serialize};
use crate::coordinates::Coordinates;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SolarSystemBody {
Sun,
Moon,
Mercury,
Venus,
Mars,
Jupiter,
Saturn,
Uranus,
Neptune,
}
impl SolarSystemBody {
pub fn is_planet(self) -> bool {
!matches!(self, Self::Sun | Self::Moon)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Target {
Sky {
name: Option<String>,
coordinates: Coordinates,
},
SolarSystem(SolarSystemBody),
Scenery,
}
impl Target {
pub fn named(name: impl Into<String>, coordinates: Coordinates) -> Self {
Self::Sky {
name: Some(name.into()),
coordinates,
}
}
pub fn at(coordinates: Coordinates) -> Self {
Self::Sky {
name: None,
coordinates,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Prerequisite {
SiteLocation,
Clock,
Calibration,
Focus,
DarkFrames,
Other(String),
}
impl std::fmt::Display for Prerequisite {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::SiteLocation => f.write_str("site location"),
Self::Clock => f.write_str("clock"),
Self::Calibration => f.write_str("calibration"),
Self::Focus => f.write_str("focus"),
Self::DarkFrames => f.write_str("dark frames"),
Self::Other(what) => f.write_str(what),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Readiness {
Ready,
Needs(Vec<Prerequisite>),
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum AcquisitionState {
Idle,
Slewing,
Centring,
OnTarget,
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct StackSettings {
pub exposure: Option<Duration>,
pub gain: Option<i32>,
pub frame_limit: Option<u32>,
pub filter: Option<String>,
pub restart: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub enum StackState {
#[default]
Idle,
Stacking,
Finished,
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct StackProgress {
pub state: StackState,
pub stacked: u32,
pub dropped: u32,
pub frame_limit: Option<u32>,
pub integration: Option<Duration>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum AutoFocusState {
Idle,
Running,
Done {
position: Option<i32>,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct PlateSolution {
pub center: Coordinates,
pub rotation_deg: Option<f64>,
pub pixel_scale_arcsec: Option<f64>,
pub field_of_view_deg: Option<(f64, f64)>,
}
impl PlateSolution {
pub fn new(center: Coordinates) -> Self {
Self {
center,
rotation_deg: None,
pixel_scale_arcsec: None,
field_of_view_deg: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn m31() -> Coordinates {
Coordinates::from_hours(0.712, 41.27).unwrap()
}
fn round_trip<T>(value: &T) -> T
where
T: Serialize + for<'de> Deserialize<'de>,
{
serde_json::from_str(&serde_json::to_string(value).unwrap()).unwrap()
}
#[test]
fn targets_round_trip() {
for target in [
Target::named("M31", m31()),
Target::at(m31()),
Target::SolarSystem(SolarSystemBody::Jupiter),
Target::Scenery,
] {
assert_eq!(round_trip(&target), target);
}
}
#[test]
fn the_sun_and_moon_are_not_planets() {
assert!(!SolarSystemBody::Sun.is_planet());
assert!(!SolarSystemBody::Moon.is_planet());
assert!(SolarSystemBody::Mercury.is_planet());
assert!(SolarSystemBody::Neptune.is_planet());
}
#[test]
fn readiness_and_states_round_trip() {
let needs = Readiness::Needs(vec![
Prerequisite::Clock,
Prerequisite::Other("level the tripod".into()),
]);
assert_eq!(round_trip(&needs), needs);
assert_eq!(round_trip(&Readiness::Ready), Readiness::Ready);
for state in [
AcquisitionState::Idle,
AcquisitionState::Slewing,
AcquisitionState::Centring,
AcquisitionState::OnTarget,
] {
assert_eq!(round_trip(&state), state);
}
let done = AutoFocusState::Done {
position: Some(1580),
};
assert_eq!(round_trip(&done), done);
}
#[test]
fn prerequisites_read_as_plain_words() {
assert_eq!(Prerequisite::SiteLocation.to_string(), "site location");
assert_eq!(Prerequisite::Clock.to_string(), "clock");
assert_eq!(Prerequisite::Other("level".into()).to_string(), "level");
}
#[test]
fn default_stack_settings_leave_everything_to_the_telescope() {
let settings = StackSettings::default();
assert_eq!(settings.exposure, None);
assert_eq!(settings.gain, None);
assert_eq!(settings.frame_limit, None);
assert_eq!(settings.filter, None);
assert!(!settings.restart);
let chosen = StackSettings {
exposure: Some(Duration::from_secs(10)),
frame_limit: Some(60),
..StackSettings::default()
};
assert_eq!(round_trip(&chosen), chosen);
}
#[test]
fn default_progress_is_an_idle_empty_stack() {
let progress = StackProgress::default();
assert_eq!(progress.state, StackState::Idle);
assert_eq!((progress.stacked, progress.dropped), (0, 0));
assert_eq!(round_trip(&progress), progress);
}
#[test]
fn a_solution_starts_with_only_its_centre() {
let solution = PlateSolution::new(m31());
assert_eq!(solution.rotation_deg, None);
assert_eq!(solution.field_of_view_deg, None);
assert_eq!(round_trip(&solution), solution);
}
}