mirror of
https://github.com/Djeeberjr/fw-anwesenheit.git
synced 2026-05-01 02:59:09 +00:00
Compare commits
14 Commits
7a438d1a9f
...
v1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 732411cd50 | |||
| 09725c1e04 | |||
| 3e079c905f | |||
|
|
eaca9d8cec | ||
|
|
cd713d5849 | ||
| 1514409070 | |||
| 5c16aaa9fe | |||
| 24b48f6705 | |||
| 434353b1e3 | |||
| 6b9ef20187 | |||
| 3c1290aec3 | |||
| 4781570f8e | |||
| efd096a149 | |||
| dc8fd22f0f |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
/build
|
||||||
|
|||||||
7
Cargo.lock
generated
7
Cargo.lock
generated
@@ -41,6 +41,12 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.98"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-stream"
|
name = "async-stream"
|
||||||
version = "0.3.6"
|
version = "0.3.6"
|
||||||
@@ -419,6 +425,7 @@ dependencies = [
|
|||||||
name = "fw-anwesenheit"
|
name = "fw-anwesenheit"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"chrono",
|
"chrono",
|
||||||
"gpio",
|
"gpio",
|
||||||
"log",
|
"log",
|
||||||
|
|||||||
@@ -22,4 +22,5 @@ rppal = { version = "0.22.1", features = ["hal"] }
|
|||||||
smart-leds = "0.3"
|
smart-leds = "0.3"
|
||||||
ws2812-spi = "0.3"
|
ws2812-spi = "0.3"
|
||||||
rgb = "0.8.50"
|
rgb = "0.8.50"
|
||||||
|
anyhow = "1.0.98"
|
||||||
|
|
||||||
|
|||||||
61
Makefile
Normal file
61
Makefile
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
PACKAGE_NAME := fwa
|
||||||
|
VERSION := 1.0
|
||||||
|
ARCH := armhf
|
||||||
|
BUILD_DIR := build
|
||||||
|
DEB_DIR := $(BUILD_DIR)/$(PACKAGE_NAME)-$(VERSION)
|
||||||
|
BIN_DIR := $(DEB_DIR)/usr/local/bin
|
||||||
|
SERVICE_DIR := $(DEB_DIR)/lib/systemd/system
|
||||||
|
CONFIG_DIR := $(DEB_DIR)/etc
|
||||||
|
PM3_DIR := $(DEB_DIR)/usr/share/pm3
|
||||||
|
|
||||||
|
.PHONY: all build clean package prepare_package
|
||||||
|
|
||||||
|
all: package
|
||||||
|
|
||||||
|
build: $(BUILD_DIR)/fwa
|
||||||
|
|
||||||
|
$(BUILD_DIR)/fwa: web/dist
|
||||||
|
cross build --release --target arm-unknown-linux-gnueabihf
|
||||||
|
cp ./target/arm-unknown-linux-gnueabihf/release/fw-anwesenheit $@
|
||||||
|
|
||||||
|
prepare_package: $(DEB_DIR)/DEBIAN $(BIN_DIR)/fwa
|
||||||
|
mkdir -p $(SERVICE_DIR)
|
||||||
|
cp ./service/fwa.service $(SERVICE_DIR)/
|
||||||
|
cp ./service/fwa-fail.service $(SERVICE_DIR)/
|
||||||
|
|
||||||
|
mkdir -p $(CONFIG_DIR)
|
||||||
|
cp ./service/fwa.env $(CONFIG_DIR)/
|
||||||
|
|
||||||
|
mkdir -p $(PM3_DIR)
|
||||||
|
cp -r ./pre-compiled/* $(PM3_DIR)/
|
||||||
|
|
||||||
|
mkdir -p $(DEB_DIR)/var/lib/fwa/
|
||||||
|
|
||||||
|
$(BIN_DIR)/fwa: $(BUILD_DIR)/fwa
|
||||||
|
mkdir -p $(BIN_DIR)
|
||||||
|
cp $< $@
|
||||||
|
|
||||||
|
|
||||||
|
$(DEB_DIR)/DEBIAN:
|
||||||
|
mkdir -p $(DEB_DIR)/DEBIAN
|
||||||
|
echo "Package: $(PACKAGE_NAME)" > $(DEB_DIR)/DEBIAN/control
|
||||||
|
echo "Version: $(VERSION)" >> $(DEB_DIR)/DEBIAN/control
|
||||||
|
echo "Section: utils" >> $(DEB_DIR)/DEBIAN/control
|
||||||
|
echo "Priority: optional" >> $(DEB_DIR)/DEBIAN/control
|
||||||
|
echo "Architecture: $(ARCH)" >> $(DEB_DIR)/DEBIAN/control
|
||||||
|
echo "Depends: libc6 (>= 2.28)" >> $(DEB_DIR)/DEBIAN/control
|
||||||
|
echo "Maintainer: Niklas Kapelle <niklas@kapelle.org>" >> $(DEB_DIR)/DEBIAN/control
|
||||||
|
echo "Description: Feuerwehr anwesenheit" >> $(DEB_DIR)/DEBIAN/control
|
||||||
|
|
||||||
|
echo "/etc/fwa.env" > $(DEB_DIR)/DEBIAN/conffiles
|
||||||
|
|
||||||
|
web/dist:
|
||||||
|
(cd web && npm run build)
|
||||||
|
|
||||||
|
package: prepare_package
|
||||||
|
dpkg-deb --build $(DEB_DIR)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
cargo clean
|
||||||
|
rm -rf web/dist
|
||||||
|
rm -rf $(BUILD_DIR)
|
||||||
12
README.md
12
README.md
@@ -9,6 +9,10 @@ I²C fpr RTC `sudo raspi-config` -> interface -> enable I²C
|
|||||||
|
|
||||||
# Config
|
# Config
|
||||||
|
|
||||||
|
Flags:
|
||||||
|
|
||||||
|
`--error` or `-e`: Enters error state. The LED turns red and the hotspot is activated. This state gets called from systemd if the service is in a failure state.
|
||||||
|
|
||||||
Environment variables:
|
Environment variables:
|
||||||
|
|
||||||
- `PM3_BIN`: Path to the pm3 binary. Seach in path if not set. Can also be set to the `pm3_mock.sh` for testing.
|
- `PM3_BIN`: Path to the pm3 binary. Seach in path if not set. Can also be set to the `pm3_mock.sh` for testing.
|
||||||
@@ -18,3 +22,11 @@ Environment variables:
|
|||||||
- `HOTSPOT_SSID`: Set the hotspot ssid. Defaults to "fwa".
|
- `HOTSPOT_SSID`: Set the hotspot ssid. Defaults to "fwa".
|
||||||
- `HOTSPOT_PW`: Set the hotspot password. Default to "a9LG2kUVrsRRVUo1". Recommended to change.
|
- `HOTSPOT_PW`: Set the hotspot password. Default to "a9LG2kUVrsRRVUo1". Recommended to change.
|
||||||
|
|
||||||
|
Systemd:
|
||||||
|
|
||||||
|
The service is run as a systemd service. There are two service `fwa.service` and `fwa-fail.service`. They read their config
|
||||||
|
from a env file located at `/etc/fwa.env`. See example [env file](service/fwa.env).
|
||||||
|
|
||||||
|
# Building
|
||||||
|
|
||||||
|
Run `make package` to create `.deb` file. [Cross](https://github.com/cross-rs/cross) is used for building the rust code.
|
||||||
|
|||||||
19
service/fwa-fail.service
Normal file
19
service/fwa-fail.service
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Failure state for fwa.service
|
||||||
|
Requires=local-fs.target
|
||||||
|
After=local-fs.target
|
||||||
|
StartLimitIntervalSec=500
|
||||||
|
StartLimitBurst=5
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
ExecStart=/usr/local/bin/fwa --error
|
||||||
|
Restart=on-failure
|
||||||
|
RestartSec=5
|
||||||
|
User=root
|
||||||
|
Group=root
|
||||||
|
WorkingDirectory=/var/lib/fwa
|
||||||
|
EnvironmentFile=/etc/fwa.env
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
5
service/fwa.env
Normal file
5
service/fwa.env
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
PM3_BIN=/usr/share/pm3/pm3
|
||||||
|
LOG_LEVEL=warn
|
||||||
|
HOTSPOT_IDS=578B5DF2;c1532b57
|
||||||
|
HOTSPOT_SSID=fwa
|
||||||
|
HOTSPOT_PW=a9LG2kUVrsRRVUo1
|
||||||
@@ -4,6 +4,7 @@ Requires=local-fs.target
|
|||||||
After=local-fs.target
|
After=local-fs.target
|
||||||
StartLimitIntervalSec=500
|
StartLimitIntervalSec=500
|
||||||
StartLimitBurst=5
|
StartLimitBurst=5
|
||||||
|
OnFailure= fwa-fail.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=simple
|
Type=simple
|
||||||
@@ -13,12 +14,7 @@ RestartSec=5
|
|||||||
User=root
|
User=root
|
||||||
Group=root
|
Group=root
|
||||||
WorkingDirectory=/var/lib/fwa
|
WorkingDirectory=/var/lib/fwa
|
||||||
|
EnvironmentFile=/etc/fwa.env
|
||||||
Environment="PM3_BIN=/usr/local/bin/pm3/pm3"
|
|
||||||
#Environment="LOG_LEVEL=warn"
|
|
||||||
#Environment="HOTSPOT_IDS=578B5DF2;c1532b57"
|
|
||||||
#Environment="HOTSPOT_SSID=fwa"
|
|
||||||
#Environment="HOTSPOT_PW=a9LG2kUVrsRRVUo1"
|
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
@@ -1,61 +0,0 @@
|
|||||||
use rppal::pwm::{Channel, Error, Polarity, Pwm};
|
|
||||||
use std::{future::Future, time::Duration};
|
|
||||||
use tokio::time::sleep;
|
|
||||||
|
|
||||||
pub trait Buzzer {
|
|
||||||
fn beep_ack(&mut self) -> impl Future<Output = Result<(), Error>> + std::marker::Send;
|
|
||||||
|
|
||||||
fn beep_nak(&mut self) -> impl Future<Output = Result<(), Error>> + std::marker::Send;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct GPIOBuzzer {
|
|
||||||
pwm: Pwm,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GPIOBuzzer {
|
|
||||||
/// Create a new GPIOBuzzer instance.
|
|
||||||
/// 0.5 duty cyle
|
|
||||||
/// # Arguments
|
|
||||||
/// * "channel" - PWM channel for buzzer PWM0 = GPIO 12 / PWM1 = GPIO 13
|
|
||||||
pub fn new(channel: Channel) -> Result<Self, Error> {
|
|
||||||
// Enable with dummy values; we'll set frequency/duty in the tone method
|
|
||||||
let duty_cycle: f64 = 0.5;
|
|
||||||
let pwm = Pwm::with_frequency(channel, 1000.0, duty_cycle, Polarity::Normal, true)?;
|
|
||||||
pwm.disable()?; // Start disabled
|
|
||||||
|
|
||||||
Ok(GPIOBuzzer { pwm })
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Play a tone using hardware PWM on supported GPIO pins.
|
|
||||||
///
|
|
||||||
/// # Arguments
|
|
||||||
/// * `frequency` - Frequency in Hz.
|
|
||||||
/// * `duration_ms` - Duration in milliseconds.
|
|
||||||
async fn modulated_tone(&mut self, frequency: f64, duration_ms: u64) -> Result<(), Error> {
|
|
||||||
self.pwm.set_frequency(frequency, 0.5)?; // 50% duty cycle (square wave)
|
|
||||||
self.pwm.enable()?;
|
|
||||||
sleep(Duration::from_millis(duration_ms)).await;
|
|
||||||
self.pwm.disable()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Buzzer for GPIOBuzzer {
|
|
||||||
async fn beep_ack(&mut self) -> Result<(), Error> {
|
|
||||||
let sleep_ms: u64 = 10;
|
|
||||||
|
|
||||||
self.modulated_tone(1200.0, 100).await?;
|
|
||||||
sleep(Duration::from_millis(sleep_ms)).await;
|
|
||||||
self.modulated_tone(2000.0, 50).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn beep_nak(&mut self) -> Result<(), Error> {
|
|
||||||
let sleep_ms: u64 = 100;
|
|
||||||
|
|
||||||
self.modulated_tone(600.0, 150).await?;
|
|
||||||
sleep(Duration::from_millis(sleep_ms)).await;
|
|
||||||
self.modulated_tone(600.0, 150).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
56
src/color.rs
56
src/color.rs
@@ -1,56 +0,0 @@
|
|||||||
use rgb::Rgb;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum NamedColor {
|
|
||||||
Red,
|
|
||||||
Green,
|
|
||||||
Blue,
|
|
||||||
White,
|
|
||||||
Off,
|
|
||||||
Yellow,
|
|
||||||
Cyan,
|
|
||||||
Magenta,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Into<Rgb<u8>> for NamedColor {
|
|
||||||
fn into(self) -> Rgb<u8> {
|
|
||||||
match self {
|
|
||||||
NamedColor::Red => Rgb { r: 150, g: 0, b: 0 },
|
|
||||||
NamedColor::Green => Rgb { r: 0, g: 150, b: 0 },
|
|
||||||
NamedColor::Blue => Rgb { r: 0, g: 0, b: 150 },
|
|
||||||
NamedColor::White => Rgb {
|
|
||||||
r: 255,
|
|
||||||
g: 255,
|
|
||||||
b: 255,
|
|
||||||
},
|
|
||||||
NamedColor::Off => Rgb { r: 0, g: 0, b: 0 },
|
|
||||||
NamedColor::Yellow => Rgb {
|
|
||||||
r: 255,
|
|
||||||
g: 255,
|
|
||||||
b: 0,
|
|
||||||
},
|
|
||||||
NamedColor::Cyan => Rgb {
|
|
||||||
r: 0,
|
|
||||||
g: 255,
|
|
||||||
b: 255,
|
|
||||||
},
|
|
||||||
NamedColor::Magenta => Rgb {
|
|
||||||
r: 255,
|
|
||||||
g: 0,
|
|
||||||
b: 255,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl IntoIterator for NamedColor {
|
|
||||||
type Item = Self;
|
|
||||||
|
|
||||||
type IntoIter = std::vec::IntoIter<Self>;
|
|
||||||
|
|
||||||
fn into_iter(self) -> Self::IntoIter {
|
|
||||||
vec![self].into_iter()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
147
src/feedback.rs
147
src/feedback.rs
@@ -1,47 +1,156 @@
|
|||||||
|
use anyhow::Result;
|
||||||
use log::error;
|
use log::error;
|
||||||
use rppal::pwm::Channel;
|
use rgb::RGB8;
|
||||||
use std::error::Error;
|
use smart_leds::colors::{GREEN, RED};
|
||||||
use tokio::join;
|
use std::time::Duration;
|
||||||
|
use tokio::{join, time::sleep};
|
||||||
|
|
||||||
use crate::{
|
use crate::hardware::{Buzzer, StatusLed};
|
||||||
buzzer::{Buzzer, GPIOBuzzer},
|
|
||||||
led::{SpiLed, StatusLed},
|
#[cfg(not(feature = "mock_pi"))]
|
||||||
};
|
use crate::{hardware::GPIOBuzzer, hardware::SpiLed};
|
||||||
|
|
||||||
#[cfg(feature = "mock_pi")]
|
#[cfg(feature = "mock_pi")]
|
||||||
use crate::mock::{MockBuzzer, MockLed};
|
use crate::hardware::{MockBuzzer, MockLed};
|
||||||
|
|
||||||
const PWM_CHANNEL_BUZZER: Channel = Channel::Pwm0; //PWM0 = GPIO18/Physical pin 12
|
const LED_BLINK_DURATION: Duration = Duration::from_secs(1);
|
||||||
|
|
||||||
|
pub enum DeviceStatus {
|
||||||
|
NotReady,
|
||||||
|
Ready,
|
||||||
|
HotspotEnabled,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DeviceStatus {
|
||||||
|
pub fn color(&self) -> RGB8 {
|
||||||
|
match self {
|
||||||
|
Self::NotReady => RGB8::new(0, 0, 0),
|
||||||
|
Self::Ready => RGB8::new(0, 50, 0),
|
||||||
|
Self::HotspotEnabled => RGB8::new(0, 0, 50),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
pub struct Feedback<B: Buzzer, L: StatusLed> {
|
pub struct Feedback<B: Buzzer, L: StatusLed> {
|
||||||
|
device_status: DeviceStatus,
|
||||||
buzzer: B,
|
buzzer: B,
|
||||||
led: L,
|
led: L,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
|
impl<B: Buzzer, L: StatusLed> Feedback<B, L> {
|
||||||
pub async fn success(&mut self) {
|
pub async fn success(&mut self) {
|
||||||
let (buzzer_result, led_result) =
|
let buzzer_handle = Self::beep_ack(&mut self.buzzer);
|
||||||
join!(self.buzzer.beep_ack(), self.led.turn_green_on_1s());
|
let led_handle = Self::flash_led_for_duration(&mut self.led, GREEN, LED_BLINK_DURATION);
|
||||||
|
let (buzzer_result, _) = join!(buzzer_handle, led_handle);
|
||||||
|
|
||||||
buzzer_result.unwrap_or_else(|err| {
|
buzzer_result.unwrap_or_else(|err| {
|
||||||
error!("Failed to buzz: {err}");
|
error!("Failed to buzz: {err}");
|
||||||
});
|
});
|
||||||
|
|
||||||
led_result.unwrap_or_else(|err| {
|
let _ = self.led_to_status();
|
||||||
error!("Failed to set LED: {err}");
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn failure(&mut self) {
|
pub async fn failure(&mut self) {
|
||||||
let (buzzer_result, led_result) = join!(self.buzzer.beep_nak(), self.led.turn_red_on_1s());
|
let buzzer_handle = Self::beep_nak(&mut self.buzzer);
|
||||||
|
let led_handle = Self::flash_led_for_duration(&mut self.led, RED, LED_BLINK_DURATION);
|
||||||
|
|
||||||
|
let (buzzer_result, _) = join!(buzzer_handle, led_handle);
|
||||||
|
|
||||||
|
buzzer_result.unwrap_or_else(|err| {
|
||||||
|
error!("Failed to buzz: {err}");
|
||||||
|
});
|
||||||
|
|
||||||
|
let _ = self.led_to_status();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn activate_error_state(&mut self) -> Result<()> {
|
||||||
|
self.led.turn_on(RED)?;
|
||||||
|
Self::beep_nak(&mut self.buzzer).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn startup(&mut self){
|
||||||
|
self.device_status = DeviceStatus::Ready;
|
||||||
|
|
||||||
|
let led_handle = Self::flash_led_for_duration(&mut self.led, GREEN, Duration::from_secs(1));
|
||||||
|
let buzzer_handle = Self::beep_startup(&mut self.buzzer);
|
||||||
|
|
||||||
|
let (buzzer_result, led_result) = join!(buzzer_handle, led_handle);
|
||||||
|
|
||||||
buzzer_result.unwrap_or_else(|err| {
|
buzzer_result.unwrap_or_else(|err| {
|
||||||
error!("Failed to buzz: {err}");
|
error!("Failed to buzz: {err}");
|
||||||
});
|
});
|
||||||
|
|
||||||
led_result.unwrap_or_else(|err| {
|
led_result.unwrap_or_else(|err| {
|
||||||
error!("Failed to set LED: {err}");
|
error!("Failed to blink led: {err}");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let _ = self.led_to_status();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_device_status(&mut self, status: DeviceStatus){
|
||||||
|
self.device_status = status;
|
||||||
|
let _ = self.led_to_status();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn led_to_status(&mut self) -> Result<()> {
|
||||||
|
self.led.turn_on(self.device_status.color())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn flash_led_for_duration(led: &mut L, color: RGB8, duration: Duration) -> Result<()> {
|
||||||
|
led.turn_on(color)?;
|
||||||
|
|
||||||
|
sleep(duration).await;
|
||||||
|
|
||||||
|
led.turn_off()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn beep_ack(buzzer: &mut B) -> Result<()> {
|
||||||
|
buzzer
|
||||||
|
.modulated_tone(1200.0, Duration::from_millis(100))
|
||||||
|
.await?;
|
||||||
|
sleep(Duration::from_millis(10)).await;
|
||||||
|
buzzer
|
||||||
|
.modulated_tone(2000.0, Duration::from_millis(50))
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn beep_nak(buzzer: &mut B) -> Result<()> {
|
||||||
|
buzzer
|
||||||
|
.modulated_tone(600.0, Duration::from_millis(150))
|
||||||
|
.await?;
|
||||||
|
sleep(Duration::from_millis(100)).await;
|
||||||
|
buzzer
|
||||||
|
.modulated_tone(600.0, Duration::from_millis(150))
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn beep_startup(buzzer: &mut B) -> Result<()> {
|
||||||
|
buzzer
|
||||||
|
.modulated_tone(523.0, Duration::from_millis(150))
|
||||||
|
.await?;
|
||||||
|
buzzer
|
||||||
|
.modulated_tone(659.0, Duration::from_millis(150))
|
||||||
|
.await?;
|
||||||
|
buzzer
|
||||||
|
.modulated_tone(784.0, Duration::from_millis(150))
|
||||||
|
.await?;
|
||||||
|
buzzer
|
||||||
|
.modulated_tone(1046.0, Duration::from_millis(200))
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
sleep(Duration::from_millis(100)).await;
|
||||||
|
|
||||||
|
buzzer
|
||||||
|
.modulated_tone(784.0, Duration::from_millis(100))
|
||||||
|
.await?;
|
||||||
|
buzzer
|
||||||
|
.modulated_tone(880.0, Duration::from_millis(200))
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,10 +160,11 @@ pub type FeedbackImpl = Feedback<MockBuzzer, MockLed>;
|
|||||||
pub type FeedbackImpl = Feedback<GPIOBuzzer, SpiLed>;
|
pub type FeedbackImpl = Feedback<GPIOBuzzer, SpiLed>;
|
||||||
|
|
||||||
impl FeedbackImpl {
|
impl FeedbackImpl {
|
||||||
pub fn new() -> Result<Self, Box<dyn Error>> {
|
pub fn new() -> Result<Self> {
|
||||||
#[cfg(feature = "mock_pi")]
|
#[cfg(feature = "mock_pi")]
|
||||||
{
|
{
|
||||||
Ok(Feedback {
|
Ok(Feedback {
|
||||||
|
device_status: DeviceStatus::NotReady,
|
||||||
buzzer: MockBuzzer {},
|
buzzer: MockBuzzer {},
|
||||||
led: MockLed {},
|
led: MockLed {},
|
||||||
})
|
})
|
||||||
@@ -62,7 +172,8 @@ impl FeedbackImpl {
|
|||||||
#[cfg(not(feature = "mock_pi"))]
|
#[cfg(not(feature = "mock_pi"))]
|
||||||
{
|
{
|
||||||
Ok(Feedback {
|
Ok(Feedback {
|
||||||
buzzer: GPIOBuzzer::new(PWM_CHANNEL_BUZZER)?,
|
device_status: DeviceStatus::NotReady,
|
||||||
|
buzzer: GPIOBuzzer::new_default()?,
|
||||||
led: SpiLed::new()?,
|
led: SpiLed::new()?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
37
src/hardware/gpio_buzzer.rs
Normal file
37
src/hardware/gpio_buzzer.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use rppal::pwm::{Channel, Polarity, Pwm};
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::time::sleep;
|
||||||
|
|
||||||
|
use crate::hardware::Buzzer;
|
||||||
|
|
||||||
|
const DEFAULT_PWM_CHANNEL_BUZZER: Channel = Channel::Pwm0; //PWM0 = GPIO18/Physical pin 12
|
||||||
|
|
||||||
|
pub struct GPIOBuzzer {
|
||||||
|
pwm: Pwm,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GPIOBuzzer {
|
||||||
|
pub fn new_from_channel(channel: Channel) -> Result<Self, rppal::pwm::Error> {
|
||||||
|
// Enable with dummy values; we'll set frequency/duty in the tone method
|
||||||
|
let duty_cycle: f64 = 0.5;
|
||||||
|
let pwm = Pwm::with_frequency(channel, 1000.0, duty_cycle, Polarity::Normal, true)?;
|
||||||
|
pwm.disable()?;
|
||||||
|
|
||||||
|
Ok(GPIOBuzzer { pwm })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_default() -> Result<Self, rppal::pwm::Error> {
|
||||||
|
Self::new_from_channel(DEFAULT_PWM_CHANNEL_BUZZER)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Buzzer for GPIOBuzzer {
|
||||||
|
async fn modulated_tone(&mut self, frequency_hz: f64, duration: Duration) -> Result<()> {
|
||||||
|
self.pwm.set_frequency(frequency_hz, 0.5)?; // 50% duty cycle (square wave)
|
||||||
|
self.pwm.enable()?;
|
||||||
|
sleep(duration).await;
|
||||||
|
self.pwm.disable()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,56 +1,15 @@
|
|||||||
use log::{error, trace, warn};
|
use anyhow::{Result, anyhow};
|
||||||
use std::{
|
use log::{trace, warn};
|
||||||
env,
|
use std::env;
|
||||||
fmt::{self},
|
|
||||||
process::Output,
|
|
||||||
};
|
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
|
|
||||||
|
use crate::hardware::Hotspot;
|
||||||
|
|
||||||
const SSID: &str = "fwa";
|
const SSID: &str = "fwa";
|
||||||
const CON_NAME: &str = "fwa-hotspot";
|
const CON_NAME: &str = "fwa-hotspot";
|
||||||
const PASSWORD: &str = "a9LG2kUVrsRRVUo1";
|
const PASSWORD: &str = "a9LG2kUVrsRRVUo1";
|
||||||
const IPV4_ADDRES: &str = "192.168.4.1/24";
|
const IPV4_ADDRES: &str = "192.168.4.1/24";
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum HotspotError {
|
|
||||||
IoError(std::io::Error),
|
|
||||||
NonZeroExit(Output),
|
|
||||||
PasswordToShort,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for HotspotError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
match self {
|
|
||||||
HotspotError::IoError(err) => {
|
|
||||||
write!(f, "Failed to run hotspot command. I/O error: {err}")
|
|
||||||
}
|
|
||||||
HotspotError::NonZeroExit(output) => {
|
|
||||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
||||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
|
||||||
write!(
|
|
||||||
f,
|
|
||||||
"Failed to run hotspot command.\nStdout: {stdout}\nStderr: {stderr}",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
HotspotError::PasswordToShort => {
|
|
||||||
write!(f, "The password must be at leat 8 characters long")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for HotspotError {}
|
|
||||||
|
|
||||||
pub trait Hotspot {
|
|
||||||
fn enable_hotspot(
|
|
||||||
&self,
|
|
||||||
) -> impl std::future::Future<Output = Result<(), HotspotError>> + std::marker::Send;
|
|
||||||
|
|
||||||
fn disable_hotspot(
|
|
||||||
&self,
|
|
||||||
) -> impl std::future::Future<Output = Result<(), HotspotError>> + std::marker::Send;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// NetworkManager Hotspot
|
/// NetworkManager Hotspot
|
||||||
pub struct NMHotspot {
|
pub struct NMHotspot {
|
||||||
ssid: String,
|
ssid: String,
|
||||||
@@ -60,7 +19,7 @@ pub struct NMHotspot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl NMHotspot {
|
impl NMHotspot {
|
||||||
pub fn new_from_env() -> Result<Self, HotspotError> {
|
pub fn new_from_env() -> Result<Self> {
|
||||||
let ssid = env::var("HOTSPOT_SSID").unwrap_or(SSID.to_owned());
|
let ssid = env::var("HOTSPOT_SSID").unwrap_or(SSID.to_owned());
|
||||||
let password = env::var("HOTSPOT_PW").unwrap_or_else(|_| {
|
let password = env::var("HOTSPOT_PW").unwrap_or_else(|_| {
|
||||||
warn!("HOTSPOT_PW not set. Using default password");
|
warn!("HOTSPOT_PW not set. Using default password");
|
||||||
@@ -68,8 +27,7 @@ impl NMHotspot {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if password.len() < 8 {
|
if password.len() < 8 {
|
||||||
error!("Hotspot PW is to short");
|
return Err(anyhow!("Hotspot password to short"));
|
||||||
return Err(HotspotError::PasswordToShort);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(NMHotspot {
|
Ok(NMHotspot {
|
||||||
@@ -80,7 +38,7 @@ impl NMHotspot {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_hotspot(&self) -> Result<(), HotspotError> {
|
async fn create_hotspot(&self) -> Result<()> {
|
||||||
let cmd = Command::new("nmcli")
|
let cmd = Command::new("nmcli")
|
||||||
.args(["device", "wifi", "hotspot"])
|
.args(["device", "wifi", "hotspot"])
|
||||||
.arg("con-name")
|
.arg("con-name")
|
||||||
@@ -90,14 +48,13 @@ impl NMHotspot {
|
|||||||
.arg("password")
|
.arg("password")
|
||||||
.arg(&self.password)
|
.arg(&self.password)
|
||||||
.output()
|
.output()
|
||||||
.await
|
.await?;
|
||||||
.map_err(HotspotError::IoError)?;
|
|
||||||
|
|
||||||
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
||||||
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
||||||
|
|
||||||
if !cmd.status.success() {
|
if !cmd.status.success() {
|
||||||
return Err(HotspotError::NonZeroExit(cmd));
|
return Err(anyhow!("nmcli command had non-zero exit code"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let cmd = Command::new("nmcli")
|
let cmd = Command::new("nmcli")
|
||||||
@@ -109,24 +66,22 @@ impl NMHotspot {
|
|||||||
.arg("ipv4.addresses")
|
.arg("ipv4.addresses")
|
||||||
.arg(&self.ipv4)
|
.arg(&self.ipv4)
|
||||||
.output()
|
.output()
|
||||||
.await
|
.await?;
|
||||||
.map_err(HotspotError::IoError)?;
|
|
||||||
|
|
||||||
if !cmd.status.success() {
|
if !cmd.status.success() {
|
||||||
return Err(HotspotError::NonZeroExit(cmd));
|
return Err(anyhow!("nmcli command had non-zero exit code"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks if the connection already exists
|
/// Checks if the connection already exists
|
||||||
async fn exists(&self) -> Result<bool, HotspotError> {
|
async fn exists(&self) -> Result<bool> {
|
||||||
let cmd = Command::new("nmcli")
|
let cmd = Command::new("nmcli")
|
||||||
.args(["connection", "show"])
|
.args(["connection", "show"])
|
||||||
.arg(&self.con_name)
|
.arg(&self.con_name)
|
||||||
.output()
|
.output()
|
||||||
.await
|
.await?;
|
||||||
.map_err(HotspotError::IoError)?;
|
|
||||||
|
|
||||||
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
||||||
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
||||||
@@ -136,7 +91,7 @@ impl NMHotspot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Hotspot for NMHotspot {
|
impl Hotspot for NMHotspot {
|
||||||
async fn enable_hotspot(&self) -> Result<(), HotspotError> {
|
async fn enable_hotspot(&self) -> Result<()> {
|
||||||
if !self.exists().await? {
|
if !self.exists().await? {
|
||||||
self.create_hotspot().await?;
|
self.create_hotspot().await?;
|
||||||
}
|
}
|
||||||
@@ -145,32 +100,30 @@ impl Hotspot for NMHotspot {
|
|||||||
.args(["connection", "up"])
|
.args(["connection", "up"])
|
||||||
.arg(&self.con_name)
|
.arg(&self.con_name)
|
||||||
.output()
|
.output()
|
||||||
.await
|
.await?;
|
||||||
.map_err(HotspotError::IoError)?;
|
|
||||||
|
|
||||||
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
||||||
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
||||||
|
|
||||||
if !cmd.status.success() {
|
if !cmd.status.success() {
|
||||||
return Err(HotspotError::NonZeroExit(cmd));
|
return Err(anyhow!("nmcli command had non-zero exit code"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn disable_hotspot(&self) -> Result<(), HotspotError> {
|
async fn disable_hotspot(&self) -> Result<()> {
|
||||||
let cmd = Command::new("nmcli")
|
let cmd = Command::new("nmcli")
|
||||||
.args(["connection", "down"])
|
.args(["connection", "down"])
|
||||||
.arg(&self.con_name)
|
.arg(&self.con_name)
|
||||||
.output()
|
.output()
|
||||||
.await
|
.await?;
|
||||||
.map_err(HotspotError::IoError)?;
|
|
||||||
|
|
||||||
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
trace!("nmcli (std): {}", String::from_utf8_lossy(&cmd.stdout));
|
||||||
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
trace!("nmcli (err): {}", String::from_utf8_lossy(&cmd.stderr));
|
||||||
|
|
||||||
if !cmd.status.success() {
|
if !cmd.status.success() {
|
||||||
return Err(HotspotError::NonZeroExit(cmd));
|
return Err(anyhow!("nmcli command had non-zero exit code"));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
44
src/hardware/mock.rs
Normal file
44
src/hardware/mock.rs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use log::debug;
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::time::sleep;
|
||||||
|
|
||||||
|
use crate::hardware::{Buzzer, Hotspot, StatusLed};
|
||||||
|
|
||||||
|
pub struct MockBuzzer {}
|
||||||
|
|
||||||
|
impl Buzzer for MockBuzzer {
|
||||||
|
async fn modulated_tone(&mut self, frequency_hz: f64, duration: Duration) -> Result<()> {
|
||||||
|
debug!("MockBuzzer: modulte tone: {frequency_hz} Hz");
|
||||||
|
sleep(duration).await;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MockLed {}
|
||||||
|
|
||||||
|
impl StatusLed for MockLed {
|
||||||
|
fn turn_off(&mut self) -> Result<()> {
|
||||||
|
debug!("Turn mock LED off");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn turn_on(&mut self, color: rgb::RGB8) -> Result<()> {
|
||||||
|
debug!("Turn mock LED on to: {color}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MockHotspot {}
|
||||||
|
|
||||||
|
impl Hotspot for MockHotspot {
|
||||||
|
async fn enable_hotspot(&self) -> Result<()> {
|
||||||
|
debug!("Mockhotspot: Enable hotspot");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn disable_hotspot(&self) -> Result<()> {
|
||||||
|
debug!("Mockhotspot: Disable hotspot");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
45
src/hardware/mod.rs
Normal file
45
src/hardware/mod.rs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
mod gpio_buzzer;
|
||||||
|
mod hotspot;
|
||||||
|
mod mock;
|
||||||
|
mod spi_led;
|
||||||
|
|
||||||
|
pub use gpio_buzzer::GPIOBuzzer;
|
||||||
|
pub use mock::{MockBuzzer, MockHotspot, MockLed};
|
||||||
|
pub use spi_led::SpiLed;
|
||||||
|
|
||||||
|
pub trait StatusLed {
|
||||||
|
fn turn_off(&mut self) -> Result<()>;
|
||||||
|
|
||||||
|
fn turn_on(&mut self, color: rgb::RGB8) -> Result<()>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Buzzer {
|
||||||
|
fn modulated_tone(
|
||||||
|
&mut self,
|
||||||
|
frequency_hz: f64,
|
||||||
|
duration: Duration,
|
||||||
|
) -> impl Future<Output = Result<()>> + std::marker::Send;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Hotspot {
|
||||||
|
fn enable_hotspot(&self) -> impl std::future::Future<Output = Result<()>> + std::marker::Send;
|
||||||
|
|
||||||
|
fn disable_hotspot(&self) -> impl std::future::Future<Output = Result<()>> + std::marker::Send;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a struct to manage the hotspot
|
||||||
|
/// Respects the `mock_pi` flag.
|
||||||
|
pub fn create_hotspot() -> Result<impl Hotspot> {
|
||||||
|
#[cfg(feature = "mock_pi")]
|
||||||
|
{
|
||||||
|
Ok(mock::MockHotspot {})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "mock_pi"))]
|
||||||
|
{
|
||||||
|
hotspot::NMHotspot::new_from_env()
|
||||||
|
}
|
||||||
|
}
|
||||||
33
src/hardware/spi_led.rs
Normal file
33
src/hardware/spi_led.rs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
use anyhow::Result;
|
||||||
|
use rppal::spi::{Bus, Mode, SlaveSelect, Spi};
|
||||||
|
use smart_leds::SmartLedsWrite;
|
||||||
|
use ws2812_spi::Ws2812;
|
||||||
|
|
||||||
|
use crate::hardware::StatusLed;
|
||||||
|
|
||||||
|
const SPI_CLOCK_SPEED: u32 = 3_800_000;
|
||||||
|
|
||||||
|
pub struct SpiLed {
|
||||||
|
controller: Ws2812<Spi>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SpiLed {
|
||||||
|
pub fn new() -> Result<Self, rppal::spi::Error> {
|
||||||
|
let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, SPI_CLOCK_SPEED, Mode::Mode0)?;
|
||||||
|
let controller = Ws2812::new(spi);
|
||||||
|
Ok(SpiLed { controller })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StatusLed for SpiLed {
|
||||||
|
fn turn_off(&mut self) -> Result<()> {
|
||||||
|
self.controller
|
||||||
|
.write(vec![rgb::RGB8::new(0, 0, 0)].into_iter())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn turn_on(&mut self, color: rgb::RGB8) -> Result<()> {
|
||||||
|
self.controller.write(vec![color].into_iter())?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
53
src/led.rs
53
src/led.rs
@@ -1,53 +0,0 @@
|
|||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use rppal::spi::{Bus, Error, Mode, SlaveSelect, Spi};
|
|
||||||
use smart_leds::SmartLedsWrite;
|
|
||||||
use tokio::time::sleep;
|
|
||||||
use ws2812_spi::Ws2812;
|
|
||||||
|
|
||||||
use crate::color::NamedColor;
|
|
||||||
|
|
||||||
const STATUS_DURATION: Duration = Duration::from_secs(1); // 1s sleep for all status led signals
|
|
||||||
|
|
||||||
pub trait StatusLed {
|
|
||||||
fn turn_green_on_1s(
|
|
||||||
&mut self,
|
|
||||||
) -> impl std::future::Future<Output = Result<(), Error>> + std::marker::Send;
|
|
||||||
|
|
||||||
fn turn_red_on_1s(
|
|
||||||
&mut self,
|
|
||||||
) -> impl std::future::Future<Output = Result<(), Error>> + std::marker::Send;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct SpiLed {
|
|
||||||
controller: Ws2812<Spi>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SpiLed {
|
|
||||||
pub fn new() -> Result<Self, Error> {
|
|
||||||
let spi = Spi::new(Bus::Spi0, SlaveSelect::Ss0, 3_800_000, Mode::Mode0)?;
|
|
||||||
let controller = Ws2812::new(spi);
|
|
||||||
Ok(SpiLed { controller })
|
|
||||||
}
|
|
||||||
|
|
||||||
fn turn_off(&mut self) -> Result<(), Error> {
|
|
||||||
self.controller.write(NamedColor::Off.into_iter())?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl StatusLed for SpiLed {
|
|
||||||
async fn turn_green_on_1s(&mut self) -> Result<(), Error> {
|
|
||||||
self.controller.write(NamedColor::Green.into_iter())?;
|
|
||||||
sleep(STATUS_DURATION).await;
|
|
||||||
self.turn_off()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn turn_red_on_1s(&mut self) -> Result<(), Error> {
|
|
||||||
self.controller.write(NamedColor::Red.into_iter())?;
|
|
||||||
sleep(STATUS_DURATION).await;
|
|
||||||
self.turn_off()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
125
src/main.rs
125
src/main.rs
@@ -1,13 +1,17 @@
|
|||||||
use activity_fairing::{ActivityNotifier, spawn_idle_watcher};
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
use feedback::{Feedback, FeedbackImpl};
|
use feedback::{Feedback, FeedbackImpl};
|
||||||
use hotspot::{Hotspot, HotspotError, NMHotspot};
|
|
||||||
use id_store::IDStore;
|
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
use pm3::run_pm3;
|
use std::{
|
||||||
use std::{env, error::Error, sync::Arc, time::Duration};
|
env::{self, args},
|
||||||
|
sync::Arc,
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
use tally_id::TallyID;
|
use tally_id::TallyID;
|
||||||
use tokio::{
|
use tokio::{
|
||||||
fs,
|
fs,
|
||||||
|
signal::unix::{SignalKind, signal},
|
||||||
sync::{
|
sync::{
|
||||||
Mutex,
|
Mutex,
|
||||||
broadcast::{self, Receiver, Sender},
|
broadcast::{self, Receiver, Sender},
|
||||||
@@ -16,53 +20,37 @@ use tokio::{
|
|||||||
};
|
};
|
||||||
use webserver::start_webserver;
|
use webserver::start_webserver;
|
||||||
|
|
||||||
#[cfg(feature = "mock_pi")]
|
use crate::{hardware::{create_hotspot, Hotspot}, pm3::run_pm3, store::IDStore, webserver::{spawn_idle_watcher, ActivityNotifier}};
|
||||||
use mock::MockHotspot;
|
|
||||||
|
|
||||||
mod activity_fairing;
|
|
||||||
mod buzzer;
|
|
||||||
mod color;
|
|
||||||
mod feedback;
|
mod feedback;
|
||||||
mod hotspot;
|
mod hardware;
|
||||||
mod id_mapping;
|
|
||||||
mod id_store;
|
|
||||||
mod led;
|
|
||||||
mod logger;
|
|
||||||
mod mock;
|
|
||||||
mod parser;
|
|
||||||
mod pm3;
|
mod pm3;
|
||||||
|
mod logger;
|
||||||
mod tally_id;
|
mod tally_id;
|
||||||
mod webserver;
|
mod webserver;
|
||||||
|
mod store;
|
||||||
|
|
||||||
const STORE_PATH: &str = "./data.json";
|
const STORE_PATH: &str = "./data.json";
|
||||||
|
|
||||||
/// Create a struct to manage the hotspot
|
|
||||||
/// Respects the `mock_pi` flag.
|
|
||||||
fn create_hotspot() -> Result<impl Hotspot, HotspotError> {
|
|
||||||
#[cfg(feature = "mock_pi")]
|
|
||||||
{
|
|
||||||
Ok(MockHotspot {})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "mock_pi"))]
|
|
||||||
{
|
|
||||||
NMHotspot::new_from_env()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn run_webserver<H>(
|
async fn run_webserver<H>(
|
||||||
store: Arc<Mutex<IDStore>>,
|
store: Arc<Mutex<IDStore>>,
|
||||||
id_channel: Sender<String>,
|
id_channel: Sender<String>,
|
||||||
hotspot: Arc<Mutex<H>>,
|
hotspot: Arc<Mutex<H>>,
|
||||||
) -> Result<(), Box<dyn Error>>
|
user_feedback: Arc<Mutex<FeedbackImpl>>,
|
||||||
|
) -> Result<()>
|
||||||
where
|
where
|
||||||
H: Hotspot + Send + Sync + 'static,
|
H: Hotspot + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
let activity_channel = spawn_idle_watcher(Duration::from_secs(60 * 30), move || {
|
let activity_channel = spawn_idle_watcher(Duration::from_secs(60 * 30), move || {
|
||||||
info!("No activity on webserver. Disabling hotspot");
|
info!("No activity on webserver. Disabling hotspot");
|
||||||
let cloned_hotspot = hotspot.clone();
|
let cloned_hotspot = hotspot.clone();
|
||||||
|
let cloned_user_feedback = user_feedback.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let _ = cloned_hotspot.lock().await.disable_hotspot().await;
|
let _ = cloned_hotspot.lock().await.disable_hotspot().await;
|
||||||
|
cloned_user_feedback
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.set_device_status(feedback::DeviceStatus::Ready);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -71,10 +59,11 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
start_webserver(store, id_channel, notifier).await?;
|
start_webserver(store, id_channel, notifier).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn load_or_create_store() -> Result<IDStore, Box<dyn Error>> {
|
async fn load_or_create_store() -> Result<IDStore> {
|
||||||
if fs::try_exists(STORE_PATH).await? {
|
if fs::try_exists(STORE_PATH).await? {
|
||||||
info!("Loading data from file");
|
info!("Loading data from file");
|
||||||
IDStore::new_from_json(STORE_PATH).await
|
IDStore::new_from_json(STORE_PATH).await
|
||||||
@@ -103,32 +92,38 @@ async fn handle_ids_loop(
|
|||||||
hotspot_enable_ids: Vec<TallyID>,
|
hotspot_enable_ids: Vec<TallyID>,
|
||||||
id_store: Arc<Mutex<IDStore>>,
|
id_store: Arc<Mutex<IDStore>>,
|
||||||
hotspot: Arc<Mutex<impl Hotspot>>,
|
hotspot: Arc<Mutex<impl Hotspot>>,
|
||||||
mut user_feedback: FeedbackImpl,
|
user_feedback: Arc<Mutex<FeedbackImpl>>,
|
||||||
) -> Result<(), Box<dyn Error>> {
|
) -> Result<()> {
|
||||||
while let Ok(tally_id_string) = id_channel.recv().await {
|
while let Ok(tally_id_string) = id_channel.recv().await {
|
||||||
let tally_id = TallyID(tally_id_string);
|
let tally_id = TallyID(tally_id_string);
|
||||||
|
|
||||||
if hotspot_enable_ids.contains(&tally_id) {
|
if hotspot_enable_ids.contains(&tally_id) {
|
||||||
info!("Enableing hotspot");
|
info!("Enableing hotspot");
|
||||||
hotspot
|
let hotspot_enable_result = hotspot.lock().await.enable_hotspot().await;
|
||||||
.lock()
|
|
||||||
.await
|
match hotspot_enable_result {
|
||||||
.enable_hotspot()
|
Ok(_) => {
|
||||||
.await
|
user_feedback
|
||||||
.unwrap_or_else(|err| {
|
.lock()
|
||||||
error!("Hotspot: {err}");
|
.await
|
||||||
});
|
.set_device_status(feedback::DeviceStatus::HotspotEnabled);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Hotspot: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Should the ID be added anyway or ignored ?
|
// TODO: Should the ID be added anyway or ignored ?
|
||||||
}
|
}
|
||||||
|
|
||||||
if id_store.lock().await.add_id(tally_id) {
|
if id_store.lock().await.add_id(tally_id) {
|
||||||
info!("Added new id to current day");
|
info!("Added new id to current day");
|
||||||
|
|
||||||
user_feedback.success().await;
|
user_feedback.lock().await.success().await;
|
||||||
|
|
||||||
if let Err(e) = id_store.lock().await.export_json(STORE_PATH).await {
|
if let Err(e) = id_store.lock().await.export_json(STORE_PATH).await {
|
||||||
error!("Failed to save id store to file: {e}");
|
error!("Failed to save id store to file: {e}");
|
||||||
user_feedback.failure().await;
|
user_feedback.lock().await.failure().await;
|
||||||
// TODO: How to handle a failure to save ?
|
// TODO: How to handle a failure to save ?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,36 +132,60 @@ async fn handle_ids_loop(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn enter_error_state(feedback: Arc<Mutex<FeedbackImpl>>, hotspot: Arc<Mutex<impl Hotspot>>) {
|
||||||
|
let _ = feedback.lock().await.activate_error_state().await;
|
||||||
|
let _ = hotspot.lock().await.enable_hotspot().await;
|
||||||
|
|
||||||
|
let mut sigterm = signal(SignalKind::terminate()).unwrap();
|
||||||
|
sigterm.recv().await;
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<()> {
|
||||||
logger::setup_logger();
|
logger::setup_logger();
|
||||||
|
|
||||||
info!("Starting application");
|
info!("Starting application");
|
||||||
|
|
||||||
|
let user_feedback = Arc::new(Mutex::new(Feedback::new()?));
|
||||||
|
let hotspot = Arc::new(Mutex::new(create_hotspot()?));
|
||||||
|
|
||||||
|
let error_flag_set = args().any(|e| e == "--error" || e == "-e");
|
||||||
|
if error_flag_set {
|
||||||
|
error!("Error flag set. Entering error state");
|
||||||
|
enter_error_state(user_feedback.clone(), hotspot).await;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let store: Arc<Mutex<IDStore>> = Arc::new(Mutex::new(load_or_create_store().await?));
|
||||||
|
let hotspot_enable_ids = get_hotspot_enable_ids();
|
||||||
|
|
||||||
let (tx, rx) = broadcast::channel::<String>(32);
|
let (tx, rx) = broadcast::channel::<String>(32);
|
||||||
let sse_tx = tx.clone();
|
let sse_tx = tx.clone();
|
||||||
|
|
||||||
let store: Arc<Mutex<IDStore>> = Arc::new(Mutex::new(load_or_create_store().await?));
|
|
||||||
let user_feedback = Feedback::new()?;
|
|
||||||
let hotspot = Arc::new(Mutex::new(create_hotspot()?));
|
|
||||||
let hotspot_enable_ids = get_hotspot_enable_ids();
|
|
||||||
|
|
||||||
let pm3_handle = run_pm3(tx);
|
let pm3_handle = run_pm3(tx);
|
||||||
|
|
||||||
|
user_feedback.lock().await.startup().await;
|
||||||
|
|
||||||
let loop_handle = handle_ids_loop(
|
let loop_handle = handle_ids_loop(
|
||||||
rx,
|
rx,
|
||||||
hotspot_enable_ids,
|
hotspot_enable_ids,
|
||||||
store.clone(),
|
store.clone(),
|
||||||
hotspot.clone(),
|
hotspot.clone(),
|
||||||
user_feedback,
|
user_feedback.clone(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let webserver_handle = run_webserver(store.clone(), sse_tx, hotspot.clone());
|
let webserver_handle = run_webserver(
|
||||||
|
store.clone(),
|
||||||
|
sse_tx,
|
||||||
|
hotspot.clone(),
|
||||||
|
user_feedback.clone(),
|
||||||
|
);
|
||||||
|
|
||||||
let run_result = try_join!(pm3_handle, loop_handle, webserver_handle);
|
let run_result = try_join!(pm3_handle, loop_handle, webserver_handle);
|
||||||
|
|
||||||
if let Err(e) = run_result {
|
if let Err(e) = run_result {
|
||||||
error!("Failed to run application: {e}");
|
error!("Failed to run application: {e}");
|
||||||
|
return Err(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
45
src/mock.rs
45
src/mock.rs
@@ -1,45 +0,0 @@
|
|||||||
use log::debug;
|
|
||||||
|
|
||||||
use crate::{buzzer::Buzzer, hotspot::Hotspot, led::StatusLed};
|
|
||||||
|
|
||||||
pub struct MockBuzzer {}
|
|
||||||
|
|
||||||
impl Buzzer for MockBuzzer {
|
|
||||||
async fn beep_ack(&mut self) -> Result<(), rppal::pwm::Error> {
|
|
||||||
debug!("Mockbuzzer: ACK");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn beep_nak(&mut self) -> Result<(), rppal::pwm::Error> {
|
|
||||||
debug!("Mockbuzzer: NAK");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct MockLed {}
|
|
||||||
|
|
||||||
impl StatusLed for MockLed {
|
|
||||||
async fn turn_green_on_1s(&mut self) -> Result<(), rppal::spi::Error> {
|
|
||||||
debug!("Mockled: Turn LED green for 1 sec");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn turn_red_on_1s(&mut self) -> Result<(), rppal::spi::Error> {
|
|
||||||
debug!("Mockled: Turn LED red for 1 sec");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct MockHotspot {}
|
|
||||||
|
|
||||||
impl Hotspot for MockHotspot {
|
|
||||||
async fn enable_hotspot(&self) -> Result<(), crate::hotspot::HotspotError> {
|
|
||||||
debug!("Mockhotspot: Enable hotspot");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn disable_hotspot(&self) -> Result<(), crate::hotspot::HotspotError> {
|
|
||||||
debug!("Mockhotspot: Disable hotspot");
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
4
src/pm3/mod.rs
Normal file
4
src/pm3/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
mod runner;
|
||||||
|
mod parser;
|
||||||
|
|
||||||
|
pub use runner::run_pm3;
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
|
use anyhow::{Result, anyhow};
|
||||||
use log::{debug, info, trace, warn};
|
use log::{debug, info, trace, warn};
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::error::Error;
|
|
||||||
use std::process::Stdio;
|
use std::process::Stdio;
|
||||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
@@ -11,7 +11,7 @@ use tokio::sync::broadcast;
|
|||||||
/// Runs the pm3 binary and monitors it's output
|
/// Runs the pm3 binary and monitors it's output
|
||||||
/// The pm3 binary is ether set in the env var PM3_BIN or found in the path
|
/// The pm3 binary is ether set in the env var PM3_BIN or found in the path
|
||||||
/// The ouput is parsed and send via the `tx` channel
|
/// The ouput is parsed and send via the `tx` channel
|
||||||
pub async fn run_pm3(tx: broadcast::Sender<String>) -> Result<(), Box<dyn Error>> {
|
pub async fn run_pm3(tx: broadcast::Sender<String>) -> Result<()> {
|
||||||
kill_orphans().await;
|
kill_orphans().await;
|
||||||
|
|
||||||
let pm3_path = match env::var("PM3_BIN") {
|
let pm3_path = match env::var("PM3_BIN") {
|
||||||
@@ -32,8 +32,8 @@ pub async fn run_pm3(tx: broadcast::Sender<String>) -> Result<(), Box<dyn Error>
|
|||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.spawn()?;
|
.spawn()?;
|
||||||
|
|
||||||
let stdout = cmd.stdout.take().ok_or("Failed to get stdout")?;
|
let stdout = cmd.stdout.take().ok_or(anyhow!("Failed to get stdout"))?;
|
||||||
let mut stdin = cmd.stdin.take().ok_or("Failed to get stdin")?;
|
let mut stdin = cmd.stdin.take().ok_or(anyhow!("Failed to get stdin"))?;
|
||||||
|
|
||||||
let mut reader = BufReader::new(stdout).lines();
|
let mut reader = BufReader::new(stdout).lines();
|
||||||
|
|
||||||
@@ -62,10 +62,15 @@ pub async fn run_pm3(tx: broadcast::Sender<String>) -> Result<(), Box<dyn Error>
|
|||||||
};
|
};
|
||||||
|
|
||||||
let status = cmd.wait().await?;
|
let status = cmd.wait().await?;
|
||||||
if status.success() {
|
|
||||||
|
// We use the exit code here because status.success() is false if the child was terminated by a
|
||||||
|
// signal
|
||||||
|
let code = status.code().unwrap_or(0);
|
||||||
|
|
||||||
|
if code == 0 {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err("PM3 exited with a non-zero exit code".into())
|
Err(anyhow!("PM3 exited with a non-zero exit code: {code}"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
|
use anyhow::{Result, anyhow};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::collections::{HashMap, HashSet};
|
||||||
collections::{HashMap, HashSet},
|
|
||||||
error::Error,
|
|
||||||
};
|
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
|
||||||
use crate::{id_mapping::IDMapping, tally_id::TallyID};
|
use crate::{store::IDMapping, tally_id::TallyID};
|
||||||
|
|
||||||
/// Represents a single day that IDs can attend
|
/// Represents a single day that IDs can attend
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
@@ -30,7 +28,7 @@ impl IDStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creats a new `IDStore` from a json file
|
/// Creats a new `IDStore` from a json file
|
||||||
pub async fn new_from_json(filepath: &str) -> Result<Self, Box<dyn Error>> {
|
pub async fn new_from_json(filepath: &str) -> Result<Self> {
|
||||||
let read_string = fs::read_to_string(filepath).await?;
|
let read_string = fs::read_to_string(filepath).await?;
|
||||||
Ok(serde_json::from_str(&read_string)?)
|
Ok(serde_json::from_str(&read_string)?)
|
||||||
}
|
}
|
||||||
@@ -59,14 +57,14 @@ impl IDStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the store to a json file
|
/// Writes the store to a json file
|
||||||
pub async fn export_json(&self, filepath: &str) -> Result<(), Box<dyn Error + Send + Sync>> {
|
pub async fn export_json(&self, filepath: &str) -> Result<()> {
|
||||||
fs::write(filepath, serde_json::to_string(&self)?).await?;
|
fs::write(filepath, serde_json::to_string(&self)?).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Export the store to a csv file.
|
/// Export the store to a csv file.
|
||||||
/// With days in the rows and IDs in the collum.
|
/// With days in the rows and IDs in the collum.
|
||||||
pub fn export_csv(&self) -> Result<String, Box<dyn Error>> {
|
pub fn export_csv(&self) -> Result<String> {
|
||||||
let mut csv = String::new();
|
let mut csv = String::new();
|
||||||
let seperator = ";";
|
let seperator = ";";
|
||||||
let mut user_ids: HashSet<TallyID> = HashSet::new();
|
let mut user_ids: HashSet<TallyID> = HashSet::new();
|
||||||
@@ -100,7 +98,7 @@ impl IDStore {
|
|||||||
let was_there: bool = self
|
let was_there: bool = self
|
||||||
.days
|
.days
|
||||||
.get(day)
|
.get(day)
|
||||||
.ok_or("Failed to access day")?
|
.ok_or(anyhow!("Failed to access day"))?
|
||||||
.ids
|
.ids
|
||||||
.contains(user_id);
|
.contains(user_id);
|
||||||
|
|
||||||
5
src/store/mod.rs
Normal file
5
src/store/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
mod id_store;
|
||||||
|
mod id_mapping;
|
||||||
|
|
||||||
|
pub use id_store::IDStore;
|
||||||
|
pub use id_mapping::{IDMapping,Name};
|
||||||
6
src/webserver/mod.rs
Normal file
6
src/webserver/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
mod server;
|
||||||
|
mod activity_fairing;
|
||||||
|
|
||||||
|
pub use activity_fairing::{ActivityNotifier,spawn_idle_watcher};
|
||||||
|
pub use server::start_webserver;
|
||||||
|
|
||||||
@@ -14,10 +14,9 @@ use tokio::select;
|
|||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tokio::sync::broadcast::Sender;
|
use tokio::sync::broadcast::Sender;
|
||||||
|
|
||||||
use crate::activity_fairing::ActivityNotifier;
|
use crate::store::{IDMapping, IDStore, Name};
|
||||||
use crate::id_mapping::{IDMapping, Name};
|
|
||||||
use crate::id_store::IDStore;
|
|
||||||
use crate::tally_id::TallyID;
|
use crate::tally_id::TallyID;
|
||||||
|
use crate::webserver::ActivityNotifier;
|
||||||
|
|
||||||
#[derive(Embed)]
|
#[derive(Embed)]
|
||||||
#[folder = "web/dist"]
|
#[folder = "web/dist"]
|
||||||
@@ -42,7 +42,9 @@
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<IDTable bind:this={idTable} />
|
<IDTable bind:this={idTable} onEdit={(id,firstName,lastName)=>{
|
||||||
|
addModal.open(id,firstName,lastName);
|
||||||
|
}}/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<AddIDModal
|
<AddIDModal
|
||||||
|
|||||||
@@ -9,8 +9,12 @@
|
|||||||
|
|
||||||
let modal: Modal;
|
let modal: Modal;
|
||||||
|
|
||||||
export function open(id: string) {
|
export function open(presetID: string, presetFirstName?: string, presetLastName?: string) {
|
||||||
displayID = id;
|
displayID = presetID;
|
||||||
|
|
||||||
|
firstName = presetFirstName ?? "";
|
||||||
|
lastName = presetLastName ?? "";
|
||||||
|
|
||||||
modal.open();
|
modal.open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
import type { IDMapping } from "./IDMapping";
|
import type { IDMapping } from "./IDMapping";
|
||||||
let data: IDMapping | undefined = $state();
|
let data: IDMapping | undefined = $state();
|
||||||
|
|
||||||
|
let { onEdit }: { onEdit?: (string,string,string) => void } = $props();
|
||||||
|
|
||||||
export async function reloadData() {
|
export async function reloadData() {
|
||||||
let res = await fetch("/api/mapping");
|
let res = await fetch("/api/mapping");
|
||||||
|
|
||||||
@@ -82,6 +84,8 @@
|
|||||||
|
|
||||||
<span class="indicator">{indicator("first")}</span>
|
<span class="indicator">{indicator("first")}</span>
|
||||||
</th>
|
</th>
|
||||||
|
<th>
|
||||||
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@@ -90,6 +94,9 @@
|
|||||||
<td class="whitespace-nowrap pr-5 pl-2 py-1">{row.id}</td>
|
<td class="whitespace-nowrap pr-5 pl-2 py-1">{row.id}</td>
|
||||||
<td class="whitespace-nowrap pr-5">{row.last}</td>
|
<td class="whitespace-nowrap pr-5">{row.last}</td>
|
||||||
<td class="whitespace-nowrap pr-5">{row.first}</td>
|
<td class="whitespace-nowrap pr-5">{row.first}</td>
|
||||||
|
<td class="pr-5" ><button onclick={()=>{
|
||||||
|
onEdit && onEdit(row.id,row.first,row.last);
|
||||||
|
}} class="cursor-pointer">🔧</button></td>
|
||||||
</tr>
|
</tr>
|
||||||
{/each}
|
{/each}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
<button
|
<button
|
||||||
class="bg-indigo-500 rounded-2xl px-2 cursor-pointer mx-2"
|
class="bg-indigo-500 rounded-2xl px-2 cursor-pointer mx-2"
|
||||||
onclick={() => {
|
onclick={() => {
|
||||||
if (onAdd && id != "") {
|
if (onAdd) {
|
||||||
onAdd(id);
|
onAdd(id);
|
||||||
}
|
}
|
||||||
}}>+</button
|
}}>+</button
|
||||||
|
|||||||
Reference in New Issue
Block a user