8 Commits

Author SHA1 Message Date
f7ec98f8e3 0.2.3 2023-11-06 22:29:54 +01:00
924a14cdcb Tweak frontend layout and style 2023-11-06 21:27:27 +01:00
d5a221e1a3 0.2.2 2023-11-05 22:01:00 +01:00
b8e05e33a6 Remove websocket rate limit 2023-11-05 13:42:23 +01:00
362534384a 0.2.1 2023-11-05 13:28:37 +01:00
fee9c7a171 Fix clippy nags 2023-11-05 13:28:20 +01:00
e458c69c80 Do hacks with scale to stop overflowing 2023-11-05 13:15:14 +01:00
548e7d3240 Disable bulb controls when no bulbs selected 2023-11-05 12:49:22 +01:00
12 changed files with 134 additions and 71 deletions

8
Cargo.lock generated
View File

@ -206,7 +206,7 @@ dependencies = [
[[package]] [[package]]
name = "common" name = "common"
version = "0.2.0" version = "0.2.3"
dependencies = [ dependencies = [
"chrono", "chrono",
"lighter_lib", "lighter_lib",
@ -475,8 +475,6 @@ dependencies = [
"http", "http",
"js-sys", "js-sys",
"pin-project", "pin-project",
"serde",
"serde_json",
"thiserror", "thiserror",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
@ -578,7 +576,7 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "hemma" name = "hemma"
version = "0.2.0" version = "0.2.3"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@ -602,7 +600,7 @@ dependencies = [
[[package]] [[package]]
name = "hemma_web" name = "hemma_web"
version = "0.2.0" version = "0.2.3"
dependencies = [ dependencies = [
"chrono", "chrono",
"common", "common",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "hemma" name = "hemma"
version = "0.2.0" version = "0.2.3"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -144,7 +144,7 @@ fn wind_speed_to_beaufort(mps: f64) -> BeaufortScale {
let index = beaufort_wind_speeds let index = beaufort_wind_speeds
.iter() .iter()
.enumerate() .enumerate()
.find_map(|(i, range)| range.contains(&mps).then(|| i)) .find_map(|(i, range)| range.contains(&mps).then_some(i))
.unwrap_or(beaufort_wind_speeds.len()); .unwrap_or(beaufort_wind_speeds.len());
BeaufortScale(index as u8) BeaufortScale(index as u8)

View File

@ -32,7 +32,7 @@ struct Opt {
#[clap(short, long)] #[clap(short, long)]
quiet: bool, quiet: bool,
#[clap(long, short, default_value = "127.0.0.0:8000")] #[clap(long, short, default_value = "127.0.0.1:8000")]
bind: SocketAddr, bind: SocketAddr,
#[clap(long, default_value = "./www")] #[clap(long, default_value = "./www")]

View File

@ -53,7 +53,6 @@ pub async fn lights_task(state: &State) {
loop { loop {
let notify = bulb_states.notify_on_change(); let notify = bulb_states.notify_on_change();
sleep(tokio::time::Duration::from_millis(1000 / 10)).await; // limit to 10 updates/second
select! { select! {
_ = notify => { _ = notify => {
let lights_state = lights_state.get(); let lights_state = lights_state.get();
@ -112,20 +111,19 @@ pub async fn lights_task(state: &State) {
}; };
if let Some(time) = time { if let Some(time) = time {
let handle = spawn(wake_task( let handle = spawn(wake_task(
state.client_message.clone(), state.client_message.clone(),
cmd.clone(), cmd.clone(),
id.clone(), id.clone(),
day, day,
time, time,
)); ));
if let Some(old_handle) = wake_tasks.insert((id, day), handle) { if let Some(old_handle) = wake_tasks.insert((id, day), handle) {
old_handle.abort();
}} else {
if let Some(old_handle) = wake_tasks.remove(&(id, day)) {
old_handle.abort(); old_handle.abort();
} }
} else if let Some(old_handle) = wake_tasks.remove(&(id, day)) {
old_handle.abort();
} }
} }
_ => {} _ => {}
@ -181,7 +179,11 @@ fn next_alarm(now: DateTime<Local>, day: Weekday, time: NaiveTime) -> DateTime<L
let day_now = now.weekday().num_days_from_monday() as i64; let day_now = now.weekday().num_days_from_monday() as i64;
let alarm = now + chrono::Duration::days(day_of_alarm - day_now); let alarm = now + chrono::Duration::days(day_of_alarm - day_now);
let mut alarm = alarm.date().and_time(time).unwrap(); let mut alarm = alarm
.date_naive()
.and_time(time)
.and_local_timezone(Local)
.unwrap();
if alarm <= now { if alarm <= now {
alarm += chrono::Duration::weeks(1); alarm += chrono::Duration::weeks(1);

View File

@ -1,6 +1,6 @@
[package] [package]
name = "common" name = "common"
version = "0.2.0" version = "0.2.3"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -1,6 +1,6 @@
[package] [package]
name = "hemma_web" name = "hemma_web"
version = "0.2.0" version = "0.2.3"
authors = ["Joakim Hulthe <joakim@hulthe.net"] authors = ["Joakim Hulthe <joakim@hulthe.net"]
edition = "2021" edition = "2021"
@ -10,7 +10,7 @@ wasm-bindgen = "=0.2.87" # must match Trunk.toml
serde = { version = "1.0.0", features = ['derive'] } serde = { version = "1.0.0", features = ['derive'] }
ron = "0.7.1" ron = "0.7.1"
chrono = { version = "0.4.20", features = ["serde"] } chrono = { version = "0.4.20", features = ["serde"] }
gloo-net = "0.4.0" gloo-net = { version = "0.4.0", default-features = false, features = ["websocket"] }
gloo-console = "0.3.0" gloo-console = "0.3.0"
[dependencies.css_typegen] [dependencies.css_typegen]

View File

@ -7,7 +7,7 @@ wasm_bindgen = "0.2.87"
[[proxy]] [[proxy]]
# This WebSocket proxy example has a backend and ws field. This example will listen for # This WebSocket proxy example has a backend and ws field. This example will listen for
# WebSocket connections at `/api/ws` and proxy them to `ws://localhost:9000/api/ws`. # WebSocket connections at `/api/ws` and proxy them to `ws://localhost:9000/api/ws`.
backend = "ws://localhost:8000/api/ws" backend = "ws://127.0.0.1:8000/api/ws"
ws = true ws = true
#[[proxy]] #[[proxy]]

View File

@ -0,0 +1,4 @@
[toolchain]
channel = "stable"
components = ["rust-src", "rustfmt", "rust-src"]
targets = ["wasm32-unknown-unknown"]

View File

@ -19,7 +19,7 @@ pub struct ColorPicker {
#[derive(Default)] #[derive(Default)]
enum ColorPickerSetting { enum ColorPickerSetting {
#[default] #[default]
HSB, Hsb,
Kelvin, Kelvin,
} }
@ -55,14 +55,14 @@ impl ColorPicker {
} }
match self.dragging.take() { match self.dragging.take() {
Some(ColorPickerAttr::HueSat) => self.mode = ColorPickerSetting::HSB, Some(ColorPickerAttr::HueSat) => self.mode = ColorPickerSetting::Hsb,
Some(ColorPickerAttr::Brightness) => {} Some(ColorPickerAttr::Brightness) => {}
Some(ColorPickerAttr::Temperature) => self.mode = ColorPickerSetting::Kelvin, Some(ColorPickerAttr::Temperature) => self.mode = ColorPickerSetting::Kelvin,
None => return, None => return,
} }
let color = match self.mode { let color = match self.mode {
ColorPickerSetting::HSB => { ColorPickerSetting::Hsb => {
BulbColor::hsb(self.hue, self.saturation, self.brightness) BulbColor::hsb(self.hue, self.saturation, self.brightness)
} }
ColorPickerSetting::Kelvin => { ColorPickerSetting::Kelvin => {
@ -130,10 +130,12 @@ impl ColorPicker {
let (r, g, b) = hsb_to_rgb(self.hue, 1.0, 1.0); let (r, g, b) = hsb_to_rgb(self.hue, 1.0, 1.0);
let saturation_gradient = match self.mode { let saturation_gradient = match self.mode {
ColorPickerSetting::HSB => { ColorPickerSetting::Hsb => {
format!("background: linear-gradient(0deg, #000, rgba({r},{g},{b},1));") format!("background: linear-gradient(0deg, #000, rgba({r},{g},{b},1));")
} }
ColorPickerSetting::Kelvin => format!("background: linear-gradient(0deg, #000, #fff);"), ColorPickerSetting::Kelvin => {
"background: linear-gradient(0deg, #000, #fff);".to_string()
}
}; };
div![ div![
@ -186,7 +188,7 @@ impl ColorPicker {
self.hue = h; self.hue = h;
self.saturation = s; self.saturation = s;
self.brightness = b; self.brightness = b;
self.mode = ColorPickerSetting::HSB; self.mode = ColorPickerSetting::Hsb;
} }
pub fn set_kelvin(&mut self, t: f32, b: f32) { pub fn set_kelvin(&mut self, t: f32, b: f32) {

View File

@ -3,8 +3,8 @@ use crate::css::C;
use chrono::{NaiveTime, Weekday}; use chrono::{NaiveTime, Weekday};
use common::{BulbGroup, BulbGroupShape, BulbMap, ClientMessage, ServerMessage}; use common::{BulbGroup, BulbGroupShape, BulbMap, ClientMessage, ServerMessage};
use lighter_lib::{BulbId, BulbMode}; use lighter_lib::{BulbId, BulbMode};
use seed::prelude::*; use seed::{attrs, button, div, h2, input, table, td, tr, C};
use seed::{attrs, button, div, input, C}; use seed::{prelude::*, IF};
use seed_router::Page; use seed_router::Page;
use std::collections::{BTreeMap, HashMap, HashSet}; use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt::Write; use std::fmt::Write;
@ -112,7 +112,7 @@ impl Page for Model {
} }
Msg::ColorPicker(msg) => { Msg::ColorPicker(msg) => {
self.color_picker self.color_picker
.update(msg, &mut orders.proxy(|msg| Msg::ColorPicker(msg))); .update(msg, &mut orders.proxy(Msg::ColorPicker));
} }
Msg::SetBulbPower(power) => { Msg::SetBulbPower(power) => {
self.groups_interacted = true; self.groups_interacted = true;
@ -125,7 +125,7 @@ impl Page for Model {
}); });
} }
Msg::LightTime(time, day) => { Msg::LightTime(time, day) => {
if time == "" { if time.is_empty() {
self.for_selected_bulbs(|id, _| { self.for_selected_bulbs(|id, _| {
let message = ClientMessage::SetBulbWakeTime { let message = ClientMessage::SetBulbWakeTime {
id: id.clone(), id: id.clone(),
@ -228,24 +228,21 @@ impl Page for Model {
.next() .next()
.and_then(|&index| self.bulb_map.groups.get(index)) .and_then(|&index| self.bulb_map.groups.get(index))
.and_then(|group| group.bulbs.first()) .and_then(|group| group.bulbs.first())
.and_then(|id| self.bulb_states.get(id)) .and_then(|id| self.bulb_states.get(id));
.cloned() // TODO: remove clone
.unwrap_or_default();
let calendar_day = |day: Weekday| { let calendar_day = |day: Weekday| {
let time = selected_bulb let time = selected_bulb
.wake_schedule .and_then(|b| b.wake_schedule.get(&day))
.get(&day)
.map(|t| t.to_string()) .map(|t| t.to_string())
.unwrap_or_default(); .unwrap_or_default();
div![ tr![
C![C.calendar_day], C![C.calendar_day],
day.to_string(), td![day.to_string()],
input![ td![input![
C![C.calendar_time_input], C![C.calendar_time_input],
attrs! {At::Placeholder => time}, attrs! {At::Placeholder => time},
input_ev(Ev::Input, move |input| Msg::LightTime(input, day)) input_ev(Ev::Input, move |input| Msg::LightTime(input, day))
], ]],
] ]
}; };
@ -254,25 +251,25 @@ impl Page for Model {
div![ div![
C![C.bulb_map], C![C.bulb_map],
attrs! { attrs! {
At::Style => format!("width: {}rem; height: {}rem;", bulb_map_width, bulb_map_height), At::Style => format!("min-width: {}rem; height: {}rem;", bulb_map_width, bulb_map_height),
}, },
ev(Ev::Click, |_| Msg::DeselectGroups), ev(Ev::Click, |_| Msg::DeselectGroups),
self.bulb_map.groups.iter().enumerate().map(view_bulb_group), self.bulb_map.groups.iter().enumerate().map(view_bulb_group),
], ],
div![ div![
C![C.bulb_controls], C![C.bulb_controls],
self.color_picker IF!(selected_bulb.is_none() => C![C.cross_out]),
.view() self.color_picker.view().map_msg(Msg::ColorPicker),
.map_msg(|msg| Msg::ColorPicker(msg)),
button![ button![
if selected_bulb.mode.power { if selected_bulb.map(|b| b.mode.power).unwrap_or(false) {
C![C.bulb_power_button, C.bulb_power_button_on] C![C.bulb_power_button, C.bulb_power_button_on]
} else { } else {
C![C.bulb_power_button] C![C.bulb_power_button]
}, },
ev(Ev::Click, move |_| Msg::SetBulbPower( {
!selected_bulb.mode.power let target_power = selected_bulb.map(|b| !b.mode.power);
)), ev(Ev::Click, move |_| target_power.map(Msg::SetBulbPower))
},
div![attrs! { At::Id => "switch_socket" }], div![attrs! { At::Id => "switch_socket" }],
div![attrs! { At::Id => "off_label" }, "Off"], div![attrs! { At::Id => "off_label" }, "Off"],
div![attrs! { At::Id => "on_label" }, "On"], div![attrs! { At::Id => "on_label" }, "On"],
@ -282,13 +279,17 @@ impl Page for Model {
], ],
div![ div![
C![C.calendar_box], C![C.calendar_box],
calendar_day(Weekday::Mon), IF!(selected_bulb.is_none() => C![C.cross_out]),
calendar_day(Weekday::Tue), h2!["Wake Schedule"],
calendar_day(Weekday::Wed), table![
calendar_day(Weekday::Thu), calendar_day(Weekday::Mon),
calendar_day(Weekday::Fri), calendar_day(Weekday::Tue),
calendar_day(Weekday::Sat), calendar_day(Weekday::Wed),
calendar_day(Weekday::Sun), calendar_day(Weekday::Thu),
calendar_day(Weekday::Fri),
calendar_day(Weekday::Sat),
calendar_day(Weekday::Sun),
],
], ],
] ]
} }

View File

@ -19,6 +19,16 @@ body {
font-size: x-large; font-size: x-large;
} }
/* filthy hacks to make the page stop overflowing on mobile */
@media (width <= 430px) {
body {
margin: 0;
transform-origin: top left;
scale: 0.9;
}
}
@media (width <= 390px) { body { scale: 0.85; } }
.info_box { .info_box {
margin: auto; margin: auto;
max-width: 40em; max-width: 40em;
@ -84,16 +94,12 @@ body {
} }
.bulb_box { .bulb_box {
display: flex;
flex-direction: row;
flex-wrap: wrap;
align-items: center;
justify-content: space-around;
width: fit-content; width: fit-content;
margin: auto;
} }
.bulb_box > * { .bulb_box > * {
margin: 0.5em; margin-bottom: 1.5rem;
} }
.bulb_controls { .bulb_controls {
@ -101,24 +107,62 @@ body {
flex-direction: row; flex-direction: row;
align-items: center; align-items: center;
padding-right: 0.5em; padding-right: 0.5em;
background: #56636e; background: #00000000;
border: solid 0.25em #5b3f63;
} }
.cross_out {
position: relative;
overflow: hidden;
opacity: 0.5;
}
.cross_out:before {
position: absolute;
content: '';
display: block;
background: #00000000;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
z-index: 90;
}
.cross_out:after {
position: absolute;
content: '';
background: #56636e;
display: block;
width: 0;
height: 10px;
border-radius: 8px;
-webkit-transform: rotate(-30deg);
transform: rotate(-30deg);
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
z-index: 91;
box-shadow: black 0.5rem 0.6rem 1rem 0.3rem;
animation: to_width_90 .2s ease-out 0s 1 forwards;
}
@keyframes to_width_90 { to { width: 90%; } }
.bulb_map { .bulb_map {
background: url(images/blueprint_bg.png); background: url(images/blueprint_bg.png);
background-size: auto; background-size: auto;
background-size: 1em; background-size: 1em;
padding: 2rem; padding: 2rem;
border: solid 0.4rem #5b3f63; border: solid 0.1rem;
border-radius: 0.2rem;
} }
.bulb_map::after { .bulb_map::after {
position: absolute; position: absolute;
width: 100%; width: 100%;
height: 100%; height: 100%;
content: #0003;
} }
.bulb_group { .bulb_group {
@ -130,6 +174,7 @@ body {
font-size: 2em; font-size: 2em;
position: absolute; position: absolute;
box-shadow: #0006 0.1em 0.1em 0.1em; box-shadow: #0006 0.1em 0.1em 0.1em;
user-select: none;
transition: 0.3s ease-in-out; transition: 0.3s ease-in-out;
} }
@ -277,7 +322,18 @@ body {
//margin-bottom: .7em; //margin-bottom: .7em;
margin-left: .5em; margin-left: .5em;
} }
.calendar_box { .calendar_box {
with: 10em; display: flex;
justify-content: center;
}
.calendar_box > h2 {
writing-mode: sideways-lr;
margin-top: auto;
margin-bottom: auto;
}
.calendar_box > * {
flex-shrink: 1;
} }