Skip to content

Commit

Permalink
Updates (#6)
Browse files Browse the repository at this point in the history
* update

* Update Cargo.toml

* fixes

* anyhow::result

* update

* Bump egui

* Bump versions

* fix lints
  • Loading branch information
bircni authored Jul 16, 2024
1 parent 1d07023 commit 2112104
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 750 deletions.
22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ short_description = "A GUI for the awtrix clock."

[dependencies]
# Error handling
anyhow = "1.0.83"
anyhow = "1.0.86"
# Networking
reqwest = { version = "0.12.4", features = ["blocking"] }
reqwest = { version = "0.12.5", features = ["blocking"] }
# Parsing
serde = { version = "1.0.200", features = ["derive"] }
serde_json = "1.0.116"
semver = "1.0.22"
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120"
semver = "1.0.23"
# GUI
eframe = { version = "0.27.2", features = ["wgpu"] }
egui = "0.27.2"
egui-notify = "0.14.0"
egui_extras = { version = "0.27.2", features = ["syntect", "image"] }
eframe = "0.28.1"
egui = "0.28.1"
egui-notify = "0.15.0"
egui_extras = { version = "0.28.1", features = ["syntect", "image"] }
image = "0.25.1"
open = "5.1.2"
ping = "0.5.2"
open = "5.3.0"
parking_lot = "0.12.3"
75 changes: 6 additions & 69 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,28 @@
use anyhow::Context;
use core::str;
use ping::dgramsock::ping;
use serde::{Deserialize, Serialize};
use std::{
env, fs,
io::Write,
net::IpAddr,
str::FromStr,
sync::mpsc,
thread,
time::{Duration, Instant},
};
use std::{env, fs, io::Write};

const ENV: &str = ".awtrix.env";

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
pub ip: Option<IpAddr>,
pub ip_str: String,
#[serde(skip, default = "Instant::now")]
pub last_ping: Instant,
pub ip: String,
// true = On, false = Off
pub last_state: bool,
}

impl Config {
pub fn new() -> Self {
match Self::read() {
Ok(config) => {
let mut config = config;
config.check_status(true);
config
}
Ok(config) => config,
Err(_) => Self {
ip: None,
ip_str: String::new(),
last_ping: Instant::now(),
ip: String::new(),
last_state: false,
},
}
}

pub fn check_status(&mut self, force: bool) {
if !force && self.last_ping.elapsed() < Duration::from_secs(10) {
return;
}

if let Some(ip) = self.ip {
self.last_state = Config::check_power(ip).is_ok();
self.last_ping = Instant::now();
}
}

pub fn set_ip(&mut self) -> anyhow::Result<()> {
let ip = IpAddr::from_str(&self.ip_str).context("Failed to parse IP")?;
self.ip = Some(ip);
Config::write(self)?;
Ok(())
}

fn read() -> anyhow::Result<Self> {
let curr = env::current_exe()?;
let filepath = curr.parent().context("Failed to gt parent path")?.join(ENV);
Expand All @@ -67,41 +31,14 @@ impl Config {
serde_json::from_str(&content).context("Failed to deserialize Config")
}

pub fn write(config: &Config) -> anyhow::Result<()> {
pub fn write(&self) -> anyhow::Result<()> {
if let Some(filepath) = env::current_exe()?.parent().map(|x| x.join(ENV)) {
let mut file = fs::File::create(filepath)?;
file.write_all(serde_json::to_string(config)?.as_bytes())?;
file.write_all(serde_json::to_string(self)?.as_bytes())?;
file.flush()?;
Ok(())
} else {
anyhow::bail!("Failed to write to file")
}
}

fn check_power(ip: IpAddr) -> anyhow::Result<()> {
check_ip(ip)?;

let (sender, receiver) = mpsc::channel();
thread::spawn(move || {
sender.send(ping(
ip,
Some(Duration::from_secs(1)),
None,
None,
None,
None,
))
});
match receiver.recv()? {
Ok(()) => Ok(()),
_ => anyhow::bail!("Device is not reachable"),
}
}
}

pub fn check_ip(ip: IpAddr) -> anyhow::Result<()> {
if ip.is_unspecified() {
anyhow::bail!("IP is empty");
}
Ok(())
}
140 changes: 0 additions & 140 deletions src/customapp.rs

This file was deleted.

6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![warn(clippy::perf)]
#![warn(clippy::style)]
#![deny(clippy::all)]
#![deny(clippy::unwrap_used)]
#![warn(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_possible_truncation)]
Expand All @@ -11,7 +11,6 @@ use anyhow::Context;
use egui::ViewportBuilder;

mod config;
mod customapp;
mod ui;

fn main() -> anyhow::Result<()> {
Expand All @@ -28,12 +27,11 @@ fn main() -> anyhow::Result<()> {
"Awtrix",
eframe::NativeOptions {
viewport,
depth_buffer: 32,
follow_system_theme: true,
centered: true,
..Default::default()
},
Box::new(|cc| Box::new(ui::App::new(cc))),
Box::new(|cc| Ok(Box::new(ui::App::new(cc)))),
)
.map_err(|e| anyhow::anyhow!(e.to_string()))
.context("Failed to run native")
Expand Down
Loading

0 comments on commit 2112104

Please sign in to comment.