Make periodic refreshs
This commit is contained in:
22
Cargo.lock
generated
22
Cargo.lock
generated
@@ -208,6 +208,7 @@ dependencies = [
|
|||||||
"serialport",
|
"serialport",
|
||||||
"strum",
|
"strum",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"tokio-scoped",
|
||||||
"tokio_schedule",
|
"tokio_schedule",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -879,6 +880,27 @@ dependencies = [
|
|||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tokio-scoped"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e4beb8ba13bc53ac53ce1d52b42f02e5d8060f0f42138862869beb769722b256"
|
||||||
|
dependencies = [
|
||||||
|
"tokio",
|
||||||
|
"tokio-stream",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tokio-stream"
|
||||||
|
version = "0.1.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "267ac89e0bec6e691e5813911606935d77c476ff49024f98abcea3e7b15e37af"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
"pin-project-lite",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio_schedule"
|
name = "tokio_schedule"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
|
|||||||
@@ -15,4 +15,5 @@ log = "0.4.21"
|
|||||||
serialport = "4.3.0"
|
serialport = "4.3.0"
|
||||||
strum = { version = "0.26.2", features = ["derive"] }
|
strum = { version = "0.26.2", features = ["derive"] }
|
||||||
tokio = { version = "1.37.0", features = ["full"] }
|
tokio = { version = "1.37.0", features = ["full"] }
|
||||||
|
tokio-scoped = "0.2.0"
|
||||||
tokio_schedule = "0.3.1"
|
tokio_schedule = "0.3.1"
|
||||||
|
|||||||
40
src/main.rs
40
src/main.rs
@@ -107,23 +107,20 @@ where
|
|||||||
async fn main() {
|
async fn main() {
|
||||||
let state = Arc::new(Mutex::new(None::<Status>));
|
let state = Arc::new(Mutex::new(None::<Status>));
|
||||||
|
|
||||||
// let periodic_state = Arc::clone(&state);
|
let periodic_state = Arc::clone(&state);
|
||||||
// let periodic_refresh = every(5).minutes().perform(|| async {
|
tokio::spawn(async move {
|
||||||
// log::info!("Periodic data fetch");
|
loop {
|
||||||
// let mut lock = periodic_state.lock().unwrap();
|
log::info!("Periodic data fetch");
|
||||||
// if let Ok(status) = get_status_from_device() {
|
{
|
||||||
// *lock = Some(status.clone());
|
let mut lock = periodic_state.lock().unwrap();
|
||||||
// }
|
if let Ok(status) = get_status_from_device() {
|
||||||
// });
|
*lock = Some(status.clone());
|
||||||
// tokio::spawn(async move {
|
}
|
||||||
// loop {
|
}
|
||||||
// log::info!("Periodic data fetch");
|
|
||||||
// let mut lock = periodic_state.lock().unwrap();
|
tokio::time::sleep(tokio::time::Duration::from_secs(5 * 60)).await;
|
||||||
// if let Ok(status) = get_status_from_device() {
|
}
|
||||||
// *lock = Some(status.clone());
|
});
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
let app = Router::new().route("/", get(handler)).with_state(state);
|
let app = Router::new().route("/", get(handler)).with_state(state);
|
||||||
|
|
||||||
@@ -137,12 +134,15 @@ async fn handler(
|
|||||||
log::info!("Fetch new data");
|
log::info!("Fetch new data");
|
||||||
|
|
||||||
let mut lock = state.lock().unwrap();
|
let mut lock = state.lock().unwrap();
|
||||||
let status = if let Ok(status) = get_status_from_device() {
|
let fetched_status = get_status_from_device();
|
||||||
|
let status = if let Ok(status) = fetched_status {
|
||||||
*lock = Some(status.clone());
|
*lock = Some(status.clone());
|
||||||
|
|
||||||
status
|
status
|
||||||
} else {
|
} else {
|
||||||
lock.clone().context("No device state yet fetched")?
|
lock.clone()
|
||||||
|
.context(fetched_status.unwrap_err())
|
||||||
|
.context("Device state not yet fetched")?
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Html(format!(
|
Ok(Html(format!(
|
||||||
@@ -177,7 +177,7 @@ async fn handler(
|
|||||||
|
|
||||||
fn get_status_from_device() -> anyhow::Result<Status> {
|
fn get_status_from_device() -> anyhow::Result<Status> {
|
||||||
let data = read_device().context("Could not retrieve device data")?;
|
let data = read_device().context("Could not retrieve device data")?;
|
||||||
let data_string = std::str::from_utf8(&data).unwrap();
|
let data_string = std::str::from_utf8(&data)?;
|
||||||
|
|
||||||
let status_map = StatusTag::iter()
|
let status_map = StatusTag::iter()
|
||||||
.zip(data_string.split('\t'))
|
.zip(data_string.split('\t'))
|
||||||
|
|||||||
Reference in New Issue
Block a user