Compare commits
10 Commits
01a7576d7f
...
bulb_list
| Author | SHA1 | Date | |
|---|---|---|---|
| 071e375cc2 | |||
|
924a14cdcb
|
|||
|
d5a221e1a3
|
|||
|
b8e05e33a6
|
|||
|
362534384a
|
|||
|
fee9c7a171
|
|||
|
e458c69c80
|
|||
|
548e7d3240
|
|||
|
ac306eece4
|
|||
|
4cf6b628ee
|
1015
Cargo.lock
generated
1015
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,8 @@
|
||||
[workspace]
|
||||
members = ["backend", "common", "frontend"]
|
||||
|
||||
resolver = "2"
|
||||
|
||||
[profile.dev]
|
||||
# Issue with const-generics
|
||||
incremental = false
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
##################
|
||||
### BASE STAGE ###
|
||||
##################
|
||||
FROM rust:1.62.1 as base
|
||||
FROM rust:1.72.1 as base
|
||||
|
||||
# Install build dependencies
|
||||
RUN cargo install --locked cargo-make trunk strip_cargo_version
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "hemma"
|
||||
version = "0.2.0"
|
||||
version = "0.2.2"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@ -144,7 +144,7 @@ fn wind_speed_to_beaufort(mps: f64) -> BeaufortScale {
|
||||
let index = beaufort_wind_speeds
|
||||
.iter()
|
||||
.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());
|
||||
|
||||
BeaufortScale(index as u8)
|
||||
|
||||
@ -32,7 +32,7 @@ struct Opt {
|
||||
#[clap(short, long)]
|
||||
quiet: bool,
|
||||
|
||||
#[clap(long, short, default_value = "127.0.0.0:8000")]
|
||||
#[clap(long, short, default_value = "127.0.0.1:8000")]
|
||||
bind: SocketAddr,
|
||||
|
||||
#[clap(long, default_value = "./www")]
|
||||
|
||||
@ -53,7 +53,6 @@ pub async fn lights_task(state: &State) {
|
||||
|
||||
loop {
|
||||
let notify = bulb_states.notify_on_change();
|
||||
sleep(tokio::time::Duration::from_millis(1000 / 10)).await; // limit to 10 updates/second
|
||||
select! {
|
||||
_ = notify => {
|
||||
let lights_state = lights_state.get();
|
||||
@ -122,10 +121,9 @@ pub async fn lights_task(state: &State) {
|
||||
|
||||
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();
|
||||
}
|
||||
} 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 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 {
|
||||
alarm += chrono::Duration::weeks(1);
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "common"
|
||||
version = "0.2.0"
|
||||
version = "0.2.2"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
@ -1,20 +1,17 @@
|
||||
[package]
|
||||
name = "hemma_web"
|
||||
version = "0.2.0"
|
||||
version = "0.2.2"
|
||||
authors = ["Joakim Hulthe <joakim@hulthe.net"]
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
seed = "0.9.1"
|
||||
wasm-bindgen = "=0.2.80" # 0.2.81 has a breaking change
|
||||
serde = { version = "1", features = ['derive'] }
|
||||
serde_json = "1"
|
||||
anyhow = "*"
|
||||
seed = "0.10.0"
|
||||
wasm-bindgen = "=0.2.87" # must match Trunk.toml
|
||||
serde = { version = "1.0.0", features = ['derive'] }
|
||||
ron = "0.7.1"
|
||||
chrono = { version = "0.4.20", features = ["serde"] }
|
||||
gloo-net = { version = "0.4.0", default-features = false, features = ["websocket"] }
|
||||
gloo-console = "0.3.0"
|
||||
|
||||
[dependencies.css_typegen]
|
||||
git = "https://github.com/hulthe/css_typegen.git"
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
[serve]
|
||||
address = "0.0.0.0"
|
||||
|
||||
|
||||
[tools]
|
||||
wasm_bindgen = "0.2.87"
|
||||
|
||||
[[proxy]]
|
||||
# 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`.
|
||||
backend = "ws://localhost:8000/api/ws"
|
||||
backend = "ws://127.0.0.1:8000/api/ws"
|
||||
ws = true
|
||||
|
||||
#[[proxy]]
|
||||
|
||||
4
frontend/rust-toolchain.toml
Normal file
4
frontend/rust-toolchain.toml
Normal file
@ -0,0 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "stable"
|
||||
components = ["rust-src", "rustfmt", "rust-src"]
|
||||
targets = ["wasm32-unknown-unknown"]
|
||||
@ -1,10 +1,20 @@
|
||||
use crate::page;
|
||||
use common::{ClientMessage, ServerMessage};
|
||||
use gloo_console::error;
|
||||
use gloo_net::websocket;
|
||||
use gloo_net::websocket::futures::WebSocket;
|
||||
use seed::app::orders::OrdersContainer;
|
||||
use seed::futures::channel::mpsc::channel;
|
||||
use seed::futures::channel::mpsc::Sender;
|
||||
use seed::futures::select_biased;
|
||||
use seed::futures::SinkExt;
|
||||
use seed::futures::StreamExt;
|
||||
use seed::prelude::*;
|
||||
use seed::{log, window};
|
||||
use seed::window;
|
||||
use seed::FutureExt;
|
||||
use seed_router::Router;
|
||||
use std::collections::VecDeque;
|
||||
use std::error::Error;
|
||||
|
||||
pub type AppOrders = OrdersContainer<Msg, Model, Vec<Node<Msg>>>;
|
||||
|
||||
@ -14,8 +24,16 @@ const TIMEOUT_CONNECT_DELAYS: &[u32] = &[2, 5, 10, 10, 10, 20, 30, 60, 120, 300]
|
||||
pub struct Model {
|
||||
page: Pages,
|
||||
send_queue: VecDeque<ClientMessage>,
|
||||
socket: WebSocket,
|
||||
|
||||
/// Websocket URL
|
||||
ws_url: String,
|
||||
|
||||
/// Channel to send websocket messages.
|
||||
socket: Option<Sender<websocket::Message>>,
|
||||
|
||||
/// Handle to the websocket task.
|
||||
socket_task: Option<CmdHandle>,
|
||||
|
||||
timeout_count: usize,
|
||||
}
|
||||
|
||||
@ -47,14 +65,14 @@ pub enum Msg {
|
||||
|
||||
// Global
|
||||
Connect,
|
||||
SocketOpened(),
|
||||
SocketClosed(CloseEvent),
|
||||
SocketError(),
|
||||
SocketMessage(WebSocketMessage),
|
||||
SocketOpened(Sender<websocket::Message>),
|
||||
SocketClosed,
|
||||
SocketMessage(websocket::Message),
|
||||
}
|
||||
|
||||
pub fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
|
||||
orders.subscribe(Msg::SendMessage);
|
||||
orders.send_msg(Msg::Connect);
|
||||
|
||||
let location = window().location();
|
||||
let host = location.host().expect("Failed to get hostname");
|
||||
@ -69,73 +87,109 @@ pub fn init(url: Url, orders: &mut impl Orders<Msg>) -> Model {
|
||||
page: Pages::from_url(url, &mut orders.proxy(Msg::Page))
|
||||
.unwrap_or(Pages::NotFound(Default::default())),
|
||||
send_queue: Default::default(),
|
||||
socket: open_socket(&ws_url, orders),
|
||||
ws_url,
|
||||
socket_task: None,
|
||||
socket: None,
|
||||
timeout_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn open_socket(url: &str, orders: &mut impl Orders<Msg>) -> WebSocket {
|
||||
WebSocket::builder(url, orders)
|
||||
.on_open(Msg::SocketOpened)
|
||||
.on_close(Msg::SocketClosed)
|
||||
.on_error(Msg::SocketError)
|
||||
.on_message(Msg::SocketMessage)
|
||||
.build_and_open()
|
||||
.expect("failed to open websocket")
|
||||
}
|
||||
fn open_socket(url: String, orders: &mut impl Orders<Msg>) -> CmdHandle {
|
||||
let update_fn = orders.msg_sender();
|
||||
|
||||
pub fn update(msg: Msg, model: &mut Model, orders: &mut AppOrders) {
|
||||
#[cfg(debug_assertions)]
|
||||
log!(format!("{msg:?}"));
|
||||
orders.perform_cmd_with_handle(async move {
|
||||
let mut ws = WebSocket::open(&url).expect("Failed to open websocket");
|
||||
let (tx, mut rx) = channel(128);
|
||||
update_fn(Some(Msg::SocketOpened(tx)));
|
||||
|
||||
match msg {
|
||||
Msg::Page(msg) => model.page.update(msg, &mut orders.proxy(Msg::Page)),
|
||||
Msg::FlushMessageQueue => {
|
||||
while let Some(message) = model.send_queue.pop_front() {
|
||||
let serialized = ron::to_string(&message).unwrap();
|
||||
|
||||
if let Err(e) = model.socket.send_text(serialized) {
|
||||
model.send_queue.push_front(message);
|
||||
log!(e);
|
||||
loop {
|
||||
select_biased! {
|
||||
message = rx.next().fuse() => {
|
||||
let Some(message) = message else { return; };
|
||||
if let Err(e) = ws.send(message).await {
|
||||
error!(format!("websocket error: {e:?}"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
message = ws.next().fuse() => match message {
|
||||
Some(Ok(message)) => update_fn(Some(Msg::SocketMessage(message))),
|
||||
Some(Err(e)) => {
|
||||
error!(format!("websocket error: {e:?}"));
|
||||
update_fn(Some(Msg::SocketClosed));
|
||||
return;
|
||||
}
|
||||
None => {
|
||||
update_fn(Some(Msg::SocketClosed));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn update(msg: Msg, model: &mut Model, orders: &mut AppOrders) {
|
||||
#[cfg(debug_assertions)]
|
||||
gloo_console::debug!(format!("{msg:?}"));
|
||||
|
||||
match msg {
|
||||
Msg::Page(msg) => model.page.update(msg, &mut orders.proxy(Msg::Page)),
|
||||
Msg::FlushMessageQueue => {
|
||||
if let Some(socket) = model.socket.as_mut() {
|
||||
while let Some(message) = model.send_queue.pop_front() {
|
||||
let serialized = ron::to_string(&message).expect("failed to serialize ron");
|
||||
|
||||
let ws_message = websocket::Message::Text(serialized);
|
||||
if socket.try_send(ws_message).is_err() {
|
||||
error!("websocket queue full");
|
||||
model.send_queue.push_front(message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Msg::Connect => {
|
||||
model.socket = open_socket(&model.ws_url, orders);
|
||||
model.socket_task = Some(open_socket(model.ws_url.clone(), orders));
|
||||
}
|
||||
Msg::SendMessage(message) => {
|
||||
model.send_queue.push_back(message);
|
||||
orders.send_msg(Msg::FlushMessageQueue);
|
||||
}
|
||||
Msg::SocketOpened() => {
|
||||
Msg::SocketOpened(socket) => {
|
||||
model.socket = Some(socket);
|
||||
model.timeout_count = 0;
|
||||
orders.send_msg(Msg::FlushMessageQueue);
|
||||
}
|
||||
Msg::SocketClosed(_event) => {
|
||||
Msg::SocketClosed => {
|
||||
model.socket_task = None;
|
||||
model.socket = None;
|
||||
|
||||
let timeout_sec = TIMEOUT_CONNECT_DELAYS[model.timeout_count];
|
||||
let timeout_ms = timeout_sec * 1000;
|
||||
orders.perform_cmd(cmds::timeout(timeout_ms, || Msg::Connect));
|
||||
|
||||
log!(format!(
|
||||
error!(format!(
|
||||
"Socket closed, trying to reconnect in {timeout_sec} seconds"
|
||||
));
|
||||
|
||||
model.timeout_count = TIMEOUT_CONNECT_DELAYS.len().min(model.timeout_count + 1);
|
||||
}
|
||||
Msg::SocketError() => {}
|
||||
Msg::SocketMessage(message) => {
|
||||
if let Err(e) = handle_ws_msg(message, orders) {
|
||||
log!(e);
|
||||
error!(format!("{e:?}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_ws_msg(message: WebSocketMessage, orders: &mut impl Orders<Msg>) -> anyhow::Result<()> {
|
||||
let message = message.text().map_err(|e| anyhow::format_err!("{e:?}"))?;
|
||||
let message: ServerMessage = ron::from_str(&message)?;
|
||||
fn handle_ws_msg(
|
||||
message: websocket::Message,
|
||||
orders: &mut impl Orders<Msg>,
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
let websocket::Message::Text(text) = &message else {
|
||||
return Err("Server is sending us raw bytes on the websocket! Argh!".into());
|
||||
};
|
||||
let message: ServerMessage = ron::from_str(text)?;
|
||||
orders.notify(message);
|
||||
|
||||
Ok(())
|
||||
@ -143,9 +197,4 @@ fn handle_ws_msg(message: WebSocketMessage, orders: &mut impl Orders<Msg>) -> an
|
||||
|
||||
pub fn view(model: &Model) -> Vec<Node<Msg>> {
|
||||
vec![model.page.view().map_msg(Msg::Page)]
|
||||
//match &model.page {
|
||||
// Pages::NotFound => vec![h1!["Not Found"]],
|
||||
// Pages::InfoScreen => vec![div![C![C.info_box], raw![&model.info_page]]],
|
||||
// Pages::Lights(page) => vec![],
|
||||
//}
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ pub struct ColorPicker {
|
||||
#[derive(Default)]
|
||||
enum ColorPickerSetting {
|
||||
#[default]
|
||||
HSB,
|
||||
Hsb,
|
||||
Kelvin,
|
||||
}
|
||||
|
||||
@ -55,14 +55,14 @@ impl ColorPicker {
|
||||
}
|
||||
|
||||
match self.dragging.take() {
|
||||
Some(ColorPickerAttr::HueSat) => self.mode = ColorPickerSetting::HSB,
|
||||
Some(ColorPickerAttr::HueSat) => self.mode = ColorPickerSetting::Hsb,
|
||||
Some(ColorPickerAttr::Brightness) => {}
|
||||
Some(ColorPickerAttr::Temperature) => self.mode = ColorPickerSetting::Kelvin,
|
||||
None => return,
|
||||
}
|
||||
|
||||
let color = match self.mode {
|
||||
ColorPickerSetting::HSB => {
|
||||
ColorPickerSetting::Hsb => {
|
||||
BulbColor::hsb(self.hue, self.saturation, self.brightness)
|
||||
}
|
||||
ColorPickerSetting::Kelvin => {
|
||||
@ -130,10 +130,12 @@ impl ColorPicker {
|
||||
|
||||
let (r, g, b) = hsb_to_rgb(self.hue, 1.0, 1.0);
|
||||
let saturation_gradient = match self.mode {
|
||||
ColorPickerSetting::HSB => {
|
||||
ColorPickerSetting::Hsb => {
|
||||
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![
|
||||
@ -186,7 +188,7 @@ impl ColorPicker {
|
||||
self.hue = h;
|
||||
self.saturation = s;
|
||||
self.brightness = b;
|
||||
self.mode = ColorPickerSetting::HSB;
|
||||
self.mode = ColorPickerSetting::Hsb;
|
||||
}
|
||||
|
||||
pub fn set_kelvin(&mut self, t: f32, b: f32) {
|
||||
|
||||
@ -3,10 +3,8 @@ mod components;
|
||||
mod css;
|
||||
mod page;
|
||||
|
||||
use seed::prelude::wasm_bindgen;
|
||||
use seed::App;
|
||||
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn start() {
|
||||
pub fn main() {
|
||||
App::start("app", app::init, app::update, app::view);
|
||||
}
|
||||
@ -3,10 +3,10 @@ use crate::css::C;
|
||||
use chrono::{NaiveTime, Weekday};
|
||||
use common::{BulbGroup, BulbGroupShape, BulbMap, ClientMessage, ServerMessage};
|
||||
use lighter_lib::{BulbId, BulbMode};
|
||||
use seed::prelude::*;
|
||||
use seed::{attrs, button, div, input, C};
|
||||
use seed::{attrs, button, div, h1, h2, h3, input, label, span, table, td, tr, C};
|
||||
use seed::{prelude::*, IF};
|
||||
use seed_router::Page;
|
||||
use std::collections::{BTreeMap, HashSet, HashMap};
|
||||
use std::collections::{BTreeMap, HashMap, HashSet};
|
||||
use std::fmt::Write;
|
||||
|
||||
/// /lights page
|
||||
@ -14,16 +14,22 @@ use std::fmt::Write;
|
||||
pub struct Model {
|
||||
bulb_states: BTreeMap<BulbId, BulbState>,
|
||||
|
||||
select_mode: SelectMode,
|
||||
|
||||
bulb_groups: BTreeMap<String, Vec<BulbId>>,
|
||||
|
||||
bulb_map: BulbMap,
|
||||
|
||||
/// the currently selected bulb map groups
|
||||
/// the currently selected bulbs on the map
|
||||
selected_groups: HashSet<usize>,
|
||||
|
||||
/// the currently selected bulbs on the list
|
||||
selected_bulbs: HashSet<BulbId>,
|
||||
|
||||
/// whether the currently selected map groups have been interacted with
|
||||
groups_interacted: bool,
|
||||
|
||||
color_picker: ColorPicker,
|
||||
|
||||
}
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
@ -38,9 +44,20 @@ pub enum Msg {
|
||||
|
||||
SelectGroup(usize),
|
||||
DeselectGroups,
|
||||
|
||||
SelectBulb(BulbId),
|
||||
|
||||
ColorPicker(ColorPickerMsg),
|
||||
SetBulbPower(bool),
|
||||
LightTime(String, Weekday),
|
||||
SetSelectMode(SelectMode),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub enum SelectMode {
|
||||
#[default]
|
||||
Map,
|
||||
List,
|
||||
}
|
||||
|
||||
impl Page for Model {
|
||||
@ -56,13 +73,15 @@ impl Page for Model {
|
||||
fn update(&mut self, msg: Self::Msg, orders: &mut impl Orders<Self::Msg>) {
|
||||
match msg {
|
||||
Msg::ServerMessage(msg) => match msg {
|
||||
ServerMessage::BulbState { id, mode: new_mode, wake_schedule } => {
|
||||
ServerMessage::BulbState {
|
||||
id,
|
||||
mode: new_mode,
|
||||
wake_schedule,
|
||||
} => {
|
||||
*self.bulb_states.entry(id).or_default() = BulbState {
|
||||
mode: new_mode,
|
||||
wake_schedule,
|
||||
};
|
||||
|
||||
//color_picker.set_color(mode.color);
|
||||
}
|
||||
ServerMessage::BulbMap(bulb_map) => {
|
||||
self.bulb_map = bulb_map;
|
||||
@ -97,6 +116,11 @@ impl Page for Model {
|
||||
}
|
||||
}
|
||||
}
|
||||
Msg::SelectBulb(bulb) => {
|
||||
if !self.selected_bulbs.remove(&bulb) {
|
||||
self.selected_bulbs.insert(bulb);
|
||||
}
|
||||
}
|
||||
Msg::ColorPicker(ColorPickerMsg::SetColor(color)) => {
|
||||
self.groups_interacted = true;
|
||||
self.for_selected_bulbs(|id, _| {
|
||||
@ -109,7 +133,7 @@ impl Page for Model {
|
||||
}
|
||||
Msg::ColorPicker(msg) => {
|
||||
self.color_picker
|
||||
.update(msg, &mut orders.proxy(|msg| Msg::ColorPicker(msg)));
|
||||
.update(msg, &mut orders.proxy(Msg::ColorPicker));
|
||||
}
|
||||
Msg::SetBulbPower(power) => {
|
||||
self.groups_interacted = true;
|
||||
@ -122,7 +146,7 @@ impl Page for Model {
|
||||
});
|
||||
}
|
||||
Msg::LightTime(time, day) => {
|
||||
if time == "" {
|
||||
if time.is_empty() {
|
||||
self.for_selected_bulbs(|id, _| {
|
||||
let message = ClientMessage::SetBulbWakeTime {
|
||||
id: id.clone(),
|
||||
@ -131,8 +155,7 @@ impl Page for Model {
|
||||
};
|
||||
orders.notify(message);
|
||||
});
|
||||
}
|
||||
else if let Ok(time) = NaiveTime::parse_from_str(&time, "%H:%M") {
|
||||
} else if let Ok(time) = NaiveTime::parse_from_str(&time, "%H:%M") {
|
||||
self.for_selected_bulbs(|id, _| {
|
||||
let message = ClientMessage::SetBulbWakeTime {
|
||||
id: id.clone(),
|
||||
@ -143,37 +166,13 @@ impl Page for Model {
|
||||
});
|
||||
}
|
||||
}
|
||||
Msg::SetSelectMode(mode) => {
|
||||
self.select_mode = mode;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn view(&self) -> Node<Self::Msg> {
|
||||
//let view_bulb = |(id, (mode, color_picker)): (&BulbId, &(BulbMode, ColorPicker))| {
|
||||
// div![
|
||||
// C![C.bulb_box],
|
||||
// h1![id],
|
||||
// div![
|
||||
// C![C.bulb_controls],
|
||||
// {
|
||||
// let id = id.clone();
|
||||
// color_picker.view().map_msg(|msg| Msg::ColorPicker(id, msg))
|
||||
// },
|
||||
// button![
|
||||
// if mode.power {
|
||||
// C![C.bulb_power_button, C.bulb_power_button_on]
|
||||
// } else {
|
||||
// C![C.bulb_power_button]
|
||||
// },
|
||||
// {
|
||||
// let id = id.clone();
|
||||
// let power = !mode.power;
|
||||
// ev(Ev::Click, move |_| Msg::SetBulbPower(id, power))
|
||||
// },
|
||||
// ],
|
||||
// ],
|
||||
// ]
|
||||
//};
|
||||
|
||||
fn view(&self) -> Node<Self::Msg> {
|
||||
let bulb_map_width = self
|
||||
.bulb_map
|
||||
.groups
|
||||
@ -190,7 +189,7 @@ fn view(&self) -> Node<Self::Msg> {
|
||||
.max()
|
||||
.unwrap_or(0);
|
||||
|
||||
let view_bulb_group = |(i, group): (usize, &BulbGroup)| {
|
||||
let bulb_group_map = |(i, group): (usize, &BulbGroup)| {
|
||||
let (w, h) = (group.shape.width(), group.shape.height());
|
||||
let mut style = String::new();
|
||||
write!(
|
||||
@ -221,52 +220,114 @@ fn view(&self) -> Node<Self::Msg> {
|
||||
]
|
||||
};
|
||||
|
||||
let selected_bulb = self
|
||||
.selected_groups
|
||||
let bulb_group_list = |(_i, group): (usize, &BulbGroup)| {
|
||||
div![
|
||||
C![C.bulb_list_group],
|
||||
h1![&group.name],
|
||||
group.bulbs.iter().map(|bulb| {
|
||||
let select_ev = || {
|
||||
let bulb = bulb.clone();
|
||||
ev(Ev::Click, |_| Msg::SelectBulb(bulb))
|
||||
};
|
||||
|
||||
button![
|
||||
C![C.bulb_list_bulb],
|
||||
select_ev(),
|
||||
div![
|
||||
C![C.bulb_list_checkbox],
|
||||
label![
|
||||
C![C.container],
|
||||
input![
|
||||
attrs! { At::Type => "checkbox" },
|
||||
IF!(self.selected_bulbs.contains(bulb) => attrs! { At::Checked => true }),
|
||||
select_ev(),
|
||||
],
|
||||
div![C![C.checkmark]],
|
||||
],
|
||||
],
|
||||
span![bulb],
|
||||
]
|
||||
}),
|
||||
]
|
||||
};
|
||||
|
||||
// pick one (arbitrary) selected bulb to pull values for the controls from
|
||||
let selected_bulb = if let SelectMode::Map = self.select_mode {
|
||||
self.selected_groups
|
||||
.iter()
|
||||
.next()
|
||||
.and_then(|&index| self.bulb_map.groups.get(index))
|
||||
.and_then(|group| group.bulbs.first())
|
||||
.and_then(|id| self.bulb_states.get(id))
|
||||
.cloned() // TODO: remove clone
|
||||
.unwrap_or_default();
|
||||
} else {
|
||||
self.selected_bulbs
|
||||
.iter()
|
||||
.next()
|
||||
.and_then(|id| self.bulb_states.get(id))
|
||||
};
|
||||
|
||||
let calendar_day = |day: Weekday| {
|
||||
let time = selected_bulb.wake_schedule.get(&day).map(|t| t.to_string()).unwrap_or_default();
|
||||
div![
|
||||
let time = selected_bulb
|
||||
.and_then(|b| b.wake_schedule.get(&day))
|
||||
.map(|t| t.to_string())
|
||||
.unwrap_or_default();
|
||||
tr![
|
||||
C![C.calendar_day],
|
||||
day.to_string(),
|
||||
input![
|
||||
td![day.to_string()],
|
||||
td![input![
|
||||
C![C.calendar_time_input],
|
||||
attrs! {At::Placeholder => time},
|
||||
input_ev(Ev::Input, move |input| Msg::LightTime(input, day))
|
||||
],
|
||||
]],
|
||||
]
|
||||
};
|
||||
|
||||
|
||||
div![
|
||||
C![C.bulb_box],
|
||||
div![
|
||||
C![C.bulb_map],
|
||||
attrs! {
|
||||
At::Style => format!("width: {}rem; height: {}rem;", bulb_map_width, bulb_map_height),
|
||||
},
|
||||
ev(Ev::Click, |_| Msg::DeselectGroups),
|
||||
self.bulb_map.groups.iter().enumerate().map(view_bulb_group),
|
||||
C![C.selector_selector],
|
||||
button![
|
||||
"Map",
|
||||
ev(Ev::Click, move |_| Msg::SetSelectMode(SelectMode::Map))
|
||||
],
|
||||
div![
|
||||
C![C.bulb_controls],
|
||||
self.color_picker
|
||||
.view()
|
||||
.map_msg(|msg| Msg::ColorPicker(msg)),
|
||||
C![C.selector_selector_arrow],
|
||||
IF!(self.select_mode == SelectMode::Map =>
|
||||
attrs! { At::Style => "transform: rotateY(180deg);"}),
|
||||
],
|
||||
button![
|
||||
if selected_bulb.mode.power {
|
||||
"List",
|
||||
ev(Ev::Click, move |_| Msg::SetSelectMode(SelectMode::List))
|
||||
],
|
||||
],
|
||||
match self.select_mode {
|
||||
SelectMode::Map => div![
|
||||
C![C.bulb_map],
|
||||
attrs! {
|
||||
At::Style => format!("min-width: {}rem; height: {}rem;", bulb_map_width, bulb_map_height),
|
||||
},
|
||||
ev(Ev::Click, |_| Msg::DeselectGroups),
|
||||
self.bulb_map.groups.iter().enumerate().map(bulb_group_map),
|
||||
],
|
||||
SelectMode::List => div![
|
||||
C![C.bulb_list],
|
||||
self.bulb_map.groups.iter().enumerate().map(bulb_group_list),
|
||||
],
|
||||
},
|
||||
div![
|
||||
C![C.bulb_controls],
|
||||
IF!(selected_bulb.is_none() => C![C.cross_out]),
|
||||
self.color_picker.view().map_msg(Msg::ColorPicker),
|
||||
button![
|
||||
if selected_bulb.map(|b| b.mode.power).unwrap_or(false) {
|
||||
C![C.bulb_power_button, C.bulb_power_button_on]
|
||||
} else {
|
||||
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 => "off_label" }, "Off"],
|
||||
div![attrs! { At::Id => "on_label" }, "On"],
|
||||
@ -276,6 +337,9 @@ fn view(&self) -> Node<Self::Msg> {
|
||||
],
|
||||
div![
|
||||
C![C.calendar_box],
|
||||
IF!(selected_bulb.is_none() => C![C.cross_out]),
|
||||
h2!["Wake Schedule"],
|
||||
table![
|
||||
calendar_day(Weekday::Mon),
|
||||
calendar_day(Weekday::Tue),
|
||||
calendar_day(Weekday::Wed),
|
||||
@ -284,17 +348,25 @@ fn view(&self) -> Node<Self::Msg> {
|
||||
calendar_day(Weekday::Sat),
|
||||
calendar_day(Weekday::Sun),
|
||||
],
|
||||
],
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Model {
|
||||
fn for_selected_bulbs(&self, mut f: impl FnMut(&BulbId, &BulbState)) {
|
||||
if let SelectMode::Map = self.select_mode {
|
||||
self.selected_groups
|
||||
.iter()
|
||||
.filter_map(|&index| self.bulb_map.groups.get(index))
|
||||
.flat_map(|group| group.bulbs.iter())
|
||||
.filter_map(|id| self.bulb_states.get(id).map(|bulb| (id, bulb)))
|
||||
.for_each(|(id, bulb)| f(id, bulb));
|
||||
} else {
|
||||
self.selected_bulbs
|
||||
.iter()
|
||||
.filter_map(|id| self.bulb_states.get(id).map(|bulb| (id, bulb)))
|
||||
.for_each(|(id, bulb)| f(id, bulb));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
5
frontend/static/images/circle-arrow.svg
Normal file
5
frontend/static/images/circle-arrow.svg
Normal file
@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg" xmlns:bx="https://boxy-svg.com">
|
||||
<ellipse style="vector-effect: non-scaling-stroke; stroke-width: 0px; stroke: rgb(245, 222, 179); fill: rgb(245, 222, 179);" cx="250" cy="250" rx="250" ry="250"/>
|
||||
<path d="M 59.4 209.201 H 308.253 L 274.16 131.459 L 447.051 254.953 L 274.16 378.446 L 308.253 300.704 H 59.4 V 209.201 Z" style="transform-origin: 447.051px 254.952px; stroke-width: 16px; stroke: rgb(193, 168, 123);" bx:shape="arrow 59.4 131.459 387.651 246.987 91.503 172.891 34.093 1@d4e47a6f"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 607 B |
@ -19,6 +19,16 @@ body {
|
||||
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 {
|
||||
margin: auto;
|
||||
max-width: 40em;
|
||||
@ -84,16 +94,48 @@ body {
|
||||
}
|
||||
|
||||
.bulb_box {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: space-around;
|
||||
width: fit-content;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
.bulb_box > * {
|
||||
margin: 0.5em;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
/* lol i'm so funny */
|
||||
.selector_selector {
|
||||
margin-bottom: 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
//background: #534f44;
|
||||
}
|
||||
.selector_selector > button {
|
||||
width: 10rem;
|
||||
border: solid 0.1rem wheat;
|
||||
background-color: #3a3743;
|
||||
color: white;
|
||||
font-size: 1.5rem;
|
||||
border-left: solid white 0.1rem;
|
||||
border-right: solid white 0.1rem;
|
||||
}
|
||||
.selector_selector > button:hover {
|
||||
background-color: #4c4858;
|
||||
}
|
||||
.selector_selector > button:active {
|
||||
background-color: #312e38;
|
||||
}
|
||||
.selector_selector > button:first-child {
|
||||
border-top-left-radius: 0.5rem;
|
||||
}
|
||||
.selector_selector > button:last-child {
|
||||
border-top-right-radius: 0.5rem;
|
||||
}
|
||||
.selector_selector_arrow {
|
||||
position: absolute;
|
||||
width: 2.15rem;
|
||||
height: 2.15rem;
|
||||
background: url("/images/circle-arrow.svg");
|
||||
transition: transform 0.5s ease-in-out;
|
||||
}
|
||||
|
||||
.bulb_controls {
|
||||
@ -101,9 +143,88 @@ body {
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
padding-right: 0.5em;
|
||||
background: #56636e;
|
||||
border: solid 0.25em #5b3f63;
|
||||
background: #00000000;
|
||||
}
|
||||
|
||||
.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_list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.bulb_list_group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.bulb_list_group > h1 {
|
||||
margin-top: 0.6em;
|
||||
margin-bottom: 0;
|
||||
font-size: 1.3em;
|
||||
border-bottom: solid 1px;
|
||||
}
|
||||
|
||||
.bulb_list_bulb {
|
||||
font-family: Ubuntu Mono;
|
||||
display: flex;
|
||||
font-size: 0.8em;
|
||||
padding-top: 0.5em;
|
||||
padding-bottom: 0.5em;
|
||||
margin-top: 0.3em;
|
||||
background-color: #3a3743;
|
||||
border: solid 0.2em #45374f;
|
||||
border-radius: 0.5em;
|
||||
color: wheat;
|
||||
}
|
||||
.bulb_list_bulb:hover {
|
||||
background-color: #4c4858;
|
||||
}
|
||||
.bulb_list_bulb:active {
|
||||
background-color: #312e38;
|
||||
}
|
||||
.bulb_list_bulb > span {
|
||||
margin: auto;
|
||||
flex-grow: 1;
|
||||
padding-right: 1em;
|
||||
}
|
||||
|
||||
.bulb_map {
|
||||
@ -111,14 +232,14 @@ body {
|
||||
background-size: auto;
|
||||
background-size: 1em;
|
||||
padding: 2rem;
|
||||
border: solid 0.4rem #5b3f63;
|
||||
border: solid 0.1rem;
|
||||
border-radius: 0.2rem;
|
||||
}
|
||||
|
||||
.bulb_map::after {
|
||||
position: absolute;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
content: #0003;
|
||||
}
|
||||
|
||||
.bulb_group {
|
||||
@ -130,6 +251,7 @@ body {
|
||||
font-size: 2em;
|
||||
position: absolute;
|
||||
box-shadow: #0006 0.1em 0.1em 0.1em;
|
||||
user-select: none;
|
||||
transition: 0.3s ease-in-out;
|
||||
}
|
||||
|
||||
@ -277,7 +399,80 @@ body {
|
||||
//margin-bottom: .7em;
|
||||
margin-left: .5em;
|
||||
}
|
||||
|
||||
.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;
|
||||
}
|
||||
|
||||
.bulb_list_checkbox {}
|
||||
.bulb_list_checkbox input[type="checkbox"] {
|
||||
visibility: hidden;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.bulb_list_checkbox *,
|
||||
.bulb_list_checkbox ::after,
|
||||
.bulb_list_checkbox ::before {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.bulb_list_checkbox .container {
|
||||
display: block;
|
||||
position: relative;
|
||||
cursor: pointer;
|
||||
font-size: 25px;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
/* Create a custom checkbox */
|
||||
.bulb_list_checkbox .checkmark {
|
||||
position: relative;
|
||||
top: 0;
|
||||
left: 0;
|
||||
height: 1.3em;
|
||||
width: 1.3em;
|
||||
background: black;
|
||||
border-radius: 50px;
|
||||
transition: all 0.7s;
|
||||
--spread: 10px;
|
||||
}
|
||||
|
||||
/* When the checkbox is checked, add a blue background */
|
||||
.bulb_list_checkbox .container input:checked ~ .checkmark {
|
||||
background: black;
|
||||
/* spawn a bunch of colored balls and blur them together */
|
||||
box-shadow: -5px -5px var(--spread) 0px #5B51D8, 0 -5px var(--spread) 0px #833AB4, 5px -5px var(--spread) 0px #E1306C, 5px 0 var(--spread) 0px #FD1D1D, 5px 5px var(--spread) 0px #F77737, 0 5px var(--spread) 0px #FCAF45, -5px 5px var(--spread) 0px #FFDC80;
|
||||
}
|
||||
|
||||
/* Create the checkmark/indicator (hidden when not checked) */
|
||||
.bulb_list_checkbox .checkmark::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Show the checkmark when checked */
|
||||
.bulb_list_checkbox .container input:checked ~ .checkmark::after {
|
||||
display: block;
|
||||
}
|
||||
|
||||
/* Style the checkmark/indicator */
|
||||
.bulb_list_checkbox .container .checkmark::after {
|
||||
left: 0.5em;
|
||||
top: 0.34em;
|
||||
width: 0.25em;
|
||||
height: 0.5em;
|
||||
border: solid wheat;
|
||||
border-width: 0 0.15em 0.15em 0;
|
||||
transform: rotate(45deg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user