forked from moke/spaceapi-tuerpie-nix
Added dead man handle, added logger, bump crate versions
This commit is contained in:
parent
29b2014c80
commit
251e167922
File diff suppressed because it is too large
Load Diff
|
@ -7,6 +7,8 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
|
env_logger = "0.10.1"
|
||||||
futures = "0.3.28"
|
futures = "0.3.28"
|
||||||
|
log = "0.4.20"
|
||||||
rppal = "0.14.1"
|
rppal = "0.14.1"
|
||||||
spaceapi-dezentrale-client = { git = "https://github.com/dezentrale/spaceapi-rs.git", package = "spaceapi-dezentrale-client", branch = "main" }
|
spaceapi-dezentrale-client = { git = "https://github.com/dezentrale/spaceapi-rs.git", package = "spaceapi-dezentrale-client", branch = "main" }
|
||||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -29,28 +29,27 @@ fn main() {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// maybe not the best solution but the pi isn't doing anything else
|
// maybe not the best solution but the pi isn't doing anything else
|
||||||
println!("Waiting {} secs to read the door status", RECHECK_DELAY);
|
log::debug!("Waiting {} secs to read the door status", RECHECK_DELAY);
|
||||||
sleep(Duration::from_secs(RECHECK_DELAY));
|
sleep(Duration::from_secs(RECHECK_DELAY));
|
||||||
|
|
||||||
// read new status
|
// read new status
|
||||||
let door_status_new = check_door(&pin);
|
let door_status_new = check_door(&pin);
|
||||||
println!("Read {}", door_status_new);
|
log::debug!("Read {}", door_status_new);
|
||||||
|
|
||||||
// if the new status isn't the old one
|
// if the new status isn't the old one
|
||||||
if door_status_old != door_status_new {
|
if door_status_old != door_status_new {
|
||||||
// wait for the switch to stop bouncing around
|
// wait for the switch to stop bouncing around
|
||||||
println!("Waiting {} secs for recheck", ANTI_BOUNCE_DELAY);
|
log::debug!("Waiting {} secs for recheck", ANTI_BOUNCE_DELAY);
|
||||||
sleep(Duration::from_secs(ANTI_BOUNCE_DELAY));
|
sleep(Duration::from_secs(ANTI_BOUNCE_DELAY));
|
||||||
// the new read status is still the same after a minute then push it to the api
|
// the new read status is still the same after a minute then push it to the api
|
||||||
if door_status_new == check_door(&pin)
|
if door_status_new == check_door(&pin) {
|
||||||
{
|
log::debug!("Check passed, applying new status");
|
||||||
println!("Check passed, applying new status");
|
log::debug!("Pushing space status: Open = {}", door_status_new);
|
||||||
println!("Pushing space status: Open = {}", door_status_new);
|
|
||||||
block_on(push_door_status(&spaceapi_client, door_status_new));
|
block_on(push_door_status(&spaceapi_client, door_status_new));
|
||||||
println!("Saving space status: {}", door_status_new);
|
log::debug!("Saving space status: {}", door_status_new);
|
||||||
door_status_old = door_status_new;
|
door_status_old = door_status_new;
|
||||||
} else {
|
} else {
|
||||||
println!("Check wasn't successfully")
|
log::debug!("Check wasn't successful")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,5 +61,10 @@ fn check_door(pin: &InputPin) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn push_door_status(spaceapi: &Client, open: bool) {
|
async fn push_door_status(spaceapi: &Client, open: bool) {
|
||||||
if open { spaceapi.open().await.unwrap() } else { spaceapi.close().await.unwrap() }
|
if open {
|
||||||
|
let _ = spaceapi
|
||||||
|
.keep_open()
|
||||||
|
.await
|
||||||
|
.map_err(|err| log::error!("SpaceAPI call failed: {err}"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue