diff --git a/entrypoint/src/lib.rs b/entrypoint/src/lib.rs index d0f0660..e1b32d1 100644 --- a/entrypoint/src/lib.rs +++ b/entrypoint/src/lib.rs @@ -2,15 +2,19 @@ use std::{num::NonZeroUsize, path::PathBuf, str::FromStr, time::Duration}; use clap::Parser; use driver_kasa::connection::LB130USHandle; +use futures::stream::{FuturesUnordered, StreamExt}; use home_assistant::{ event::context::context::Context, home_assistant::HomeAssistant, light::HomeAssistantLight, notify::service::mobile_app::StandardNotification, object_id::ObjectId, }; use persisted::{persisted, Config, Keyspace, Partition, PartitionCreateOptions}; -use protocol::light::{IsOff, IsOn}; +use protocol::light::{IsOff, IsOn, Kelvin, TurnToTemperature}; use pyo3::prelude::*; use shadow_rs::shadow; -use tokio::{task::spawn_blocking, time::interval}; +use tokio::{ + task::{spawn_blocking, JoinSet}, + time::{interval, MissedTickBehavior}, +}; use tracing::{level_filters::LevelFilter, Level}; use tracing_appender::rolling::{self, RollingFileAppender}; use tracing_subscriber::{ @@ -97,148 +101,40 @@ async fn real_main( let built_at = build_info::BUILD_TIME; tracing::info!(built_at); - // let lamp = HomeAssistantLight { - // home_assistant, - // object_id: ObjectId::from_str("jacob_s_lamp_side").unwrap(), - // }; + let mut bathroom_mirror_lights = [ + LB130USHandle::new( + ([10, 0, 3, 80], 9999).into(), + Duration::from_secs(10), + (64).try_into().unwrap(), + ), + LB130USHandle::new( + ([10, 0, 3, 82], 9999).into(), + Duration::from_secs(10), + (64).try_into().unwrap(), + ), + ]; - // let ip = [10, 0, 3, 71]; - // let port = 9999; + let mut interval = interval(Duration::from_secs(15)); + interval.set_missed_tick_behavior(MissedTickBehavior::Delay); - // let some_light = LB130USHandle::new( - // (ip, port).into(), - // Duration::from_secs(10), - // (64).try_into().unwrap(), - // ); + let mut temperature = Kelvin::MIN; - let mut int = interval(Duration::from_secs(170)); - let mut value = 0; - - tokio::time::sleep(Duration::from_secs(15)).await; - - let services = Python::attach(|py| home_assistant.services(py)).unwrap(); loop { - int.tick().await; + let instant = interval.tick().await; + // temperature = temperature.wrapping_add(5173); + tracing::info!(?temperature); - tracing::debug!(?value); - // let service_result: Result, _> = services - // .call_service( - // StandardNotification::builder() - // .object_id(ObjectId("galaxy_s21_ultra_1".parse().unwrap())) - // .message(format!("The counter is now {value:?}").parse().unwrap()) - // .title("New value of the counter".into()) - // .build(), - // Option::>::None, - // Option::<()>::None, - // false, - // ) - // .await; - // tracing::debug!(?service_result); + let tasks = bathroom_mirror_lights + .iter_mut() + .map(|bathroom_mirror_light| bathroom_mirror_light.turn_to_temperature(temperature)); + let mut tasks = FuturesUnordered::from_iter(tasks); - value += 1; - - // tracing::info!("about to call get_sysinfo"); - // let sysinfo_res = some_light.get_sysinfo().await; - // tracing::info!(?sysinfo_res, "got sys info"); - - // let is_on = some_light.is_on().await; - // tracing::info!(?is_on); - // let is_off = some_light.is_off().await; - // tracing::info!(?is_off); - - // let is_on = lamp.is_on().await; - // tracing::info!(?is_on); - // let is_off = lamp.is_off().await; - // tracing::info!(?is_off); - - // let something = lamp.turn_on().await; - // tracing::info!(?something); - } - - if let Some(persistence_directory) = persistence_directory { - let config = Config::new(persistence_directory); - let keyspace = Keyspace::open(config).unwrap(); // TODO: just debugging and experiencing it - let partition_name = "trying_this_out_partition"; - let create_options = PartitionCreateOptions::default(); - let partition = - spawn_blocking(move || keyspace.open_partition(partition_name, create_options)) - .await - .unwrap() - // TODO: just debugging and experiencing it - .unwrap(); - - let identifier = "0a7wmg09awgmagw97nawg7awg90a8wgn982".into(); - let buffer = NonZeroUsize::new(128).unwrap(); - let (setter, signal, task) = persisted(partition, identifier, buffer).await; - - let consumer = tokio::spawn({ - let mut subscription = signal.subscribe().unwrap(); - - let services = Python::attach(|py| home_assistant.services(py)).unwrap(); - async move { - let initial = subscription.get(); - tracing::debug!(?initial); - - loop { - tracing::info!("waiting for changed"); - subscription.changed().await.unwrap(); - - let value = subscription.get(); - tracing::debug!(?value); - - // TODO: WIP: DEBUGGING - let service_result: Result, _> = services - .call_service( - StandardNotification::builder() - .object_id(ObjectId("galaxy_s21_ultra_1".parse().unwrap())) - .message(format!("The counter is now {value:?}").parse().unwrap()) - .title("New value of the counter".into()) - .build(), - Option::>::None, - Option::<()>::None, - false, - ) - .await; - - tracing::error!(?service_result); - let service_result_introspection = Python::attach(|py| { - service_result - .map(|ret| ret.bind(py).get_type().name().unwrap().to_string()) - }); - tracing::error!(?service_result_introspection); - } + while let Some(result) = tasks.next().await { + match result { + Ok(()) => {} + Err(error_turning_to_temperature) => tracing::error!(?error_turning_to_temperature), } - }); - - let producer = tokio::spawn({ - let mut subscription = signal.subscribe().unwrap(); - async move { - let mut int = interval(Duration::from_secs(190)); - - loop { - int.tick().await; - - let value = subscription.get(); - tracing::debug!(?value); - - // let mut counter = value.unwrap_or(14u16); - // tracing::info!(?counter); - - if let Ok(counter) = value { - let counter: u16 = counter + 1; - setter.set(counter).await.unwrap(); - } - } - } - }); - - producer.await.unwrap(); - consumer.await.unwrap(); - task.await.unwrap(); - - unreachable!(); - } else { - panic!("please set PERSISTENCE_DIRECTORY while debugging and trying persisted out") + } } }