Merge pull request 'Refactoring and fixes' (#2) from alex/spaceapi-tuerpie-nix:main into main
Reviewed-on: #2
This commit is contained in:
commit
9061256d4c
|
@ -0,0 +1,5 @@
|
|||
[target.armv7-unknown-linux-musleabi]
|
||||
linker = "arm-linux-gnueabi-gcc"
|
||||
|
||||
[target.armv7-unknown-linux-musleabihf]
|
||||
linker = "arm-linux-gnueabihf-gcc"
|
|
@ -98,9 +98,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.76"
|
||||
version = "0.1.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "531b97fb4cd3dfdce92c35dedbfdc1f0b9d8091c8ca943d6dae340ef5012d514"
|
||||
checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -202,9 +202,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.4.12"
|
||||
version = "4.4.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcfab8ba68f3668e89f6ff60f5b205cea56aa7b769451a59f34b8682f51c056d"
|
||||
checksum = "52bdc885e4cacc7f7c9eedc1ef6da641603180c783c41a15c264944deeaab642"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
]
|
||||
|
@ -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"
|
||||
|
@ -395,7 +389,6 @@ checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0"
|
|||
dependencies = [
|
||||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-executor",
|
||||
"futures-io",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
|
@ -418,34 +411,12 @@ version = "0.3.30"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
|
||||
|
||||
[[package]]
|
||||
name = "futures-executor"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
"futures-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1"
|
||||
|
||||
[[package]]
|
||||
name = "futures-macro"
|
||||
version = "0.3.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
version = "0.3.30"
|
||||
|
@ -467,7 +438,6 @@ dependencies = [
|
|||
"futures-channel",
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"futures-macro",
|
||||
"futures-sink",
|
||||
"futures-task",
|
||||
"memchr",
|
||||
|
@ -885,14 +855,13 @@ checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
|
|||
|
||||
[[package]]
|
||||
name = "pi"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"dotenv",
|
||||
"env_logger",
|
||||
"futures",
|
||||
"log",
|
||||
"rppal",
|
||||
"spaceapi-dezentrale-client",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -921,9 +890,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
|||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.73"
|
||||
version = "1.0.75"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dd5e8a1f1029c43224ad5898e50140c2aebb1705f19e67c918ebf5b9e797fe1"
|
||||
checksum = "907a61bd0f64c2f29cd1cf1dc34d05176426a3f504a78010f08416ddb7b13708"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
@ -943,9 +912,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.34"
|
||||
version = "1.0.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22a37c9326af5ed140c86a46655b5278de879853be5573c01df185b6f49a580a"
|
||||
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
@ -991,18 +960,18 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "ref-cast"
|
||||
version = "1.0.21"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53313ec9f12686aeeffb43462c3ac77aa25f590a5f630eb2cde0de59417b29c7"
|
||||
checksum = "c4846d4c50d1721b1a3bef8af76924eef20d5e723647333798c1b519b3a9473f"
|
||||
dependencies = [
|
||||
"ref-cast-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ref-cast-impl"
|
||||
version = "1.0.21"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2566c4bf6845f2c2e83b27043c3f5dfcd5ba8f2937d6c00dc009bfb51a079dc4"
|
||||
checksum = "5fddb4f8d99b0a2ebafc65a87a69a7b9875e4b1ae1f00db265d300ef7f28bccc"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -1284,18 +1253,18 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.193"
|
||||
version = "1.0.194"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
|
||||
checksum = "0b114498256798c94a0689e1a15fec6005dee8ac1f41de56404b67afc2a4b773"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.193"
|
||||
version = "1.0.194"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
|
||||
checksum = "a3385e45322e8f9931410f01b3031ec534c3947d0e94c18049af4d9f9907d4e0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -1304,9 +1273,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.109"
|
||||
version = "1.0.111"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb0652c533506ad7a2e353cce269330d6afd8bdfb6d75e0ace5b35aacbd7b9e9"
|
||||
checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
|
@ -1336,9 +1305,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "serde_yaml"
|
||||
version = "0.9.29"
|
||||
version = "0.9.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a15e0ef66bf939a7c890a0bf6d5a733c70202225f9888a89ed5c62298b019129"
|
||||
checksum = "b1bf28c79a99f70ee1f1d83d10c875d2e70618417fda01ad1785e027579d9d38"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"itoa",
|
||||
|
@ -1470,9 +1439,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.45"
|
||||
version = "2.0.48"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0eae3c679c56dc214320b67a1bc04ef3dfbd6411f6443974b5e4893231298e66"
|
||||
checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -1524,18 +1493,18 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.53"
|
||||
version = "1.0.56"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2cd5904763bad08ad5513ddbb12cf2ae273ca53fa9f68e843e236ec6dfccc09"
|
||||
checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.53"
|
||||
version = "1.0.56"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3dcf4a824cce0aeacd6f38ae6f24234c8e80d68632338ebaa1443b5df9e29e19"
|
||||
checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -2125,9 +2094,9 @@ checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"
|
|||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.5.31"
|
||||
version = "0.5.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "97a4882e6b134d6c28953a387571f1acdd3496830d5e36c5e3a1075580ea641c"
|
||||
checksum = "8434aeec7b290e8da5c3f0d628cb0eac6cabcb31d14bb74f779a08109a5914d6"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
[package]
|
||||
name = "pi"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
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"
|
||||
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" }
|
||||
tokio = { version = "1.35.1", features = ["macros"] }
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
|
|
81
README.md
81
README.md
|
@ -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
|
||||
```
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
[toolchain]
|
||||
channel = "stable"
|
||||
targets = ["armv7-unknown-linux-musleabihf"]
|
||||
profile = "default"
|
115
src/main.rs
115
src/main.rs
|
@ -1,70 +1,77 @@
|
|||
use std::env;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
|
||||
use futures::executor::block_on;
|
||||
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() {
|
||||
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");
|
||||
|
||||
fn main() {
|
||||
// setup
|
||||
log::debug!("Setup GPIO");
|
||||
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()
|
||||
.api_key(&env::var("API_KEY").unwrap())
|
||||
.base_url(&env::var("SPACEAPI_URL").unwrap())
|
||||
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);
|
||||
block_on(push_door_status(&spaceapi_client, door_status_old));
|
||||
|
||||
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);
|
||||
block_on(push_door_status(&spaceapi_client, door_status_new));
|
||||
log::debug!("Saving space status: {}", door_status_new);
|
||||
door_status_old = door_status_new;
|
||||
} else {
|
||||
log::debug!("Check wasn't successful")
|
||||
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(|_| ())
|
||||
.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) {
|
||||
if open {
|
||||
let _ = spaceapi
|
||||
.keep_open()
|
||||
.await
|
||||
.map_err(|err| log::error!("SpaceAPI call failed: {err}"));
|
||||
log::debug!("Pin state is {pin_state}");
|
||||
if pin_state == Level::Low {
|
||||
DoorStatus::Open
|
||||
} else {
|
||||
DoorStatus::Closed
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue