Compare commits

...

2 commits

Author SHA1 Message Date
850c629eca added moonphase to output 2023-11-08 18:56:30 -08:00
ca4b5ffcda fixed angle percentage 2023-11-08 18:44:48 -08:00

View file

@ -16,13 +16,13 @@ pub fn run_calendar(args: &CliArgs) {
} }
let (mut moonrise, mut moonset, mut moonday, mut sunrise, mut sunset): ( let (mut moonrise, mut moonset, mut moonday, mut sunrise, mut sunset): (
Option<(u32, u32)>, Option<i32>,
Option<(u32, u32)>, Option<i32>,
Option<(u32, u32)>, Option<i32>,
Option<(u32, u32)>, Option<i32>,
Option<(u32, u32)>, Option<i32>,
) = (None, None, None, None, None); ) = (None, None, None, None, None);
let moonphase: Option<u32> = None; let mut moonphase: Option<u32> = None;
let mut false_empty_count = 0; let mut false_empty_count = 0;
loop { loop {
@ -42,13 +42,13 @@ pub fn run_calendar(args: &CliArgs) {
let value = buffer[21..].to_string(); let value = buffer[21..].to_string();
if value.starts_with("Moonrise") { if value.starts_with("Moonrise") {
moonrise = parse_time(value[8..].trim(), args.debug); moonrise = parse_time(value[8..].trim());
} else if value.starts_with("Moonset") { } else if value.starts_with("Moonset") {
moonset = parse_time(value[7..].trim(), args.debug); moonset = parse_time(value[7..].trim());
} else if value.starts_with("Moonday") { } else if value.starts_with("Moonday") {
moonday = parse_time(value[7..].trim(), args.debug); moonday = parse_time(value[7..].trim());
} else if value.starts_with("Moonphase") { } else if value.starts_with("Moonphase") {
let moonphase = match value[9..].trim()[0..2].parse::<u32>() { moonphase = match value[9..].trim()[0..2].parse::<u32>() {
Ok(val) => Some(val), Ok(val) => Some(val),
Err(e) => { Err(e) => {
if args.debug { if args.debug {
@ -58,9 +58,9 @@ pub fn run_calendar(args: &CliArgs) {
} }
}; };
} else if value.starts_with("Sunrise") { } else if value.starts_with("Sunrise") {
sunrise = parse_time(value[7..].trim(), args.debug); sunrise = parse_time(value[7..].trim());
} else if value.starts_with("Sunset") { } else if value.starts_with("Sunset") {
sunset = parse_time(value[6..].trim(), args.debug); sunset = parse_time(value[6..].trim());
} else { } else {
continue; continue;
} }
@ -79,55 +79,56 @@ pub fn run_calendar(args: &CliArgs) {
let mut output_state = CalandarState { let mut output_state = CalandarState {
has_bg: false, has_bg: false,
is_sun: false, is_sun: false,
gradient_angle: 0, gradient_angle_percentage: 0.0,
moonphase: 0,
}; };
let (now_hr, now_min) = (Local::now().hour(), Local::now().minute()); if moonphase.is_some() {
output_state.moonphase = moonphase.unwrap();
}
let now_mins = (Local::now().hour() * 60 +Local::now().minute()) as i32;
if sunrise.is_none() { if sunrise.is_none() {
sunrise = Some((8, 0)); sunrise = Some(8 * 60 + 0);
} }
if sunset.is_none() { if sunset.is_none() {
sunset = Some((18, 0)); sunset = Some(18 * 60 + 0);
} }
let sun_rise = sunrise.clone().unwrap(); let sun_rise = sunrise.clone().unwrap();
let sun_set = sunset.clone().unwrap(); let sun_set = sunset.clone().unwrap();
if now_hr > sun_rise.0 && now_min > sun_rise.1 && now_hr < sun_set.0 && now_min < sun_set.1 { if now_mins > sun_rise && now_mins < sun_set {
// it is after sunrise, and before sunset // it is after sunrise, and before sunset
output_state.has_bg = true; output_state.has_bg = true;
output_state.is_sun = true; output_state.is_sun = true;
output_state.gradient_angle = { output_state.gradient_angle_percentage = {
let sunrise_mins = sun_rise.0 * 60 + sun_rise.1; (now_mins - sun_rise) as f32
(((now_hr * 60 + now_min) - sunrise_mins) as f32 / (sun_set - sun_rise) as f32
/ ((sun_set.0 * 60 + sun_set.1) - sunrise_mins) as f32)
.floor() as i32
}; };
} else { } else {
// Check if the moon is up // Check if the moon is up
if moonrise.is_some() { if moonrise.is_some() {
let (moonrise_hr, moonrise_min) = moonrise.clone().unwrap(); let moon_rise = moonrise.clone().unwrap();
if moonset.is_some() { if moonset.is_some() {
let (moonset_hr, moonset_min) = moonset.clone().unwrap(); let moon_set = moonset.clone().unwrap();
if now_hr > moonrise_hr if now_mins > moon_rise
&& now_min > moonrise_min && now_mins < moon_set
&& now_hr < moonset_hr
&& now_min < moonset_min
{ {
// moon is up // moon is up
output_state.has_bg = true; output_state.has_bg = true;
output_state.is_sun = false; output_state.is_sun = false;
} }
} else { } else {
if now_hr > moonrise_hr && now_min > moonrise_hr { if now_mins > moon_rise {
output_state.has_bg = true; output_state.has_bg = true;
output_state.is_sun = false; output_state.is_sun = false;
} }
} }
} else { } else {
if moonset.is_some() { if moonset.is_some() {
let (moonset_hr, moonset_min) = moonset.clone().unwrap(); let moon_set = moonset.clone().unwrap();
if now_hr < moonset_hr && now_min < moonset_min { if now_mins < moon_set {
// moon is up // moon is up
output_state.has_bg = true; output_state.has_bg = true;
output_state.is_sun = false; output_state.is_sun = false;
@ -138,19 +139,19 @@ pub fn run_calendar(args: &CliArgs) {
if output_state.has_bg == true && output_state.is_sun == false { if output_state.has_bg == true && output_state.is_sun == false {
// moon is up, get the position // moon is up, get the position
if moonrise.is_some() { if moonrise.is_some() {
let (moonrise_hr, moonrise_min) = moonrise.clone().unwrap(); let moon_rise = moonrise.clone().unwrap();
let (moonday_hrs, moonday_mins) = moonday.clone().unwrap(); let moon_day = moonday.clone().unwrap();
output_state.gradient_angle = { output_state.gradient_angle_percentage = {
(((now_hr * 60 + now_min) - (moonrise_hr * 60 + moonrise_min)) as f32 (now_mins - moon_rise) as f32
/ (moonday_hrs * 60 + moonday_mins) as f32) as i32 / moon_day as f32
}; };
} else { } else {
let (moonset_hr, moonset_min) = moonset.clone().unwrap(); let moon_set = moonset.clone().unwrap();
let (moonday_hrs, moonday_mins) = moonday.clone().unwrap(); let moon_day = moonday.clone().unwrap();
let begin_mins: i32 = (moonset_hr as i32 - moonday_hrs as i32) * 60 + (moonset_min as i32 - moonday_mins as i32); let begin_mins: i32 = moon_set - moon_day;
let time_since_rise = (now_hr * 60 + now_min) as i32 + (begin_mins.abs()); let time_since_rise = now_mins + (begin_mins.abs());
output_state.gradient_angle = (time_since_rise as f32 / (moonset_hr * 60 + moonset_min) as f32) as i32; output_state.gradient_angle_percentage = time_since_rise as f32 / moon_set as f32;
} }
} }
@ -159,34 +160,29 @@ pub fn run_calendar(args: &CliArgs) {
println!("{}", serde_json::to_string(&output_state).unwrap()); println!("{}", serde_json::to_string(&output_state).unwrap());
} }
fn parse_time(i: &str, do_debug: bool) -> Option<(u32, u32)> { fn parse_time(i: &str) -> Option<i32> {
if do_debug { debug!("parsing: {}", i);
debug!("parsing: {}", i);
}
let hr = match i[0..2].parse::<u32>() { let hr = match i[0..2].parse::<u32>() {
Ok(val) => val, Ok(val) => val as i32,
Err(e) => { Err(e) => {
if do_debug { debug!("Error parsing: {e}");
debug!("Error parsing: {e}");
}
return None; return None;
} }
}; };
let min = match i[3..5].parse::<u32>() { let min = match i[3..5].parse::<u32>() {
Ok(val) => val, Ok(val) => val as i32,
Err(e) => { Err(e) => {
if do_debug { debug!("Error parsing: {e}");
debug!("Error parsing: {e}");
}
return None; return None;
} }
}; };
Some((hr, min)) Some(hr * 60 + min)
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct CalandarState { struct CalandarState {
pub has_bg: bool, pub has_bg: bool,
pub is_sun: bool, pub is_sun: bool,
pub gradient_angle: i32, pub gradient_angle_percentage: f32,
pub moonphase: u32,
} }