Skip to content

Commit

Permalink
Merge branch 'main' into barebones
Browse files Browse the repository at this point in the history
  • Loading branch information
CaspianA1 committed Sep 17, 2024
2 parents 06d81bd + 7003f4c commit 75ca13d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/dashboard_defs/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,10 @@ pub fn make_dashboard(
Vec2f::new(0.4, 0.3),
update_rate_creator,
&api_keys.tomorrow_io,
"Brunswick",
"ME",
"US"
);
WindowContents::Color(ColorSDL::RGB(255, 0, 255)),
theme_color_1,
theme_color_1
)?;

////////// Making some static texture windows

Expand Down
42 changes: 23 additions & 19 deletions src/dashboard_defs/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::{

#[derive(Clone)]
struct WeatherStateData {
text_color: ColorSDL,

request_url: String,
weather_changed: bool,

Expand All @@ -39,9 +41,7 @@ impl Updatable for WeatherStateData {
let response = request::get(&self.request_url)?;
let all_info_json: serde_json::Value = serde_json::from_str(response.as_str()?)?;

// the `minutely` field is for all of the weather 1 hour forward, in in minute increments
let weather_per_minute_for_next_hour_json = &all_info_json["timelines"]["minutely"];
let current_weather_json = &weather_per_minute_for_next_hour_json[0]["values"];
let current_weather_json = &all_info_json["data"]["timelines"][0]["intervals"][0]["values"];

let associated_code = current_weather_json["weatherCode"].as_i64().unwrap() as u16;
let (weather_code_descriptor, associated_emoji) = WEATHER_CODE_MAPPING.get(&(associated_code)).unwrap();
Expand Down Expand Up @@ -103,23 +103,21 @@ pub fn weather_updater_fn(params: WindowUpdaterParams) -> MaybeError {
let individual_window_state = params.window.get_state_mut::<ContinuallyUpdated<WeatherStateData>>();

individual_window_state.update(&())?;
let inner_state = individual_window_state.get_data();
let inner = individual_window_state.get_data();

if !inner_state.weather_changed || inner_state.curr_weather_info.is_none() {
if !inner.weather_changed || inner.curr_weather_info.is_none() {
return Ok(());
}

let (rounded_temperature, weather_code_descriptor, associated_emoji) = inner_state.curr_weather_info.unwrap();
let (rounded_temperature, weather_code_descriptor, associated_emoji) = inner.curr_weather_info.unwrap();
let weather_string = format!("Weather: {rounded_temperature}° and {weather_code_descriptor} {associated_emoji}");

let weather_text_color = ColorSDL::BLACK;

let texture_creation_info = TextureCreationInfo::Text((
Cow::Borrowed(inner_shared_state.font_info),

TextDisplayInfo {
text: DisplayText::new(&weather_string),
color: weather_text_color,
color: inner.text_color,
pixel_area: params.area_drawn_to_screen,

scroll_fn: |seed, _| {
Expand All @@ -141,39 +139,45 @@ pub fn weather_updater_fn(params: WindowUpdaterParams) -> MaybeError {
pub fn make_weather_window(
top_left: Vec2f, size: Vec2f,
update_rate_creator: UpdateRateCreator, api_key: &str,
city_name: &str, state_code: &str, country_code: &str) -> Window {
background_contents: WindowContents,
text_color: ColorSDL, border_color: ColorSDL) -> GenericResult<Window> {

let curr_location_response = request::get("https://ipinfo.io/json")?;
let curr_location_json: serde_json::Value = serde_json::from_str(curr_location_response.as_str()?)?;
let location = &curr_location_json["loc"].as_str().context("No location field available!")?;

const UPDATE_RATE_SECS: Seconds = 60.0 * 10.0; // Once every 10 minutes

let weather_update_rate = update_rate_creator.new_instance(UPDATE_RATE_SECS);
let location = [city_name, state_code, country_code].join(",");

let request_url = request::build_url("https://api.tomorrow.io/v4/weather/forecast",
let request_url = request::build_url("https://api.tomorrow.io/v4/timelines",
&[],

// TODO: limit the retreived fields somehow
&[
("apikey", Cow::Borrowed(api_key)),
("location", Cow::Borrowed(&location)),
("units", Cow::Borrowed("imperial"))
("location", Cow::Borrowed(location)),
("timesteps", Cow::Borrowed("1m")),
("units", Cow::Borrowed("imperial")),
("fields", Cow::Borrowed("temperature,weatherCode"))
]
);

let data = WeatherStateData {
text_color,
request_url,
weather_changed: false,
curr_weather_info: None
};

let continually_updated = ContinuallyUpdated::new(&data, &(), "Weather");

Window::new(
Ok(Window::new(
Some((weather_updater_fn, weather_update_rate)),
DynamicOptional::new(continually_updated),
WindowContents::Color(ColorSDL::RGB(255, 0, 255)),
Some(ColorSDL::RED),
background_contents,
Some(border_color),
top_left,
size,
None
)
))
}

0 comments on commit 75ca13d

Please sign in to comment.