Initial commit
This commit is contained in:
commit
c9ab8eb43c
7 changed files with 1176 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/target
|
1042
Cargo.lock
generated
Normal file
1042
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "iss_locator"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
reqwest = { version = "0.11.18",features = ["json","blocking"]}
|
||||||
|
serde_json = "1.0.104"
|
9
LICENSE
Normal file
9
LICENSE
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) <year> <copyright holders>
|
||||||
|
|
||||||
|
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
README.md
Normal file
5
README.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# ISS locator
|
||||||
|
|
||||||
|
Borne out of an idea about Amature Radio, i wanted to have a utility that can quickly check if ISS is in visible and amature radio range to listen to.
|
||||||
|
|
||||||
|
Your location is recieved from IP, ISS location from an API, plagiarized Haversine formula to check the distance between.
|
74
src/lib.rs
Normal file
74
src/lib.rs
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
|
||||||
|
pub struct Location {
|
||||||
|
pub lat: f64,
|
||||||
|
pub long: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Location {
|
||||||
|
pub fn build(args: &[String]) -> Result<Location, &'static str> {
|
||||||
|
if args.len() < 2{
|
||||||
|
println!("{}",args.len());
|
||||||
|
return Err("Not enough arguments");
|
||||||
|
}
|
||||||
|
|
||||||
|
let lat: f64 = match args[0].parse::<f64>() {
|
||||||
|
Ok(num) =>
|
||||||
|
if num >= -90.0 && num <= 90.0 {
|
||||||
|
num
|
||||||
|
} else {
|
||||||
|
panic!("Invalid Lattitude: {num}")
|
||||||
|
},
|
||||||
|
Err(e) => panic!("{:?}",e),
|
||||||
|
};
|
||||||
|
|
||||||
|
let long: f64 = match args[1].parse::<f64>() {
|
||||||
|
Ok(num) =>
|
||||||
|
if num >= -180.0 && num <= 180.0 {
|
||||||
|
num
|
||||||
|
} else {
|
||||||
|
panic!("Invalid Longitude: {num}")
|
||||||
|
},
|
||||||
|
Err(e) => panic!("{:?}",e),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Location { lat,long })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_distance(local: Location, distant: Location) -> f64 {
|
||||||
|
let r:f64 = 6367.45;
|
||||||
|
let delta_long = (local.long - distant.long).to_radians();
|
||||||
|
let delta_lat = (local.lat - distant.lat).to_radians();
|
||||||
|
|
||||||
|
let ang_inner = (delta_lat / 2.0).sin().powi(2)
|
||||||
|
+ local.lat.to_radians().cos() * distant.lat.to_radians().cos() * (delta_long / 2.0).sin().powi(2);
|
||||||
|
let ang = 2.0 * ang_inner.sqrt().asin();
|
||||||
|
r*ang
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_iss_location() -> Result<[String;2],Box<dyn std::error::Error>>{
|
||||||
|
let object: Value = match reqwest::blocking::get("http://api.open-notify.org/iss-now.json")?.json::<serde_json::Value>() {
|
||||||
|
Ok(object) => object["iss_position"].clone(),
|
||||||
|
Err(e) => panic!("Error serializing json data: {e}"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut lat = object["latitude"].to_string();
|
||||||
|
lat.retain(|c| !r#"()":'"#.contains(c));
|
||||||
|
let mut long = object["longitude"].to_string();
|
||||||
|
long.retain(|c| !r#"()":'"#.contains(c));
|
||||||
|
Ok([lat,long])
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_local_location() -> Result<[String;2],Box<dyn std::error::Error>>{
|
||||||
|
let object: Value = match reqwest::blocking::get("http://ip-api.com/json/")?.json::<serde_json::Value>() {
|
||||||
|
Ok(object) => object.clone(),
|
||||||
|
Err(e) => panic!("Error serializing json data: {e}"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let (lat,long) = (object["lat"].to_string(),object["lon"].to_string());
|
||||||
|
Ok([lat,long])
|
||||||
|
}
|
||||||
|
|
35
src/main.rs
Normal file
35
src/main.rs
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
use std::process;
|
||||||
|
use iss_locator::Location;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
let iss_location = match iss_locator::get_iss_location() {
|
||||||
|
Ok(string) => string,
|
||||||
|
Err(e) => panic!("Iss location aquisition failure: {e}"),
|
||||||
|
};
|
||||||
|
let local_location = match iss_locator::get_local_location() {
|
||||||
|
Ok(string) => string,
|
||||||
|
Err(e) => panic!("Local location aquisition failure: {e}"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let iss: Location = Location::build(&iss_location).unwrap_or_else(|err| {
|
||||||
|
eprintln!("{err}");
|
||||||
|
process::exit(1);
|
||||||
|
});
|
||||||
|
let local: Location = Location::build(&local_location).unwrap_or_else(|err| {
|
||||||
|
eprintln!("{err}");
|
||||||
|
process::exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
println!("local:{},{}",local.lat,local.long);
|
||||||
|
println!(" iss :{},{}",iss.lat,iss.long);
|
||||||
|
let distance = iss_locator::get_distance(local,iss);
|
||||||
|
println!("distance: {:.3} km",distance);
|
||||||
|
if distance < 2246.6449 {
|
||||||
|
println!("You are within ISS range");
|
||||||
|
} else {
|
||||||
|
println!("Outside of ISS range");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue