Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update #6

Merged
merged 8 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading