Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-pro committed Dec 30, 2023
1 parent a4b33cc commit 51066e9
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 32 deletions.
14 changes: 9 additions & 5 deletions app/src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use maplit::btreemap;
use serde::Serialize;
use std::collections::BTreeMap;
use std::time::{SystemTime, UNIX_EPOCH};
use sunrise_sunset_calculator::SunriseSunsetParameters;
use sunrise_sunset_calculator::{SunriseSunsetParameters, SunriseSunsetResult};
use wildmatch::WildMatch;

#[derive(Clone, Debug)]
Expand All @@ -18,16 +18,18 @@ pub struct SolarAndBrightnessResults {
pub visible: bool,
}

#[derive(Debug, Default)]
#[derive(Debug)]
pub struct ApplyResults {
pub unknown_devices: Vec<brightness::Error>,
pub monitors: Vec<MonitorResult>,
pub sun: SunriseSunsetResult,
}

#[derive(Debug)]
pub struct MonitorResult {
pub name: String,
pub properties: BTreeMap<&'static str, String>,
pub brightness: Option<BrightnessDetails>,
pub brightness: BrightnessDetails,
pub set_error: Option<brightness::Error>,
}

Expand Down Expand Up @@ -134,13 +136,14 @@ pub fn apply_brightness(
}

MonitorResult {
name,
properties,
brightness: Some(BrightnessDetails {
brightness: BrightnessDetails {
expiry_time: brightness.expiry_time,
brightness: brightness.brightness,
brightness_day: monitor_day,
brightness_night: monitor_night,
}),
},
set_error: error,
}
})
Expand All @@ -155,6 +158,7 @@ pub fn apply_brightness(
ApplyResults {
unknown_devices: failed_monitors,
monitors: monitor_results,
sun,
}
}

Expand Down
39 changes: 34 additions & 5 deletions app/src/calculator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ pub fn calculate_brightness(
sun: &SunriseSunsetResult,
time_now: i64,
) -> BrightnessResult {
let low = brightness_night;
let high = brightness_day;
// Special case where there is no difference in brightness
if brightness_night == brightness_day {
return BrightnessResult {
expiry_time: time_now + 86400000,
brightness: brightness_day,
};
}

let low = brightness_day.min(brightness_night);
let high = brightness_day.max(brightness_night);
let transition_secs = transition_mins * 60; //time for transition from low to high
let half_transition_secs = (transition_secs / 2) as i64;

Expand All @@ -86,18 +94,39 @@ pub fn calculate_brightness(
) // When the sun will rise again - transition
};

// If nighttime brightness is greater than day (weird!) then we need to flip around.
let backwards = brightness_night > brightness_day;

if time_now < time_a {
let event = if sun.visible { sun.rise } else { sun.set };
sine_curve(time_now, transition_secs, event, !sun.visible, low, high)
sine_curve(
time_now,
transition_secs,
event,
!(sun.visible ^ backwards),
low,
high,
)
} else if time_now >= time_b {
// Must be greater or equal to or it would get stuck in a loop
let event = if sun.visible { sun.set } else { sun.rise };
sine_curve(time_now, transition_secs, event, sun.visible, low, high)
sine_curve(
time_now,
transition_secs,
event,
sun.visible ^ backwards,
low,
high,
)
} else {
// Time is >=A and <B, therefore the brightness next change is at B
BrightnessResult {
expiry_time: time_b,
brightness: if sun.visible { high } else { low },
brightness: if sun.visible {
brightness_day
} else {
brightness_night
},
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl From<ApplyResults> for ApplyResultsSerializable {
#[derive(Debug, Serialize)]
pub struct MonitorResultSerializable {
pub properties: BTreeMap<&'static str, String>,
pub brightness: Option<BrightnessDetails>,
pub brightness: BrightnessDetails,
pub set_error: Option<String>,
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl SsbConfig {

pub fn save(&self) -> anyhow::Result<()> {
let path = config_directory()?.join(CONFIG_FILE_NAME);
let serialised = serde_json::to_string(&self).unwrap();
let serialised = serde_json::to_string_pretty(&self).unwrap();
let mut temp_file = NamedTempFile::new()?;
temp_file.write_all(serialised.as_bytes())?;
temp_file.flush()?;
Expand Down
2 changes: 1 addition & 1 deletion app/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn calculate_timeout(results: &Option<ApplyResults>) -> Option<SystemTime> {
results
.monitors
.iter()
.flat_map(|m| m.brightness.as_ref().map(|b| b.expiry_time))
.map(|m| m.brightness.expiry_time)
.min()
.map(|e| UNIX_EPOCH + Duration::from_secs(e as u64))
} else {
Expand Down
91 changes: 72 additions & 19 deletions app/src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use crate::apply::ApplyResults;
use crate::common::UserEvent;
use crate::config::SsbConfig;
use crate::controller::Message;
use chrono::{Local, TimeZone};
use egui::{Align, Color32, Layout, ScrollArea};
use egui_winit::winit::event_loop::EventLoopProxy;
use enum_iterator::Sequence;
use std::sync::mpsc::Sender;
use std::sync::{Arc, RwLock};

const SPACING: f32 = 10.0;

pub struct SsbEguiApp {
main_loop: EventLoopProxy<UserEvent>,
selected_page: Page,
Expand All @@ -25,6 +28,14 @@ struct BrightnessSettings {
}

impl BrightnessSettings {
fn from_config(config: &SsbConfig) -> Self {
Self {
brightness_day: config.brightness_day,
brightness_night: config.brightness_night,
transition_mins: config.transition_mins,
}
}

fn copy_to_config(&self, config: &mut SsbConfig) {
config.brightness_night = self.brightness_night;
config.brightness_day = self.brightness_day;
Expand Down Expand Up @@ -69,11 +80,7 @@ impl SsbEguiApp {
selected_page: Page::Status,
overlay: None,
results,
brightness_settings_page: BrightnessSettings {
brightness_day: config_read.brightness_day,
brightness_night: config_read.brightness_night,
transition_mins: config_read.transition_mins,
},
brightness_settings_page: BrightnessSettings::from_config(&config_read),
config: config.clone(),
controller,
}
Expand Down Expand Up @@ -134,18 +141,10 @@ impl SsbEguiApp {
fn render_main(&mut self, ui: &mut egui::Ui) {
ScrollArea::both().show(ui, |ui| {
ui.heading(self.selected_page.title());
ui.add_space(10.0);
ui.add_space(SPACING);
match self.selected_page {
Page::Status => {
let results = self.results.read().unwrap();

if let Some(results) = results.as_ref() {
ui.label(format!("{} monitors", results.monitors.len()));
}
}
Page::BrightnessSettings => {
self.render_brightness_settings(ui);
}
Page::Status => self.render_status(ui),
Page::BrightnessSettings => self.render_brightness_settings(ui),
Page::LocationSettings => {}
Page::Monitors => {}
}
Expand Down Expand Up @@ -178,11 +177,65 @@ impl SsbEguiApp {
});
}

fn render_status(&mut self, ui: &mut egui::Ui) {
let results = self.results.read().unwrap();

if let Some(results) = results.as_ref() {
let date_format = "%H:%M %P (%b %d)";
let sunrise = Local
.timestamp_opt(results.sun.rise, 0)
.unwrap()
.format(date_format)
.to_string();
let sunset = Local
.timestamp_opt(results.sun.set, 0)
.unwrap()
.format(date_format)
.to_string();

egui::Grid::new("sun_times_grid")
.num_columns(2)
.show(ui, |ui| {
if results.sun.visible {
ui.label("Sunrise");
ui.label(sunrise);
ui.end_row();
ui.label("Sunset");
ui.label(sunset);
ui.end_row();
} else {
ui.label("Sunset");
ui.label(sunset);
ui.end_row();
ui.label("Sunrise");
ui.label(sunrise);
ui.end_row();
}
});

ui.separator();

egui::Grid::new("monitors_grid")
.striped(true)
.num_columns(1)
.show(ui, |ui| {
ui.label("Name");
ui.label("Brightness")
.on_hover_text("The computed brightness percentage for this monitor");
ui.end_row();

for monitor in &results.monitors {
ui.label(&monitor.name);
ui.label(format!("{}%", monitor.brightness.brightness));
ui.end_row();
}
});
}
}

fn render_brightness_settings(&mut self, ui: &mut egui::Ui) {
egui::Grid::new("brightness_settings")
.num_columns(2)
.spacing([40.0, 4.0])
.striped(true)
.show(ui, |ui| {

ui.label("Day Brightness");
Expand All @@ -198,7 +251,7 @@ impl SsbEguiApp {
ui.end_row();

});
ui.add_space(10.0);
ui.add_space(SPACING);
ui.with_layout(Layout::left_to_right(Align::LEFT), |ui| {
if ui.button("Apply").clicked() {
let mut config = self.config.write().unwrap();
Expand Down

0 comments on commit 51066e9

Please sign in to comment.