1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use std::path::PathBuf;
use std::time::Duration;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use scopinator_seestar::InteropKey;
mod commands;
mod target;
use target::{BackendKind, HostArg, Target};
#[derive(Parser)]
#[command(name = "scopinator", about = "Telescope control CLI")]
struct Cli {
/// Logging verbosity (-v for debug, -vv for trace)
#[arg(short, long, action = clap::ArgAction::Count)]
verbose: u8,
/// Path to RSA interoperability PEM key for the verify_client handshake (firmware 6.70+)
#[arg(long, env = "SEESTAR_INTEROP_PEM", value_name = "PATH")]
interop_pem: Option<PathBuf>,
/// The kind of telescope at --host. A URL in --host says the same:
/// `alpaca://192.168.1.50:11111`, `seestar://192.168.1.100`. Without
/// either, a Seestar.
#[arg(long, global = true, env = "SCOPINATOR_BACKEND", value_enum)]
backend: Option<BackendKind>,
/// The Alpaca server's port, when --host has none (default 11111).
#[arg(long, global = true, value_name = "PORT")]
port: Option<u16>,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Discover telescopes on the local network (Seestars and Alpaca servers)
Discover {
/// Discovery timeout in seconds
#[arg(short, long, default_value = "3")]
timeout: u64,
},
/// Show telescope status
Status {
/// Telescope address: an IPv4 address, or a URL naming the backend
/// (alpaca://HOST:PORT, seestar://HOST)
#[arg(short = 'H', long)]
host: HostArg,
},
/// Slew to coordinates
///
/// On an Alpaca mount the coordinates are J2000 unless --jnow is given,
/// tracking is turned on if it is off, and the command waits for the
/// mount to arrive. A Seestar is sent the coordinates as given and
/// reports when it starts moving.
Goto {
/// Telescope address: an IPv4 address, or a URL naming the backend
/// (alpaca://HOST:PORT, seestar://HOST)
#[arg(short = 'H', long)]
host: HostArg,
/// Right ascension in hours (0-24)
#[arg(long)]
ra: f64,
/// Declination in degrees (-90 to 90)
#[arg(long, allow_negative_numbers = true)]
dec: f64,
/// Target name
#[arg(short, long, default_value = "Target")]
name: String,
/// Set the telescope's clock from this computer even if it looks
/// right. By default the clock is set only when it is clearly wrong
/// (some models lose it when powered off), because the telescope
/// cannot find a target without it.
#[arg(long, conflicts_with = "no_sync_clock")]
sync_clock: bool,
/// Never set the telescope's clock; only warn when it looks wrong.
#[arg(long)]
no_sync_clock: bool,
/// The coordinates are in the equinox of date, not J2000 (Alpaca).
#[arg(long)]
jnow: bool,
/// How long to wait for the mount to arrive, in seconds (Alpaca).
#[arg(long, default_value = "300")]
timeout: u64,
},
/// Park the telescope
Park {
/// Telescope address: an IPv4 address, or a URL naming the backend
/// (alpaca://HOST:PORT, seestar://HOST)
#[arg(short = 'H', long)]
host: HostArg,
/// How long to wait for the mount to park, in seconds (Alpaca).
#[arg(long, default_value = "300")]
timeout: u64,
},
/// Take the mount out of park (a Seestar opens by itself on a goto)
Unpark {
/// Telescope address: an IPv4 address, or a URL naming the backend
/// (alpaca://HOST:PORT, seestar://HOST)
#[arg(short = 'H', long)]
host: HostArg,
},
/// Send any Seestar control method by name and print the reply as JSON.
///
/// For methods this tool has no command for. Nothing is checked: what
/// the method does to the telescope is up to you.
Raw {
/// Telescope address: an IPv4 address, or a URL naming the backend
/// (alpaca://HOST:PORT, seestar://HOST)
#[arg(short = 'H', long)]
host: HostArg,
/// Method name as the firmware spells it, e.g. iscope_get_app_state
method: String,
/// Params as JSON, e.g. '{"keys":["mount"]}'. Omit to send none.
params: Option<String>,
/// For a method with no params that reads a positional element
/// (get_disk_volume, get_annotate_result): when the connection needs
/// the verify marker, send it as an object.
#[arg(long)]
object_marker: bool,
},
}
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::parse();
let log_level = match cli.verbose {
0 => "info",
1 => "debug",
_ => "trace",
};
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new(log_level)),
)
.init();
// Load interop key once; fail fast if the file exists but is unparseable.
let interop_key: Option<InteropKey> = if let Some(path) = &cli.interop_pem {
let pem = std::fs::read_to_string(path)
.with_context(|| format!("failed to read interop PEM from {}", path.display()))?;
let key = InteropKey::from_pem(&pem)
.with_context(|| format!("failed to parse interop PEM from {}", path.display()))?;
Some(key)
} else {
None
};
let target = |host: &HostArg| Target::resolve(host, cli.backend, cli.port);
match cli.command {
Commands::Discover { timeout } => {
commands::discover(Duration::from_secs(timeout)).await?;
}
Commands::Status { host } => match target(&host)? {
Target::Seestar { host } => commands::status(host, interop_key).await?,
Target::Alpaca { host, port } => {
let backend = commands::neutral::connect_alpaca(host, port).await?;
commands::neutral::status(backend).await?;
}
},
Commands::Goto {
host,
ra,
dec,
name,
sync_clock,
no_sync_clock,
jnow,
timeout,
} => match target(&host)? {
Target::Seestar { host } => {
let clock = match (sync_clock, no_sync_clock) {
(true, _) => commands::ClockPolicy::Always,
(_, true) => commands::ClockPolicy::Never,
_ => commands::ClockPolicy::IfWrong,
};
commands::goto(host, ra, dec, &name, interop_key, clock).await?;
}
Target::Alpaca { host, port } => {
if sync_clock || no_sync_clock {
anyhow::bail!("--sync-clock and --no-sync-clock are for a Seestar");
}
let mut coords = scopinator_types::Coordinates::from_hours(ra, dec)
.context("invalid coordinates")?;
if jnow {
coords.epoch = scopinator_types::Epoch::JNow;
}
let backend = commands::neutral::connect_alpaca(host, port).await?;
commands::neutral::goto(backend, coords, Duration::from_secs(timeout)).await?;
}
},
Commands::Park { host, timeout } => match target(&host)? {
Target::Seestar { host } => commands::park(host, interop_key).await?,
Target::Alpaca { host, port } => {
let backend = commands::neutral::connect_alpaca(host, port).await?;
commands::neutral::park(backend, Duration::from_secs(timeout)).await?;
}
},
Commands::Unpark { host } => match target(&host)? {
Target::Seestar { .. } => anyhow::bail!(
"a Seestar has no unpark request: it opens its arm when it is sent a goto"
),
Target::Alpaca { host, port } => {
let backend = commands::neutral::connect_alpaca(host, port).await?;
commands::neutral::unpark(backend).await?;
}
},
Commands::Raw {
host,
method,
params,
object_marker,
} => match target(&host)? {
Target::Seestar { host } => {
commands::raw(host, &method, params.as_deref(), object_marker, interop_key).await?;
}
Target::Alpaca { .. } => {
anyhow::bail!("`raw` sends Seestar control methods; it has no Alpaca form")
}
},
}
Ok(())
}