feat(entrypoint): take arguments in preparation for a new persisted
crate (featuring a Signal
that serializes with postcard
to a fjall
database)
This commit is contained in:
@@ -17,6 +17,7 @@ axum = { version = "0.8.1", default-features = false, features = [
|
||||
"http1",
|
||||
"tokio",
|
||||
] }
|
||||
clap = { version = "4", features = ["derive", "env"] }
|
||||
deranged = { workspace = true, features = ["serde"] }
|
||||
driver-kasa = { path = "../driver/kasa" }
|
||||
emitter-and-signal = { path = "../emitter-and-signal" }
|
||||
|
@@ -1,16 +1,18 @@
|
||||
use std::{str::FromStr, time::Duration};
|
||||
use std::{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,
|
||||
};
|
||||
use protocol::light::Light;
|
||||
use protocol::light::{IsOff, IsOn};
|
||||
use pyo3::prelude::*;
|
||||
use shadow_rs::shadow;
|
||||
use tokio::time::interval;
|
||||
use tracing::{level_filters::LevelFilter, Level};
|
||||
use tracing_appender::rolling::{self, RollingFileAppender};
|
||||
use tracing_subscriber::{
|
||||
fmt::{self, format::FmtSpan},
|
||||
fmt::{self, fmt, format::FmtSpan},
|
||||
layer::SubscriberExt,
|
||||
registry,
|
||||
util::SubscriberInitExt,
|
||||
@@ -22,7 +24,53 @@ mod tracing_to_home_assistant;
|
||||
|
||||
shadow!(build_info);
|
||||
|
||||
async fn real_main(home_assistant: HomeAssistant) -> ! {
|
||||
#[derive(Debug, Parser)]
|
||||
struct Args {
|
||||
#[arg(env)]
|
||||
persistence_directory: Option<PathBuf>,
|
||||
|
||||
#[arg(env)]
|
||||
tracing_directory: Option<PathBuf>,
|
||||
#[arg(env, default_value = "")]
|
||||
tracing_file_name_prefix: String,
|
||||
#[arg(env, default_value = "log")]
|
||||
tracing_file_name_suffix: String,
|
||||
#[arg(env, default_value_t = 64)]
|
||||
tracing_max_log_files: usize,
|
||||
}
|
||||
|
||||
async fn real_main(
|
||||
Args {
|
||||
persistence_directory,
|
||||
tracing_directory,
|
||||
tracing_file_name_prefix,
|
||||
tracing_file_name_suffix,
|
||||
tracing_max_log_files,
|
||||
}: Args,
|
||||
home_assistant: HomeAssistant,
|
||||
) -> ! {
|
||||
let tracing_to_directory_res = tracing_directory
|
||||
.map(|tracing_directory| {
|
||||
tracing_appender::rolling::Builder::new()
|
||||
.filename_prefix(tracing_file_name_prefix)
|
||||
.filename_suffix(tracing_file_name_suffix)
|
||||
.max_log_files(tracing_max_log_files)
|
||||
.build(tracing_directory)
|
||||
.map(tracing_appender::non_blocking)
|
||||
})
|
||||
.transpose();
|
||||
|
||||
let (tracing_to_directory, _guard, tracing_to_directory_initialization_error) =
|
||||
match tracing_to_directory_res {
|
||||
Ok(tracing_to_directory) => match tracing_to_directory {
|
||||
Some((tracing_to_directory, guard)) => {
|
||||
(Some(tracing_to_directory), Some(guard), None)
|
||||
}
|
||||
None => (None, None, None),
|
||||
},
|
||||
Err(error) => (None, None, Some(error)),
|
||||
};
|
||||
|
||||
registry()
|
||||
.with(
|
||||
fmt::layer()
|
||||
@@ -31,14 +79,25 @@ async fn real_main(home_assistant: HomeAssistant) -> ! {
|
||||
.with_filter(LevelFilter::from_level(Level::TRACE)),
|
||||
)
|
||||
.with(TracingToHomeAssistant)
|
||||
.with(tracing_to_directory.map(|writer| {
|
||||
fmt::layer()
|
||||
.pretty()
|
||||
.with_span_events(FmtSpan::ACTIVE)
|
||||
.with_writer(writer)
|
||||
.with_filter(LevelFilter::from_level(Level::TRACE))
|
||||
}))
|
||||
.init();
|
||||
|
||||
if let Some(error) = tracing_to_directory_initialization_error {
|
||||
tracing::error!(?error, "cannot trace to directory");
|
||||
}
|
||||
|
||||
let built_at = build_info::BUILD_TIME;
|
||||
tracing::info!(built_at);
|
||||
|
||||
// let lamp = HomeAssistantLight {
|
||||
// home_assistant,
|
||||
// object_id: ObjectId::from_str("jacob_s_lamp_top").unwrap(),
|
||||
// object_id: ObjectId::from_str("jacob_s_lamp_side").unwrap(),
|
||||
// };
|
||||
|
||||
let ip = [10, 0, 3, 71];
|
||||
@@ -59,6 +118,11 @@ async fn real_main(home_assistant: HomeAssistant) -> ! {
|
||||
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;
|
||||
@@ -71,8 +135,10 @@ async fn real_main(home_assistant: HomeAssistant) -> ! {
|
||||
|
||||
#[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 {
|
||||
real_main(home_assistant).await;
|
||||
real_main(args, home_assistant).await;
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user