feat(entrypoint): try using persistence
This commit is contained in:
@@ -21,8 +21,9 @@ clap = { version = "4", features = ["derive", "env"] }
|
||||
deranged = { workspace = true, features = ["serde"] }
|
||||
driver-kasa = { path = "../driver/kasa" }
|
||||
emitter-and-signal = { path = "../emitter-and-signal" }
|
||||
home-assistant = { path = "../home-assistant" }
|
||||
home-assistant = { path = "../home-assistant", features = ["tracing"] }
|
||||
im = { version = "15.1.0", features = ["rayon"] }
|
||||
persisted = { path = "../persisted" }
|
||||
protocol = { path = "../protocol" }
|
||||
pyo3 = { workspace = true, features = [
|
||||
"auto-initialize",
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
use std::{path::PathBuf, str::FromStr, time::Duration};
|
||||
use std::{num::NonZeroUsize, path::PathBuf, str::FromStr, time::Duration};
|
||||
|
||||
use clap::Parser;
|
||||
use driver_kasa::connection::LB130USHandle;
|
||||
use home_assistant::{
|
||||
home_assistant::HomeAssistant, light::HomeAssistantLight, object_id::ObjectId,
|
||||
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 pyo3::prelude::*;
|
||||
use shadow_rs::shadow;
|
||||
use tokio::time::interval;
|
||||
use tokio::{task::spawn_blocking, time::interval};
|
||||
use tracing::{level_filters::LevelFilter, Level};
|
||||
use tracing_appender::rolling::{self, RollingFileAppender};
|
||||
use tracing_subscriber::{
|
||||
@@ -100,28 +102,49 @@ async fn real_main(
|
||||
// object_id: ObjectId::from_str("jacob_s_lamp_side").unwrap(),
|
||||
// };
|
||||
|
||||
let ip = [10, 0, 3, 71];
|
||||
let port = 9999;
|
||||
// let ip = [10, 0, 3, 71];
|
||||
// let port = 9999;
|
||||
|
||||
let some_light = LB130USHandle::new(
|
||||
(ip, port).into(),
|
||||
Duration::from_secs(10),
|
||||
(64).try_into().unwrap(),
|
||||
);
|
||||
// let some_light = LB130USHandle::new(
|
||||
// (ip, port).into(),
|
||||
// Duration::from_secs(10),
|
||||
// (64).try_into().unwrap(),
|
||||
// );
|
||||
|
||||
let mut interval = interval(Duration::from_secs(20));
|
||||
interval.tick().await;
|
||||
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 {
|
||||
interval.tick().await;
|
||||
int.tick().await;
|
||||
|
||||
tracing::info!("about to call get_sysinfo");
|
||||
let sysinfo_res = some_light.get_sysinfo().await;
|
||||
tracing::info!(?sysinfo_res, "got sys info");
|
||||
tracing::debug!(?value);
|
||||
// let service_result: Result<Py<PyAny>, _> = 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::<Context<()>>::None,
|
||||
// Option::<()>::None,
|
||||
// false,
|
||||
// )
|
||||
// .await;
|
||||
// tracing::debug!(?service_result);
|
||||
|
||||
let is_on = some_light.is_on().await;
|
||||
tracing::info!(?is_on);
|
||||
let is_off = some_light.is_off().await;
|
||||
tracing::info!(?is_off);
|
||||
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);
|
||||
@@ -131,13 +154,98 @@ async fn real_main(
|
||||
// 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<Py<PyAny>, _> = 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::<Context<()>>::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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
fn main<'py>(py: Python<'py>, home_assistant: HomeAssistant) -> PyResult<Bound<'py, PyAny>> {
|
||||
let args = Args::parse();
|
||||
|
||||
pyo3_async_runtimes::tokio::future_into_py::<_, ()>(py, async {
|
||||
pyo3_async_runtimes::tokio::future_into_py::<_, ()>(py, async move {
|
||||
real_main(args, home_assistant).await;
|
||||
})
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ impl<S: Subscriber> Layer<S> for TracingToHomeAssistant {
|
||||
|
||||
let log_data: Option<LogData<()>> = None;
|
||||
|
||||
Python::with_gil(|py| {
|
||||
Python::attach(|py| {
|
||||
let Ok(hass_logger) = HassLogger::new(py, target) else {
|
||||
return;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user