forked from moke/spaceapi-tuerpie-nix
Merge pull request 'Added dead man handle, added logger, bump crate versions' (#1) from alex/spaceapi-tuerpie-nix:spaceapi_dead_man_handle into main
Reviewed-on: moke/spaceapi-tuerpie-nix#1
This commit is contained in:
commit
582b701391
File diff suppressed because it is too large
Load Diff
|
@ -7,6 +7,8 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
dotenv = "0.15.0"
|
||||
env_logger = "0.10.1"
|
||||
futures = "0.3.28"
|
||||
log = "0.4.20"
|
||||
rppal = "0.14.1"
|
||||
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 {
|
||||
// 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));
|
||||
|
||||
// read new status
|
||||
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 door_status_old != door_status_new {
|
||||
// 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));
|
||||
// the new read status is still the same after a minute then push it to the api
|
||||
if door_status_new == check_door(&pin)
|
||||
{
|
||||
println!("Check passed, applying new status");
|
||||
println!("Pushing space status: Open = {}", door_status_new);
|
||||
if door_status_new == check_door(&pin) {
|
||||
log::debug!("Check passed, applying new status");
|
||||
log::debug!("Pushing space status: Open = {}", 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;
|
||||
} 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) {
|
||||
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