4 Commits

Author SHA1 Message Date
b7fa967b3a wip 2024-08-09 10:15:43 +02:00
428e75488d Typos and todos 2024-08-09 10:15:18 +02:00
86918e69c3 0.3.3 2024-03-27 08:59:18 +01:00
2af82804c0 Tweak light waker behaviour 2024-03-27 08:58:25 +01:00
9 changed files with 90 additions and 12 deletions

6
Cargo.lock generated
View File

@ -203,7 +203,7 @@ dependencies = [
[[package]] [[package]]
name = "common" name = "common"
version = "0.3.2" version = "0.3.3"
dependencies = [ dependencies = [
"chrono", "chrono",
"lighter_lib", "lighter_lib",
@ -605,7 +605,7 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "hemma" name = "hemma"
version = "0.3.2" version = "0.3.3"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@ -630,7 +630,7 @@ dependencies = [
[[package]] [[package]]
name = "hemma_web" name = "hemma_web"
version = "0.3.2" version = "0.3.3"
dependencies = [ dependencies = [
"chrono", "chrono",
"common", "common",

View File

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

View File

@ -0,0 +1,60 @@
use std::{collections::HashMap, sync::Arc};
use common::{BulbPrefs, Param};
use lighter_lib::BulbId;
use lighter_manager::manager::BulbManager;
use std::sync::Mutex;
use crate::util::DeadMansHandle;
use super::LightScript;
pub struct Auto {
state: Arc<Mutex<AutoState>>,
_task_handle: DeadMansHandle,
}
struct AutoState {
manager: BulbManager,
enabled_bulbs: HashMap<BulbId, AutoBulbState>,
}
#[derive(Default)]
struct AutoBulbState {}
impl LightScript for Auto {
fn get_params(&mut self, bulb: &BulbId) -> BulbPrefs {
let state = self.state.lock().unwrap();
let enabled = state.enabled_bulbs.contains_key(bulb);
BulbPrefs {
kvs: [("Auto".to_string(), Param::Toggle(enabled))]
.into_iter()
.collect(),
}
}
fn set_param(&mut self, bulb: &BulbId, name: &str, param: Param) {
if name != "Auto" {
error!("invalid param name");
return;
}
let Param::Toggle(enabled) = param else {
error!("invalid param kind");
return;
};
let mut state = self.state.lock().unwrap();
if !enabled {
state.enabled_bulbs.remove(bulb);
} else {
let bulb_state = AutoBulbState::default();
state.enabled_bulbs.insert(bulb.clone(), bulb_state);
}
}
}
async fn auto_task(state: Arc<Mutex<AutoState>>) {
loop {}
}

View File

@ -1,8 +1,10 @@
use common::{BulbPrefs, Param}; use common::{BulbPrefs, Param};
use lighter_lib::BulbId; use lighter_lib::BulbId;
mod daylight;
mod party; mod party;
mod waker; mod waker;
pub use daylight::Daylight;
pub use party::Party; pub use party::Party;
pub use waker::Waker; pub use waker::Waker;

View File

@ -37,13 +37,12 @@ impl LightScript for Party {
fn set_param(&mut self, bulb: &BulbId, name: &str, param: Param) { fn set_param(&mut self, bulb: &BulbId, name: &str, param: Param) {
if name != "Party" { if name != "Party" {
error!("invalit param name"); error!("invalid param name");
return; return;
} }
// TODO: should be toggle
let Param::Toggle(enabled) = param else { let Param::Toggle(enabled) = param else {
error!("invalit param kind"); error!("invalid param kind");
return; return;
}; };

View File

@ -27,6 +27,8 @@ impl Waker {
} }
const TIME_FMT: &str = "%H:%M"; const TIME_FMT: &str = "%H:%M";
const WAKE_TARGET_BRIGHTNESS: u8 = 75;
const WAKE_TARGET_TEMPERATURE: u8 = 60;
impl LightScript for Waker { impl LightScript for Waker {
fn get_params(&mut self, bulb: &BulbId) -> BulbPrefs { fn get_params(&mut self, bulb: &BulbId) -> BulbPrefs {
@ -116,9 +118,8 @@ async fn wake_task(manager: BulbManager, id: BulbId, day: Weekday, time: NaiveTi
let r = manager let r = manager
.until_interrupted(id.clone(), async { .until_interrupted(id.clone(), async {
// slowly turn up brightness of bulb // slowly turn up brightness of bulb
for brightness in (1..=75).map(|i| (i as f32) * 0.01) { for brightness in (1..=WAKE_TARGET_BRIGHTNESS).map(|i| (i as f32) * 0.01) {
//sleep(Duration::from_secs(12)).await; sleep(Duration::from_secs(12)).await;
sleep(Duration::from_millis(500)).await;
manager manager
.send_command(BulbCommand::SetColor( .send_command(BulbCommand::SetColor(
@ -130,6 +131,21 @@ async fn wake_task(manager: BulbManager, id: BulbId, day: Weekday, time: NaiveTi
)) ))
.await .await
} }
// slowly turn up temperature of bulb
for temperature in (1..=WAKE_TARGET_TEMPERATURE).map(|i| (i as f32) * 0.01) {
sleep(Duration::from_secs(12)).await;
manager
.send_command(BulbCommand::SetColor(
BulbSelector::Id(id.clone()),
BulbColor::Kelvin {
t: temperature,
b: WAKE_TARGET_BRIGHTNESS as f32 * 0.01,
},
))
.await
}
}) })
.await; .await;

View File

@ -1,5 +1,6 @@
use tokio::task::AbortHandle; use tokio::task::AbortHandle;
/// A tokio task handle that will abort the task when dropped.
pub struct DeadMansHandle(pub AbortHandle); pub struct DeadMansHandle(pub AbortHandle);
impl From<AbortHandle> for DeadMansHandle { impl From<AbortHandle> for DeadMansHandle {

View File

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

View File

@ -1,6 +1,6 @@
[package] [package]
name = "hemma_web" name = "hemma_web"
version = "0.3.2" version = "0.3.3"
authors = ["Joakim Hulthe <joakim@hulthe.net"] authors = ["Joakim Hulthe <joakim@hulthe.net"]
edition = "2021" edition = "2021"