1
0
Fork 0

Compare commits

...

4 Commits

3 changed files with 647 additions and 595 deletions

1196
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,14 @@
[package] [package]
name = "pi" name = "pi"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
dotenv = "0.15.0" dotenv = "0.15.0"
futures = "0.3.28" env_logger = "0.10.1"
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" }
tokio = { version = "1.35.1", features = ["macros"] }

View File

@ -2,7 +2,6 @@ use std::env;
use std::thread::sleep; use std::thread::sleep;
use std::time::Duration; use std::time::Duration;
use futures::executor::block_on;
use rppal::gpio::{Gpio, InputPin, Level}; use rppal::gpio::{Gpio, InputPin, Level};
use spaceapi_dezentrale_client::Client; use spaceapi_dezentrale_client::Client;
@ -12,7 +11,8 @@ static DOOR_PIN: u8 = 27;
static RECHECK_DELAY: u64 = 5; static RECHECK_DELAY: u64 = 5;
static ANTI_BOUNCE_DELAY: u64 = 1; static ANTI_BOUNCE_DELAY: u64 = 1;
fn main() { #[tokio::main]
async fn main() {
// setup // setup
let gpio = Gpio::new().unwrap(); let gpio = Gpio::new().unwrap();
let pin = gpio.get(DOOR_PIN).unwrap().into_input(); let pin = gpio.get(DOOR_PIN).unwrap().into_input();
@ -25,32 +25,33 @@ fn main() {
// get initial door status // get initial door status
let mut door_status_old = check_door(&pin); let mut door_status_old = check_door(&pin);
block_on(push_door_status(&spaceapi_client, door_status_old)); let _ = push_door_status(&spaceapi_client, door_status_old).await;
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); let _ = push_door_status(&spaceapi_client, door_status_new)
block_on(push_door_status(&spaceapi_client, door_status_new)); .await
println!("Saving space status: {}", door_status_new); .map_err(|err| format!("Problem while pushing door status: {err}")) ;
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")
} }
} }
} }
@ -61,6 +62,15 @@ fn check_door(pin: &InputPin) -> bool {
pin.read() == Level::Low pin.read() == Level::Low
} }
async fn push_door_status(spaceapi: &Client, open: bool) { async fn push_door_status(spaceapi: &Client, open: bool) -> Result<(), String> {
if open { spaceapi.open().await.unwrap() } else { spaceapi.close().await.unwrap() } if open {
spaceapi
.keep_open()
.await
.map(|_| ())
} else {
spaceapi
.close()
.await
}
} }