Skip to content

Commit

Permalink
Merge pull request #5 from jackra1n/more_logging
Browse files Browse the repository at this point in the history
Add more logs for debugging purposes
  • Loading branch information
jackra1n committed May 21, 2024
2 parents c718928 + 1b7ef64 commit 12d4945
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustberry-poe-monitor"
version = "1.0.2"
version = "1.0.3"
edition = "2021"
authors = ["jackra1n"]
description = "A simple PoE Monitor for Raspberry Pi Waveshare PoE HAT (B)"
Expand Down
5 changes: 5 additions & 0 deletions src/fan_controller.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use linux_embedded_hal::{I2cdev, i2cdev::core::I2CDevice};
use anyhow::{anyhow, Result};
use log::debug;

const SLAVE_ADDRESS: u16 = 0x20;
const FAN_ON_COMMAND: u8 = 0xFE;
Expand All @@ -14,6 +15,7 @@ pub struct FanController {

impl FanController {
pub fn new(temp_on: f32, temp_off: f32) -> Result<Self> {
debug!("Initializing FanController");
if temp_off <= 0.0 || temp_on <= 0.0 {
return Err(anyhow!("Temperatures must be greater than 0"));
}
Expand All @@ -22,6 +24,7 @@ impl FanController {
}

let i2c = I2cdev::new("/dev/i2c-1")?;
debug!("I2C initialized");

Ok(FanController {
i2c,
Expand All @@ -32,13 +35,15 @@ impl FanController {
}

pub fn fan_on(&mut self) -> Result<(), Box<dyn std::error::Error>> {
debug!("Sending fan on command");
self.i2c.set_slave_address(SLAVE_ADDRESS)?;
self.i2c.smbus_write_byte(FAN_ON_COMMAND)?;
self.is_running = true;
Ok(())
}

pub fn fan_off(&mut self) -> Result<(), Box<dyn std::error::Error>> {
debug!("Sending fan off command");
self.i2c.set_slave_address(SLAVE_ADDRESS)?;
self.i2c.smbus_write_byte(FAN_OFF_COMMAND)?;
self.is_running = false;
Expand Down
23 changes: 21 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fs;
use std::thread;
use std::time::{Duration, Instant};
use sysinfo::{System, SystemExt, CpuExt, DiskExt};
use log::info;
use log::{info, debug, trace};
use clap::Parser;
use env_logger::{Builder, Env};

Expand All @@ -29,6 +29,15 @@ fn main() -> Result<(), Box<dyn Error>> {
let env = Env::default().default_filter_or("info");
Builder::from_env(env).init();

let version = env!("CARGO_PKG_VERSION");

debug!("Binary info:");
debug!("================================");
debug!("rustberry-poe-monitor: {}", version);
debug!("Target OS: {}", std::env::consts::OS);
debug!("Target Family: {}", std::env::consts::FAMILY);
debug!("Target Architecture: {}", std::env::consts::ARCH);

let mut poe_disp = PoeDisplay::new()?;
info!("Display initialized");

Expand All @@ -38,22 +47,32 @@ fn main() -> Result<(), Box<dyn Error>> {

let mut sys: System = SystemExt::new_all();

debug!("System initialized. System info:");
debug!("================================");
debug!("System name: {}", sys.name().unwrap_or_default());
debug!("System kernel version: {}", sys.kernel_version().unwrap_or_default());
debug!("System OS version: {}", sys.os_version().unwrap_or_default());

let mut disk_usage = String::new();
let disk_update_interval = Duration::from_secs(60);
let mut last_disk_update = Instant::now() - disk_update_interval;
info!("Starting main loop");

fan_controller.fan_off()?;

loop {
sys.refresh_cpu();
sys.refresh_memory();

let ip_address = get_local_ip();
let temp = get_cpu_temperature();

let temp_str = format!("{:.1}", temp);
let cpu_usage = format!("{:.1}", get_cpu_usage(&sys));
let ram_usage = format!("{:.1}", get_ram_usage(&sys));


trace!("Checking fan controller. Fan running: {}", fan_controller.is_running);
trace!("Temp: {}, Temp-on: {}, Temp-off: {}", temp, fan_controller.temp_on, fan_controller.temp_off);
if fan_controller.is_running {
if temp <= fan_controller.temp_off {
fan_controller.fan_off()?;
Expand Down

0 comments on commit 12d4945

Please sign in to comment.