forked from moke/spaceapi-tuerpie-nix
Refactoring
This commit is contained in:
parent
ca70d8b39c
commit
703bb1b48c
107
src/main.rs
107
src/main.rs
|
@ -1,76 +1,75 @@
|
||||||
use std::env;
|
|
||||||
use std::thread::sleep;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use rppal::gpio::{Gpio, InputPin, Level};
|
use rppal::gpio::{Gpio, InputPin, Level};
|
||||||
use spaceapi_dezentrale_client::Client;
|
use spaceapi_dezentrale_client::ClientBuilder;
|
||||||
|
use std::{env, time::Duration};
|
||||||
|
use tokio::time::sleep;
|
||||||
|
|
||||||
// set your scripts for opening and closing the space here
|
// Static delays
|
||||||
static DOOR_PIN: u8 = 27;
|
static RECHECK_DELAY: Duration = Duration::from_secs(5);
|
||||||
// delays in seconds
|
static ANTI_BOUNCE_DELAY: Duration = Duration::from_secs(1);
|
||||||
static RECHECK_DELAY: u64 = 5;
|
|
||||||
static ANTI_BOUNCE_DELAY: u64 = 1;
|
#[derive(Debug)]
|
||||||
|
enum DoorStatus {
|
||||||
|
Open,
|
||||||
|
Closed,
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
|
let door_pin = env::var("DOOR_PIN").unwrap_or("27".to_string());
|
||||||
|
let door_pin = door_pin.parse().expect("A number for the DOOR_PIN");
|
||||||
|
|
||||||
// setup
|
// setup
|
||||||
log::debug!("Setup GPIO");
|
log::debug!("Setup GPIO");
|
||||||
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();
|
||||||
|
|
||||||
let spaceapi_client = spaceapi_dezentrale_client::ClientBuilder::new()
|
log::debug!("Build client");
|
||||||
.api_key(&env::var("API_KEY").unwrap())
|
let spaceapi_client = ClientBuilder::new()
|
||||||
.base_url(&env::var("SPACEAPI_URL").unwrap())
|
.api_key(&env::var("API_KEY").expect("Set API_KEY in environment"))
|
||||||
|
.base_url(&env::var("SPACEAPI_URL").expect("Set SPACEAPI_URL in environment"))
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// get initial door status
|
|
||||||
let mut door_status_old = check_door(&pin);
|
|
||||||
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
|
log::debug!("Checking door status");
|
||||||
log::debug!("Waiting {} secs to read the door status", RECHECK_DELAY);
|
let push_result = match check_door(&pin).await {
|
||||||
sleep(Duration::from_secs(RECHECK_DELAY));
|
DoorStatus::Open => {
|
||||||
|
log::debug!("Door is open, sending keep open to server");
|
||||||
// read new status
|
spaceapi_client.keep_open().await.map(|_| ())
|
||||||
let door_status_new = check_door(&pin);
|
|
||||||
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
|
|
||||||
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) {
|
|
||||||
log::debug!("Check passed, applying new status");
|
|
||||||
log::debug!("Pushing space status: Open = {}", door_status_new);
|
|
||||||
let _ = push_door_status(&spaceapi_client, door_status_new)
|
|
||||||
.await
|
|
||||||
.map_err(|err| format!("Problem while pushing door status: {err}"));
|
|
||||||
log::debug!("Saving space status: {}", door_status_new);
|
|
||||||
door_status_old = door_status_new;
|
|
||||||
} else {
|
|
||||||
log::debug!("Check wasn't successful")
|
|
||||||
}
|
}
|
||||||
|
DoorStatus::Closed => {
|
||||||
|
log::debug!("Door is closed, sending explicit close to server");
|
||||||
|
spaceapi_client.close().await
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let _ = push_result.map_err(|err| format!("Problem while pushing door status: {err}"));
|
||||||
|
|
||||||
|
sleep(RECHECK_DELAY).await;
|
||||||
|
log::debug!("Waiting {RECHECK_DELAY:?} secs to read the door status");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn check_door(pin: &InputPin) -> DoorStatus {
|
||||||
|
// reading space status from door
|
||||||
|
let mut pin_state = pin.read();
|
||||||
|
|
||||||
|
// Anti bounce verification
|
||||||
|
loop {
|
||||||
|
sleep(ANTI_BOUNCE_DELAY).await;
|
||||||
|
let pin_state_check = pin.read();
|
||||||
|
if pin_state_check == pin_state {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
pin_state = pin_state_check
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn check_door(pin: &InputPin) -> bool {
|
log::debug!("Pin state is {pin_state}");
|
||||||
// reading space status from door
|
if pin_state == Level::Low {
|
||||||
pin.read() == Level::Low
|
DoorStatus::Open
|
||||||
}
|
|
||||||
|
|
||||||
async fn push_door_status(spaceapi: &Client, open: bool) -> Result<(), String> {
|
|
||||||
if open {
|
|
||||||
log::debug!("Sending keep open to server");
|
|
||||||
spaceapi.keep_open().await.map(|_| ())
|
|
||||||
} else {
|
} else {
|
||||||
log::debug!("Sending explicit close to server");
|
DoorStatus::Closed
|
||||||
spaceapi.close().await
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue