feat: moved more values to constants

This commit is contained in:
Jet Pham 2025-06-06 20:34:56 -07:00
parent 6be649f0fc
commit 93f5d8e904
No known key found for this signature in database
3 changed files with 32 additions and 19 deletions

View file

@ -6,6 +6,10 @@ use serenity::all::{prelude::*, Color, CreateEmbed, CreateMessage};
use serenity::model::id::ChannelId; use serenity::model::id::ChannelId;
use tracing::{info, error}; use tracing::{info, error};
const COLOR_OPEN: Color = Color::new(0x00FF00); // Green for open
const COLOR_CLOSED: Color = Color::new(0xFF0000); // Red for closed
const COLOR_STARTUP: Color = Color::new(0xFFA500); // Orange for startup
pub struct DiscordClient { pub struct DiscordClient {
client: Client, client: Client,
channel_id: ChannelId, channel_id: ChannelId,
@ -47,11 +51,11 @@ impl DiscordClient {
crate::gpio::CircuitEvent::Closed => "We'll see you again soon.", crate::gpio::CircuitEvent::Closed => "We'll see you again soon.",
}) })
.color(match event { .color(match event {
crate::gpio::CircuitEvent::Open => Color::new(0x00FF00), // Red for open crate::gpio::CircuitEvent::Open => COLOR_OPEN,
crate::gpio::CircuitEvent::Closed => Color::new(0xFF0000), // Green for closed crate::gpio::CircuitEvent::Closed => COLOR_CLOSED,
}).thumbnail(match event { }).thumbnail(match event {
crate::gpio::CircuitEvent::Open => "https://www.noisebridge.net/images/7/7f/Open.png", // Image that says "Open" crate::gpio::CircuitEvent::Open => "https://www.noisebridge.net/images/7/7f/Open.png",
crate::gpio::CircuitEvent::Closed => "https://www.noisebridge.net/images/c/c9/Closed.png", // Image that says "Closed" crate::gpio::CircuitEvent::Closed => "https://www.noisebridge.net/images/c/c9/Closed.png",
}); });
if let Err(why) = self.channel_id.send_message(&self.client.http, CreateMessage::default().add_embed(embed)).await { if let Err(why) = self.channel_id.send_message(&self.client.http, CreateMessage::default().add_embed(embed)).await {
@ -71,7 +75,7 @@ impl DiscordClient {
let embed = CreateEmbed::new() let embed = CreateEmbed::new()
.title("Noisebell is starting up!") .title("Noisebell is starting up!")
.description("The Noisebell service is initializing and will begin monitoring the space status.") .description("The Noisebell service is initializing and will begin monitoring the space status.")
.color(Color::new(0xFFA500)) // Orange for startup .color(COLOR_STARTUP)
.thumbnail("https://cats.com/wp-content/uploads/2024/07/Beautiful-red-cat-stretches-and-shows-tongue.jpg"); .thumbnail("https://cats.com/wp-content/uploads/2024/07/Beautiful-red-cat-stretches-and-shows-tongue.jpg");
if let Err(why) = self.channel_id.send_message(&self.client.http, CreateMessage::default().add_embed(embed)).await { if let Err(why) = self.channel_id.send_message(&self.client.http, CreateMessage::default().add_embed(embed)).await {

View file

@ -2,7 +2,7 @@ use std::time::Duration;
use std::fmt; use std::fmt;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use anyhow::Result; use anyhow::{Result, Context};
use rppal::gpio::{Gpio, InputPin, Level}; use rppal::gpio::{Gpio, InputPin, Level};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
@ -27,8 +27,11 @@ pub struct GpioMonitor {
impl GpioMonitor { impl GpioMonitor {
pub fn new(pin_number: u8, poll_interval: Duration) -> Result<Self> { pub fn new(pin_number: u8, poll_interval: Duration) -> Result<Self> {
let gpio = Gpio::new()?; let gpio = Gpio::new()
let pin = gpio.get(pin_number)?.into_input_pullup(); .context("Failed to initialize GPIO")?;
let pin = gpio.get(pin_number)
.context(format!("Failed to get GPIO pin {}", pin_number))?
.into_input_pullup();
Ok(Self { pin, poll_interval }) Ok(Self { pin, poll_interval })
} }
@ -38,6 +41,7 @@ impl GpioMonitor {
F: FnMut(CircuitEvent) + Send + 'static, F: FnMut(CircuitEvent) + Send + 'static,
{ {
let mut previous_state = self.get_current_state(); let mut previous_state = self.get_current_state();
callback(previous_state); // Send initial state
loop { loop {
let current_state = self.get_current_state(); let current_state = self.get_current_state();

View file

@ -11,18 +11,25 @@ use tracing_appender::rolling::{RollingFileAppender, Rotation};
use tracing_subscriber::filter::LevelFilter; use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt};
const DEFAULT_GPIO_PIN: u8 = 17;
const DEFAULT_POLL_INTERVAL_MS: u64 = 100;
const LOG_DIR: &str = "logs";
const LOG_PREFIX: &str = "noisebell";
const LOG_SUFFIX: &str = "log";
const MAX_LOG_FILES: usize = 7;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
info!("creating logs directory"); info!("creating logs directory");
fs::create_dir_all("logs")?; fs::create_dir_all(LOG_DIR)?;
info!("initializing logging"); info!("initializing logging");
let file_appender = RollingFileAppender::builder() let file_appender = RollingFileAppender::builder()
.rotation(Rotation::DAILY) .rotation(Rotation::DAILY)
.filename_prefix("noisebell") .filename_prefix(LOG_PREFIX)
.filename_suffix("log") .filename_suffix(LOG_SUFFIX)
.max_log_files(7) .max_log_files(MAX_LOG_FILES)
.build("logs")?; .build(LOG_DIR)?;
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender); let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
@ -44,13 +51,11 @@ async fn main() -> Result<()> {
discord_client.send_startup_message().await?; discord_client.send_startup_message().await?;
const DEFAULT_GPIO_PIN: u8 = 17;
info!("initializing gpio monitor"); info!("initializing gpio monitor");
let mut gpio_monitor = gpio::GpioMonitor::new(DEFAULT_GPIO_PIN, Duration::from_millis(100))?; let mut gpio_monitor = gpio::GpioMonitor::new(
DEFAULT_GPIO_PIN,
// Send initial state Duration::from_millis(DEFAULT_POLL_INTERVAL_MS)
discord_client.clone().send_circuit_event(&gpio_monitor.get_current_state()).await?; )?;
// Set up the callback for state changes // Set up the callback for state changes
let callback = move |event: gpio::CircuitEvent| { let callback = move |event: gpio::CircuitEvent| {