1
0
Fork 0

Compare commits

...

15 Commits

Author SHA1 Message Date
moke 9061256d4c Merge pull request 'Refactoring and fixes' (#2) from alex/spaceapi-tuerpie-nix:main into main
Reviewed-on: moke/spaceapi-tuerpie-nix#2
2024-01-09 08:50:58 +01:00
Alexander Böhm 0751b9ec5e Removed dotenv dependency 2024-01-05 17:11:57 +01:00
Alexander Böhm 07a1928d7d Removed expandable close call 2024-01-05 17:03:29 +01:00
Alexander Böhm 5545372ca8 Updated README 2024-01-05 17:02:03 +01:00
Alexander Böhm 63044009c9 Fix debug wait log message 2024-01-05 16:56:18 +01:00
Alexander Böhm 703bb1b48c Refactoring 2024-01-05 16:53:27 +01:00
Alexander Böhm ca70d8b39c Add install instruction 2024-01-05 16:30:28 +01:00
Alexander Böhm 189528591e Added debug statements 2024-01-05 16:08:31 +01:00
Alexander Böhm df2a35f4d3 Added cross compile configuration 2024-01-05 15:02:36 +01:00
Alexander Böhm af91ec61c9 Enabled stripping for release builds 2024-01-05 15:02:12 +01:00
Alexander Böhm f16bc27499 Added build instruction 2024-01-05 15:01:24 +01:00
Alexander Böhm 344dabf250 Add debug info for setup GPIO 2024-01-05 13:15:31 +01:00
Alexander Böhm 1af8be2aa1 Fix logger output 2024-01-05 10:56:56 +01:00
Alexander Böhm 281392fe0c Merge branch 'spaceapi_dead_man_handle' 2024-01-05 01:17:05 +01:00
moke 582b701391 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
2024-01-04 12:21:58 +01:00
6 changed files with 146 additions and 72 deletions

5
.cargo/config.toml Normal file
View File

@ -0,0 +1,5 @@
[target.armv7-unknown-linux-musleabi]
linker = "arm-linux-gnueabi-gcc"
[target.armv7-unknown-linux-musleabihf]
linker = "arm-linux-gnueabihf-gcc"

7
Cargo.lock generated
View File

@ -302,12 +302,6 @@ dependencies = [
"syn",
]
[[package]]
name = "dotenv"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
[[package]]
name = "either"
version = "1.9.0"
@ -863,7 +857,6 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
name = "pi"
version = "0.1.1"
dependencies = [
"dotenv",
"env_logger",
"log",
"rppal",

View File

@ -6,9 +6,11 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dotenv = "0.15.0"
env_logger = "0.10.1"
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" }
tokio = { version = "1.35.1", features = ["macros"] }
[profile.release]
strip = true

View File

@ -1,12 +1,81 @@
# Spaceapi Tuerpi
Feeding our SpaceAPI with Data from our entrance door.
Feeding our SpaceAPI with data from our entrance door.
## Setup
## Usage
Please change `DOOR_PIN` accordingly.
To run the client, please change `DOOR_PIN` accordingly to your setup. `DOOR_PIN` defaults to a closed door, when `HIGH`.
`DOOR_PIN` defaults to a closed door, when HIGH.
You need to provide the environment variables `SPACEAPI_URL` and `API_KEY` to send the status to the SpaceAPI server. Details of the server can be found on the [project page](https://github.com/dezentrale/spaceapi-rs).
You need to provide the environment variables
`SPACEAPI_URL` and `API_KEY`.
## Build
[Install stable version of rust](https://rustup.rs/) and run a regular cargo build:
```sh
cargo build
```
### Cross compile
Install arm toolchain and *libmusl* for static linked binaries:
```sh
apt-get install gcc-arm-linux-gnueabihf musl-dev musl-tools
```
Build the application for raspberry pi:
```sh
cargo build --target=armv7-unknown-linux-musleabihf
```
## Install
Build the project as release and place `pi` binary under `/usr/local/bin/spaceapi-tuerpi`
```sh
sudo install -m 755 -o root -g root \
target/release/pi \
/usr/local/bin/spaceapi-tuerpi
```
Configure the spaceapi systemd service by placing the service description under `/etc/systemd/system/spaceapi.service`:
```
[Unit]
Description=SpaceAPI tuerpi client
After=network.target
[Service]
Type=simple
Restart=always
EnvironmentFile=/etc/spaceapi-tuerpi.env
ExecStart=/usr/local/bin/spaceapi-tuerpi
[Install]
WantedBy=multi-user.target
```
Place the environment configuration with URL and API-Key under `/etc/spaceapi-tuerpi.env`:
```
SPACEAPI_URL=<your SpaceAPI server endpoint>
API_KEY=<your secret API key>
#DOOR_PIN=27 # Set the door pin
#RUST_LOG=ERROR # Set debug level
```
Ensure the `/etc/spaceapi-tuerpi.env` can be read only by root by executing:
```sh
sudo chmod 600 /etc/spaceapi-tuerpi.env
sudo chown root:root /etc/spaceapi-tuerpi.env
```
Apply changes to systemd to enable and start the service
```sh
sudo systemctl daemon-reload
sudo systemctl enable --now spaceapi
```

4
rust-toolchain Normal file
View File

@ -0,0 +1,4 @@
[toolchain]
channel = "stable"
targets = ["armv7-unknown-linux-musleabihf"]
profile = "default"

View File

@ -1,76 +1,77 @@
use std::env;
use std::thread::sleep;
use std::time::Duration;
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 DOOR_PIN: u8 = 27;
// delays in seconds
static RECHECK_DELAY: u64 = 5;
static ANTI_BOUNCE_DELAY: u64 = 1;
// Static delays
static RECHECK_DELAY: Duration = Duration::from_secs(5);
static ANTI_BOUNCE_DELAY: Duration = Duration::from_secs(1);
#[derive(Debug)]
enum DoorStatus {
Open,
Closed,
}
#[tokio::main]
async fn main() {
// setup
let gpio = Gpio::new().unwrap();
let pin = gpio.get(DOOR_PIN).unwrap().into_input();
env_logger::init();
let spaceapi_client = spaceapi_dezentrale_client::ClientBuilder::new()
.api_key(&env::var("API_KEY").unwrap())
.base_url(&env::var("SPACEAPI_URL").unwrap())
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
log::debug!("Setup GPIO");
let gpio = Gpio::new().unwrap();
let pin = gpio.get(door_pin).unwrap().into_input();
log::debug!("Build client");
let spaceapi_client = ClientBuilder::new()
.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()
.unwrap();
// get initial door status
let mut door_status_old = check_door(&pin);
let _ = push_door_status(&spaceapi_client, door_status_old).await;
loop {
// maybe not the best solution but the pi isn't doing anything else
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);
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)
log::debug!("Checking door status");
match check_door(&pin).await {
DoorStatus::Open => {
log::debug!("Door is open, sending keep open to server");
let _ = spaceapi_client
.keep_open()
.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")
.map(|_| ())
.map_err(|err| format!("Problem while pushing door status: {err}"));
}
DoorStatus::Closed => {
log::debug!("Door is closed, doing nothing");
}
};
log::debug!("Waiting {RECHECK_DELAY:?} secs to read the door status");
sleep(RECHECK_DELAY).await;
}
}
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 {
// reading space status from door
pin.read() == Level::Low
}
async fn push_door_status(spaceapi: &Client, open: bool) -> Result<(), String> {
if open {
spaceapi
.keep_open()
.await
.map(|_| ())
log::debug!("Pin state is {pin_state}");
if pin_state == Level::Low {
DoorStatus::Open
} else {
spaceapi
.close()
.await
DoorStatus::Closed
}
}