Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add weather icons for night #1941

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions files/icons/awesome4.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ volume_muted = "\uf026 \uf00d"
microphone = "\uf130" # fa-microphone
microphone_muted = "\uf131" # fa-microphone-slash
weather_clouds = "\uf0c2" # fa-cloud
weather_clouds_night = "\uf0c2" # fa-cloud
weather_default = "\uf0c2" # fa-cloud
weather_fog = "\uf0c2" # fa-cloud
weather_fog_night = "\uf0c2" # fa-cloud
weather_moon = "\uf186" # fa-moon-o
weather_rain = "\uf043" # fa-tint
weather_rain_night = "\uf043" # fa-tint
weather_snow = "\uf2dc" # fa-snowflake-o
weather_sun = "\uf185" # fa-sun-o
weather_thunder = "\uf0e7" # fa-bolt
weather_thunder_night = "\uf0e7" # fa-bolt
xrandr = "\uf26c" # fa-television
16 changes: 11 additions & 5 deletions files/icons/awesome5.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,16 @@ volume = [
volume_muted = "\uf6a9"
microphone = "\uf3c9"
microphone_muted = "\uf539"
weather_clouds = "\uf0c2"
weather_clouds = "\uf0c2" # fa-cloud
weather_default = "\uf0c2" # Cloud symbol as default
weather_rain = "\uf043"
weather_snow = "\uf2dc"
weather_sun = "\uf185"
weather_thunder = "\uf0e7"
weather_clouds_night = "\uf6c3" # fa-cloud-moon
weather_fog = "\uf0c2" # fa-cloud
weather_fog_night = "\uf0c2" # fa-cloud
weather_moon = "\uf186" # fa-moon
weather_rain = "\uf743" # fa-cloud-sun-rain
weather_rain_night = "\uf73c" # fa-cloud-moon-rain
weather_snow = "\uf2dc" # fa-snowflake
weather_sun = "\uf185" # fa-sun
weather_thunder = "\uf0e7" # fa-bolt
weather_thunder_night = "\uf0e7" # fa-bolt
xrandr = "\uf26c"
16 changes: 11 additions & 5 deletions files/icons/awesome6.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ volume = [
volume_muted = "\uf6a9"
microphone = "\uf3c9"
microphone_muted = "\uf539"
weather_clouds = "\uf0c2"
weather_clouds = "\uf0c2" # fa-cloud
weather_default = "\uf0c2" # Cloud symbol as default
weather_rain = "\uf043"
weather_snow = "\uf2dc"
weather_sun = "\uf185"
weather_thunder = "\uf0e7"
weather_clouds_night = "\uf6c3" # fa-cloud-moon
weather_fog = "\uf0c2" # fa-cloud
weather_fog_night = "\uf0c2" # fa-cloud
weather_moon = "\uf186" # fa-moon
weather_rain = "\uf743" # fa-cloud-sun-rain
weather_rain_night = "\uf73c" # fa-cloud-moon-rain
weather_snow = "\uf2dc" # fa-snowflake
weather_sun = "\uf185" # fa-sun
weather_thunder = "\uf0e7" # fa-bolt
weather_thunder_night = "\uf0e7" # fa-bolt
xrandr = "\uf26c"
6 changes: 6 additions & 0 deletions files/icons/material-nf.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,15 @@ microphone = [
"\U000f036c", # nf-md-microphone
]
weather_clouds = "\ue33d" # nf-weather-cloud
weather_clouds_night = "\ue37e" # nf-weather-night_alt_cloudy
weather_default = "\ue33d" # Cloud symbol as default
weather_fog = "\ue313" # nf-weather-fog
weather_fog_night = "\ue346" # nf-weather-night_fog
weather_moon = "\uf186" # nf-fa-moon_o
weather_rain = "\ue371" # nf-weather-raindrop
weather_rain_night = "\ue325" # nf-weather-night_alt_rain
weather_snow = "\ue36f" # nf-weather-snowflake_cold
weather_sun = "\ue30d" # nf-weather-day_sunny
weather_thunder = "\ue31d" # nf-weather-thunderstorm
weather_thunder_night = "\ue32a" # nf-weather-night_alt_thunderstorm
xrandr = "\U000f037a" # nf-md-monitor_multiple
12 changes: 9 additions & 3 deletions files/icons/material.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,15 @@ microphone = [
]
microphone_muted = "\ue02b" # mic_off
weather_clouds = "\ue42d" # wb_cloudy
weather_clouds_night = "\uea46" # nights_stay
weather_default = "\ue42d" # wb_cloudy
weather_rain = "\ue42d" # wb_cloudy
weather_snow = "\ue42d" # wb_cloudy
weather_fog = "\ue42d" # wb_cloudy
weather_fog_night = "\ue42d" # wb_cloudy
weather_moon = "\uf03d" # nightlight
weather_rain = "\ue798" # water_drop
weather_rain_night = "\ue798" # water_drop
weather_snow = "\ueb3b" # ac_unit
weather_sun = "\ue430" # wb_sunny
weather_thunder = "\ue42d" # wb_cloudy
weather_thunder = "\uebdb" # thunderstorm
weather_thunder_night = "\uebdb" # thunderstorm
xrandr = "\ue31e" # laptop
38 changes: 25 additions & 13 deletions src/blocks/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,17 @@
//!
//! # Used Icons
//!
//! - `weather_sun` (when weather is reported as "Clear")
//! - `weather_rain` (when weather is reported as "Rain" or "Drizzle")
//! - `weather_clouds` (when weather is reported as "Clouds", "Fog" or "Mist")
//! - `weather_thunder` (when weather is reported as "Thunderstorm")
//! - `weather_sun` (when weather is reported as "Clear" during the day)
//! - `weather_moon` (when weather is reported as "Clear" at night)
//! - `weather_clouds` (when weather is reported as "Clouds" during the day)
//! - `weather_clouds_night` (when weather is reported as "Clouds" at night)
//! - `weather_fog` (when weather is reported as "Fog" or "Mist" during the day)
//! - `weather_fog_night` (when weather is reported as "Fog" or "Mist" at night)
//! - `weather_rain` (when weather is reported as "Rain" or "Drizzle" during the day)
//! - `weather_rain_night` (when weather is reported as "Rain" or "Drizzle" at night)
//! - `weather_snow` (when weather is reported as "Snow")
//! - `weather_default` (in all other cases)
//! - `weather_thunder` (when weather is reported as "Thunderstorm" during the day)
//! - `weather_thunder_night` (when weather is reported as "Thunderstorm" at night)

use std::fmt;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -134,22 +139,29 @@ pub enum WeatherService {

#[derive(Clone, Copy)]
enum WeatherIcon {
Sun,
Rain,
Clouds,
Thunder,
Clear { is_night: bool },
Clouds { is_night: bool },
Fog { is_night: bool },
Rain { is_night: bool },
Snow,
Thunder { is_night: bool },
Default,
}

impl WeatherIcon {
fn to_icon_str(self) -> &'static str {
match self {
Self::Sun => "weather_sun",
Self::Rain => "weather_rain",
Self::Clouds => "weather_clouds",
Self::Thunder => "weather_thunder",
Self::Clear { is_night: false } => "weather_sun",
Self::Clear { is_night: true } => "weather_moon",
Self::Clouds { is_night: false } => "weather_clouds",
Self::Clouds { is_night: true } => "weather_clouds_night",
Self::Fog { is_night: false } => "weather_fog",
Self::Fog { is_night: true } => "weather_fog_night",
Self::Rain { is_night: false } => "weather_rain",
Self::Rain { is_night: true } => "weather_rain_night",
Self::Snow => "weather_snow",
Self::Thunder { is_night: false } => "weather_thunder",
Self::Thunder { is_night: true } => "weather_thunder_night",
Self::Default => "weather_default",
}
}
Expand Down
31 changes: 20 additions & 11 deletions src/blocks/weather/met_no.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,21 @@ impl WeatherProvider for Service<'_> {

let instant = &first.instant.details;

let summary = first
let mut symbol_code_split = first
.next_1_hours
.as_ref()
.unwrap()
.summary
.symbol_code
.split('_')
.split('_');

let summary = symbol_code_split.next().unwrap();

// Times of day can be day, night, and polartwilight
let is_night = symbol_code_split
.next()
.unwrap();
.map_or(false, |time_of_day| time_of_day == "night");

let translated = translate(self.legend, summary, lang);

let temp = instant.air_temperature.unwrap_or_default();
Expand All @@ -168,17 +174,18 @@ impl WeatherProvider for Service<'_> {
wind: wind_speed,
wind_kmh: instant.wind_speed.unwrap_or_default() * 3.6,
wind_direction: convert_wind_direction(instant.wind_from_direction).into(),
icon: weather_to_icon(summary),
icon: weather_to_icon(summary, is_night),
})
}
}

fn weather_to_icon(weather: &str) -> WeatherIcon {
fn weather_to_icon(weather: &str, is_night: bool) -> WeatherIcon {
match weather {
"cloudy" | "partlycloudy" | "fair" | "fog" => WeatherIcon::Clouds,
"clearsky" => WeatherIcon::Sun,
"cloudy" | "partlycloudy" | "fair" => WeatherIcon::Clouds{is_night},
"fog" => WeatherIcon::Fog{is_night},
"clearsky" => WeatherIcon::Clear{is_night},
"heavyrain" | "heavyrainshowers" | "lightrain" | "lightrainshowers" | "rain"
| "rainshowers" => WeatherIcon::Rain,
| "rainshowers" => WeatherIcon::Rain{is_night},
"rainandthunder"
| "heavyrainandthunder"
| "rainshowersandthunder"
Expand All @@ -194,9 +201,11 @@ fn weather_to_icon(weather: &str) -> WeatherIcon {
| "lightsleetandthunder"
| "lightrainandthunder"
| "lightsnowandthunder"
| "lightssleetshowersandthunder"
| "lightssnowshowersandthunder"
| "lightrainshowersandthunder" => WeatherIcon::Thunder,
| "lightssleetshowersandthunder" // There's a typo in the api it will be fixed in the next version to the following entry
| "lightsleetshowersandthunder"
| "lightssnowshowersandthunder"// There's a typo in the api it will be fixed in the next version to the following entry
| "lightsnowshowersandthunder"
| "lightrainshowersandthunder" => WeatherIcon::Thunder{is_night},
"heavysleet" | "heavysleetshowers" | "heavysnow" | "heavysnowshowers" | "lightsleet"
| "lightsleetshowers" | "lightsnow" | "lightsnowshowers" | "sleet" | "sleetshowers"
| "snow" | "snowshowers" => WeatherIcon::Snow,
Expand Down
20 changes: 16 additions & 4 deletions src/blocks/weather/open_weather_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use chrono::Utc;

pub(super) const URL: &str = "https://api.openweathermap.org/data/2.5/weather";
pub(super) const API_KEY_ENV: &str = "OPENWEATHERMAP_API_KEY";
Expand Down Expand Up @@ -49,6 +50,7 @@ struct ApiResponse {
weather: Vec<ApiWeather>,
main: ApiMain,
wind: ApiWind,
sys: ApiSys,
name: String,
}

Expand All @@ -65,6 +67,12 @@ struct ApiMain {
humidity: f64,
}

#[derive(Deserialize, Debug)]
struct ApiSys {
sunrise: i64,
sunset: i64,
}

#[derive(Deserialize, Debug)]
struct ApiWeather {
main: String,
Expand Down Expand Up @@ -109,6 +117,9 @@ impl WeatherProvider for Service<'_> {
.await
.error("Forecast request failed")?;

let now = Utc::now().timestamp();
let is_night = data.sys.sunrise >= now || now >= data.sys.sunset;

Ok(WeatherResult {
location: data.name,
temp: data.main.temp,
Expand All @@ -124,10 +135,11 @@ impl WeatherProvider for Service<'_> {
},
wind_direction: convert_wind_direction(data.wind.deg).into(),
icon: match data.weather[0].main.as_str() {
"Clear" => WeatherIcon::Sun,
"Rain" | "Drizzle" => WeatherIcon::Rain,
"Clouds" | "Fog" | "Mist" => WeatherIcon::Clouds,
"Thunderstorm" => WeatherIcon::Thunder,
"Clear" => WeatherIcon::Clear { is_night },
"Rain" | "Drizzle" => WeatherIcon::Rain { is_night },
"Clouds" => WeatherIcon::Clouds { is_night },
"Fog" | "Mist" => WeatherIcon::Fog { is_night },
"Thunderstorm" => WeatherIcon::Thunder { is_night },
"Snow" => WeatherIcon::Snow,
_ => WeatherIcon::Default,
},
Expand Down
6 changes: 6 additions & 0 deletions src/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,17 @@ impl Default for Icons {
"volume_muted" => "VOL MUTED",
"microphone" => "MIC",
"microphone_muted" => "MIC MUTED",
"weather_clouds_night" => "CLOUDY",
"weather_clouds" => "CLOUDY",
"weather_default" => "WEATHER",
"weather_fog_night" => "FOG",
"weather_fog" => "FOG",
"weather_moon" => "MOONY",
"weather_rain_night" => "RAIN",
"weather_rain" => "RAIN",
"weather_snow" => "SNOW",
"weather_sun" => "SUNNY",
"weather_thunder_night" => "STORM",
"weather_thunder" => "STORM",
"xrandr" => "SCREEN"
})
Expand Down