Initial Commit

+ DSN Parser working (XML is trash...)
+ Currently just prints the status of the DSN
+ Will work LED drivers next weekend
main
Joey Hines 2021-06-12 20:12:30 -06:00
commit 5ddaf4388c
No known key found for this signature in database
GPG Key ID: 80F567B5C968F91B
8 changed files with 1352 additions and 0 deletions

2
.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
/target
/.idea

1004
Cargo.lock generated 100644

File diff suppressed because it is too large Load Diff

12
Cargo.toml 100644
View File

@ -0,0 +1,12 @@
[package]
name = "dsn-visualizer"
version = "0.1.0"
authors = ["Joey Hines <joey@ahines.net>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = {version = "0.11", features=["blocking"]}
chrono = {version = "0.4.19", features=["serde"]}
xml-rs = "0.8.3"

7
LICENSE 100644
View File

@ -0,0 +1,7 @@
Copyright 2021 Joey Hines
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

5
REAMDE.md 100644
View File

@ -0,0 +1,5 @@
# NASA Deep Space Network Visualizer
A Rust project to display the status of the NASA Deep Space Network.
## License
[License](LICENSE)

69
src/dsn/mod.rs 100644
View File

@ -0,0 +1,69 @@
use crate::dsn::models::{Dish, ParseError, Signal, Station, Target};
use std::convert::TryFrom;
use std::io::BufReader;
use xml::reader::XmlEvent;
use xml::EventReader;
pub mod models;
pub fn parse_dsn_resp<T>(reader: BufReader<T>) -> Result<Vec<Station>, ParseError>
where
T: std::io::Read,
{
let mut stations = Vec::new();
let mut station: Option<Station> = None;
let mut dish: Option<Dish> = None;
let parser = EventReader::new(reader).into_iter();
for e in parser {
match e {
Ok(XmlEvent::StartElement {
name, attributes, ..
}) => {
if name.local_name.contains("station") {
if let Some(station) = station {
stations.push(station);
}
station = Some(Station::try_from(attributes).unwrap());
} else if name.local_name.contains("dish") {
dish = Some(Dish::try_from(attributes).unwrap());
} else if name.local_name.contains("downSignal") {
dish = Some(
dish.unwrap()
.set_down_signal(Signal::try_from(attributes).unwrap()),
);
} else if name.local_name.contains("upSignal") {
dish = Some(
dish.unwrap()
.set_up_signal(Signal::try_from(attributes).unwrap()),
);
} else if name.local_name.contains("target") {
dish = Some(
dish.unwrap()
.set_target(Target::try_from(attributes).unwrap()),
);
}
}
Ok(XmlEvent::EndElement { name }) => {
if name.local_name.contains("dish") && station.is_some() && dish.is_some() {
station = Some(station.unwrap().add_dish(dish.unwrap()));
dish = None;
}
}
Ok(XmlEvent::EndDocument) => {
if let Some(station) = station {
stations.push(station);
break;
}
}
Err(e) => {
println!("Error: {}", e);
break;
}
_ => {}
}
}
Ok(stations)
}

218
src/dsn/models.rs 100644
View File

@ -0,0 +1,218 @@
use std::convert::TryFrom;
use std::num::{ParseFloatError, ParseIntError};
use std::str::ParseBoolError;
use chrono::{DateTime, Utc};
use xml::attribute::OwnedAttribute;
#[derive(Clone, Debug)]
pub enum ParseError {
AttrMissing,
AttrParseInt(ParseIntError),
AttrParseFloat(ParseFloatError),
AttrParseBool(ParseBoolError),
AttrParseChrono(chrono::ParseError),
}
impl From<ParseIntError> for ParseError {
fn from(error: ParseIntError) -> Self {
ParseError::AttrParseInt(error)
}
}
impl From<ParseFloatError> for ParseError {
fn from(error: ParseFloatError) -> Self {
ParseError::AttrParseFloat(error)
}
}
impl From<ParseBoolError> for ParseError {
fn from(error: ParseBoolError) -> Self {
ParseError::AttrParseBool(error)
}
}
impl From<chrono::ParseError> for ParseError {
fn from(error: chrono::ParseError) -> Self {
ParseError::AttrParseChrono(error)
}
}
fn get_attr(attrs: &[OwnedAttribute], name: &str) -> Result<String, ParseError> {
attrs
.iter()
.find_map(|o| {
if o.name.local_name.eq(name) {
Some(o.value.clone())
} else {
None
}
})
.ok_or(ParseError::AttrMissing)
}
#[derive(Clone, Debug)]
pub struct Station {
pub name: String,
pub friendly_name: String,
pub time_utc: u64,
pub tz_offset: i64,
pub dishes: Vec<Dish>,
}
impl Station {
pub fn add_dish(mut self, dish: Dish) -> Self {
self.dishes.push(dish);
self
}
}
impl TryFrom<Vec<OwnedAttribute>> for Station {
type Error = ParseError;
fn try_from(attrs: Vec<OwnedAttribute>) -> Result<Self, Self::Error> {
let name = get_attr(&attrs, "name")?;
let friendly_name = get_attr(&attrs, "friendlyName")?;
let time_utc = get_attr(&attrs, "timeUTC")?.parse::<u64>()?;
let tz_offset = get_attr(&attrs, "timeZoneOffset")?.parse::<i64>()?;
Ok(Self {
name,
friendly_name,
time_utc,
tz_offset,
dishes: Vec::new(),
})
}
}
#[derive(Clone, Debug, Default)]
pub struct Signal {
pub signal_type: String,
pub signal_type_debug: String,
pub data_rate: Option<f64>,
pub frequency: Option<f64>,
pub power: Option<f64>,
pub spacecraft: String,
pub spacecraft_id: Option<u32>,
}
impl TryFrom<Vec<OwnedAttribute>> for Signal {
type Error = ParseError;
fn try_from(attrs: Vec<OwnedAttribute>) -> Result<Self, Self::Error> {
let signal_type = get_attr(&attrs, "signalType")?;
let signal_type_debug = get_attr(&attrs, "signalTypeDebug")?;
let data_rate = get_attr(&attrs, "dataRate")?.parse::<f64>().ok();
let frequency = get_attr(&attrs, "frequency")?.parse::<f64>().ok();
let power = get_attr(&attrs, "power")?.parse::<f64>().ok();
let spacecraft = get_attr(&attrs, "spacecraft")?;
let spacecraft_id = get_attr(&attrs, "spacecraftId")?.parse::<u32>().ok();
Ok(Self {
signal_type,
signal_type_debug,
data_rate,
frequency,
power,
spacecraft,
spacecraft_id,
})
}
}
#[derive(Clone, Debug, Default)]
pub struct Target {
name: String,
id: u32,
/// Up Leg Range (km)
upleg_range: f64,
/// Down Leg Leg Range (km)
downleg_range: f64,
/// Round trip light time (s)
rtlt: f64,
}
impl TryFrom<Vec<OwnedAttribute>> for Target {
type Error = ParseError;
fn try_from(attrs: Vec<OwnedAttribute>) -> Result<Self, Self::Error> {
let name = get_attr(&attrs, "name")?;
let id = get_attr(&attrs, "id")?.parse::<u32>()?;
let upleg_range = get_attr(&attrs, "uplegRange")?.parse::<f64>()?;
let downleg_range = get_attr(&attrs, "downlegRange")?.parse::<f64>()?;
let rtlt = get_attr(&attrs, "rtlt")?.parse::<f64>()?;
Ok(Self {
name,
id,
upleg_range,
downleg_range,
rtlt,
})
}
}
#[derive(Clone, Debug)]
pub struct Dish {
pub name: String,
pub azimuth_angle: Option<f64>,
pub elevation_angle: Option<f64>,
pub wind_speed: Option<f64>,
pub is_mspa: bool,
pub is_array: bool,
pub is_ddor: bool,
pub created: DateTime<Utc>,
pub updated: DateTime<Utc>,
pub up_signal: Signal,
pub down_signal: Signal,
pub target: Target,
}
impl Dish {
pub fn set_up_signal(mut self, signal: Signal) -> Self {
self.up_signal = signal;
self
}
pub fn set_down_signal(mut self, signal: Signal) -> Self {
self.down_signal = signal;
self
}
pub fn set_target(mut self, target: Target) -> Self {
self.target = target;
self
}
}
impl TryFrom<Vec<OwnedAttribute>> for Dish {
type Error = ParseError;
fn try_from(attrs: Vec<OwnedAttribute>) -> Result<Self, Self::Error> {
let name = get_attr(&attrs, "name")?;
let azimuth_angle = get_attr(&attrs, "azimuthAngle")?.parse::<f64>().ok();
let elevation_angle = get_attr(&attrs, "elevationAngle")?.parse::<f64>().ok();
let wind_speed = get_attr(&attrs, "windSpeed")?.parse::<f64>().ok();
let is_mspa = get_attr(&attrs, "isMSPA")?.parse::<bool>()?;
let is_array = get_attr(&attrs, "isArray")?.parse::<bool>()?;
let is_ddor = get_attr(&attrs, "isDDOR")?.parse::<bool>()?;
let created = get_attr(&attrs, "created")?.parse::<DateTime<Utc>>()?;
let updated = get_attr(&attrs, "updated")?.parse::<DateTime<Utc>>()?;
Ok(Self {
name,
azimuth_angle,
elevation_angle,
wind_speed,
is_mspa,
is_array,
is_ddor,
created,
updated,
up_signal: Signal::default(),
down_signal: Signal::default(),
target: Target::default(),
})
}
}

35
src/main.rs 100644
View File

@ -0,0 +1,35 @@
use crate::dsn::parse_dsn_resp;
use reqwest::blocking::Client;
use std::collections::HashMap;
use std::io::BufReader;
use std::time::{SystemTime, UNIX_EPOCH};
mod dsn;
fn main() {
let client = Client::new();
let mut params = HashMap::new();
let timestamp = (SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis()
/ 5000) as u64;
params.insert("r", timestamp);
let resp = client
.get("https://eyes.nasa.gov/dsn/data/dsn.xml")
.query(&params)
.send()
.unwrap()
.text()
.unwrap();
let file = BufReader::new(resp.as_bytes());
let stations = parse_dsn_resp(file).unwrap();
for station in stations {
println!("Station: {:?}\n Dishes: {:?}", station.name, station.dishes)
}
}