Skip to content

Commit

Permalink
Added basic structure for configurable personal minima.
Browse files Browse the repository at this point in the history
  • Loading branch information
RRArny committed Oct 16, 2024
1 parent 1075ceb commit 56b9ce6
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 64 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ For wxfetch to work you will need a free account on https://avwx.rest/. Once you

## Todos

- [ ] Personal wx minima
- [ ] Configuration options
- [ ] Personal wx minima
- [ ] Load secret from environment instead of file
15 changes: 1 addition & 14 deletions bacon.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ command = [
]
need_stdout = false

# Run clippy on all targets
# To disable some lints, you may change the job this way:
# [jobs.clippy-all]
# command = [
# "cargo", "clippy",
# "--all-targets",
# "--color", "always",
# "--",
# "-A", "clippy::bool_to_int_with_if",
# "-A", "clippy::collapsible_if",
# "-A", "clippy::derive_partial_eq_without_eq",
# ]
# need_stdout = false
[jobs.clippy-all]
command = [
"cargo", "clippy",
Expand Down Expand Up @@ -99,4 +86,4 @@ allow_warnings = true
# should go in your personal global prefs.toml file instead.
[keybindings]
# alt-m = "job:my-job"
c = "job:clippy-all" # comment this to have 'c' run clippy on only the default target
c = "job:clippy" # comment this to have 'c' run clippy on only the default target
2 changes: 1 addition & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn request_wx(config: &Config, secrets: &Secrets) -> Option<Value> {
}
}

/// Given a properly formattet position string and Secrets, requests METAR from `AvWx` and wraps the Response in a Result.
/// Given a properly formattet position string and Secrets, requests METAR from avwx and wraps the Response in a Result.
async fn send_api_call(position: String, secrets: &Secrets) -> Result<Response, Error> {
let uri = format!("https://avwx.rest/api/metar/{position}?onfail=nearest&options=info");
let resp: Response = Client::new()
Expand Down
36 changes: 35 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WxFetch - main.rs

use api::{check_icao_code, request_wx};
use chrono::TimeDelta;
use clap::Parser;
use colored::ColoredString;
use std::{fs::File, io::Read};
Expand Down Expand Up @@ -40,6 +41,36 @@ struct Args {

struct Config {
position: Position,
cloud_minimum: i64,
cloud_marginal: i64,
temp_minimum: i64,
spread_minimum: i64,
wind_var_maximum: i64,
wind_maximum: i64,
gust_maximum: i64,
age_maximum: TimeDelta,
age_marginal: TimeDelta,
visibility_minimum: i64,
visibility_marginal: i64,
}

impl Default for Config {
fn default() -> Self {
Self {
position: Position::GeoIP,
cloud_minimum: 6,
cloud_marginal: 15,
temp_minimum: 0,
spread_minimum: 3,
wind_var_maximum: 45,
wind_maximum: 15,
gust_maximum: 10,
age_maximum: TimeDelta::hours(1),
age_marginal: TimeDelta::hours(6),
visibility_minimum: 1500,
visibility_marginal: 5000,
}
}
}

struct Secrets {
Expand Down Expand Up @@ -69,20 +100,23 @@ async fn get_config(secrets: &Secrets) -> Config {
if check_icao_code(&icao, secrets).await {
return Config {
position: Position::Airfield(icao),
..Default::default()
};
}
println!("Invalid airfield {icao}. Defaulting to geoip...");
} else if let Some(lat) = args.latitude {
if let Some(long) = args.longitude {
return Config {
position: Position::LatLong(LatLong(lat, long)),
..Default::default()
};
}
println!("Please provide both Latitude and Longitude. Defaulting to geoip...");
}

Config {
position: Position::GeoIP,
..Default::default()
}
}

Expand All @@ -91,7 +125,7 @@ async fn get_weather(config: &Config, secrets: &Secrets) -> ColoredString {
.await
.expect("Weather request failed.");
let metar = Metar::from_json(&json, config).expect("Invalid weather data received...");
metar.colorise()
metar.colorise(config)
}

#[tokio::main]
Expand Down
Loading

0 comments on commit 56b9ce6

Please sign in to comment.