1
0
mirror of https://github.com/4yn/slidershim.git synced 2025-02-02 04:27:58 +01:00

refactor input and system module

This commit is contained in:
4yn 2022-02-13 16:35:15 +08:00
parent ac4933449b
commit 287c9c63f6
19 changed files with 75 additions and 67 deletions

View File

@ -1,22 +1,10 @@
use directories::ProjectDirs;
use image::Luma;
use log::{info, warn}; use log::{info, warn};
use qrcode::QrCode;
use serde_json::Value; use serde_json::Value;
use std::{error::Error, fs, path::PathBuf}; use std::fs;
use crate::{device::config::DeviceMode, lighting::config::LedMode, output::config::OutputMode}; use crate::{
input::config::DeviceMode, lighting::config::LedMode, output::config::OutputMode, system,
pub fn list_ips() -> Result<Vec<String>, Box<dyn Error>> { };
let mut ips = vec![];
for adapter in ipconfig::get_adapters()? {
for ip_address in adapter.ip_addresses() {
ips.push(format!("{}", ip_address));
}
}
Ok(ips)
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Config { pub struct Config {
@ -38,48 +26,6 @@ impl Config {
}) })
} }
pub fn get_log_file_path() -> Option<Box<PathBuf>> {
let project_dir = ProjectDirs::from("me", "impress labs", "slidershim").unwrap();
let config_dir = project_dir.config_dir();
fs::create_dir_all(config_dir).ok()?;
let log_path = config_dir.join("log.txt");
return Some(Box::new(log_path));
}
pub fn get_brokenithm_qr_path() -> Option<Box<PathBuf>> {
let project_dir = ProjectDirs::from("me", "impress labs", "slidershim").unwrap();
let config_dir = project_dir.config_dir();
fs::create_dir_all(config_dir).ok()?;
let brokenithm_qr_path = config_dir.join("brokenithm.png");
let ips = list_ips().ok()?;
let link = "http://imp.ress.me/t/sshelper?d=".to_string()
+ &ips
.into_iter()
.filter(|s| s.as_str().chars().filter(|x| *x == '.').count() == 3)
.map(|s| base64::encode_config(s, base64::URL_SAFE_NO_PAD))
.collect::<Vec<String>>()
.join(";");
let qr = QrCode::new(link).ok()?;
let image = qr.render::<Luma<u8>>().build();
image.save(brokenithm_qr_path.as_path()).ok()?;
return Some(Box::new(brokenithm_qr_path));
}
fn get_config_path() -> Option<Box<PathBuf>> {
let project_dir = ProjectDirs::from("me", "impress labs", "slidershim").unwrap();
let config_dir = project_dir.config_dir();
fs::create_dir_all(config_dir).ok()?;
let config_path = config_dir.join("config.json");
return Some(Box::new(config_path));
}
fn default() -> Self { fn default() -> Self {
Self::from_str( Self::from_str(
r#"{ r#"{
@ -99,7 +45,7 @@ impl Config {
} }
fn load_saved() -> Option<Self> { fn load_saved() -> Option<Self> {
let config_path = Self::get_config_path()?; let config_path = system::get_config_path()?;
if !config_path.exists() { if !config_path.exists() {
return None; return None;
} }
@ -119,7 +65,7 @@ impl Config {
pub fn save(&self) -> Option<()> { pub fn save(&self) -> Option<()> {
info!("Config saving..."); info!("Config saving...");
let config_path = Self::get_config_path()?; let config_path = system::get_config_path()?;
info!("Config saving to {:?}", config_path); info!("Config saving to {:?}", config_path);
fs::write(config_path.as_path(), self.raw.as_str()).unwrap(); fs::write(config_path.as_path(), self.raw.as_str()).unwrap();
info!("Config saved"); info!("Config saved");

View File

@ -4,8 +4,8 @@ use std::sync::{atomic::Ordering, Arc};
use crate::{ use crate::{
config::Config, config::Config,
device::{brokenithm::BrokenithmJob, config::DeviceMode, device::HidDeviceJob}, input::{brokenithm::BrokenithmJob, config::DeviceMode, device::HidDeviceJob},
lighting::{config::LedMode, led::LedJob}, lighting::{config::LedMode, lighting::LedJob},
output::{config::OutputMode, output::OutputJob}, output::{config::OutputMode, output::OutputJob},
shared::{ shared::{
utils::LoopTimer, utils::LoopTimer,

View File

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 5.7 KiB

View File

Before

Width:  |  Height:  |  Size: 4.9 KiB

After

Width:  |  Height:  |  Size: 4.9 KiB

View File

@ -9,12 +9,15 @@ mod config;
mod shared; mod shared;
mod state; mod state;
mod device; mod input;
mod lighting; mod lighting;
mod output; mod output;
mod system;
mod context; mod context;
mod manager; mod manager;
pub use config::{list_ips, Config}; pub use config::Config;
pub use manager::Manager; pub use manager::Manager;
pub use system::{get_brokenithm_qr_path, get_log_file_path, list_ips};

View File

@ -1,3 +1,3 @@
pub mod config; pub mod config;
pub mod led; pub mod lighting;

View File

@ -0,0 +1,59 @@
use directories::ProjectDirs;
use image::Luma;
use qrcode::QrCode;
use std::{error::Error, fs, path::PathBuf};
pub fn list_ips() -> Result<Vec<String>, Box<dyn Error>> {
let mut ips = vec![];
for adapter in ipconfig::get_adapters()? {
for ip_address in adapter.ip_addresses() {
ips.push(format!("{}", ip_address));
}
}
Ok(ips)
}
/// Get the %APPDATA% path for config files (and create if it does not already
/// exist).
fn get_config_dir() -> Option<Box<PathBuf>> {
let project_dir = ProjectDirs::from("me", "impress labs", "slidershim").unwrap();
let config_dir = project_dir.config_dir();
fs::create_dir_all(config_dir).ok()?;
Some(Box::new(config_dir.to_path_buf()))
}
/// Generates a helper QR for connecting with brokenithm
pub fn get_brokenithm_qr_path() -> Option<Box<PathBuf>> {
let config_dir = get_config_dir()?;
let brokenithm_qr_path = config_dir.join("brokenithm.png");
let ips = list_ips().ok()?;
let link = "http://imp.ress.me/t/sshelper?d=".to_string()
+ &ips
.into_iter()
.filter(|s| s.as_str().chars().filter(|x| *x == '.').count() == 3)
.map(|s| base64::encode_config(s, base64::URL_SAFE_NO_PAD))
.collect::<Vec<String>>()
.join(";");
let qr = QrCode::new(link).ok()?;
let image = qr.render::<Luma<u8>>().build();
image.save(brokenithm_qr_path.as_path()).ok()?;
return Some(Box::new(brokenithm_qr_path));
}
pub fn get_log_file_path() -> Option<Box<PathBuf>> {
let config_dir = get_config_dir()?;
let log_path = config_dir.join("log.txt");
return Some(Box::new(log_path));
}
pub fn get_config_path() -> Option<Box<PathBuf>> {
let config_dir = get_config_dir()?;
let config_path = config_dir.join("config.json");
return Some(Box::new(config_path));
}

View File

@ -102,7 +102,7 @@ fn main() {
// Show logs // Show logs
app.listen_global("openLogfile", |_| { app.listen_global("openLogfile", |_| {
let log_file_path = slider_io::Config::get_log_file_path(); let log_file_path = slider_io::get_log_file_path();
if let Some(log_file_path) = log_file_path { if let Some(log_file_path) = log_file_path {
open::that(log_file_path.as_path()).ok(); open::that(log_file_path.as_path()).ok();
} }
@ -110,7 +110,7 @@ fn main() {
// Show brokenithm qr // Show brokenithm qr
app.listen_global("openBrokenithmQr", |_| { app.listen_global("openBrokenithmQr", |_| {
let brokenithm_qr_path = slider_io::Config::get_brokenithm_qr_path(); let brokenithm_qr_path = slider_io::get_brokenithm_qr_path();
if let Some(brokenithm_qr_path) = brokenithm_qr_path { if let Some(brokenithm_qr_path) = brokenithm_qr_path {
open::that(brokenithm_qr_path.as_path()).ok(); open::that(brokenithm_qr_path.as_path()).ok();
} }