mirror of
https://github.com/4yn/slidershim.git
synced 2025-02-01 20:18:07 +01:00
refactor input and system module
This commit is contained in:
parent
ac4933449b
commit
287c9c63f6
@ -1,22 +1,10 @@
|
||||
use directories::ProjectDirs;
|
||||
use image::Luma;
|
||||
use log::{info, warn};
|
||||
use qrcode::QrCode;
|
||||
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};
|
||||
|
||||
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)
|
||||
}
|
||||
use crate::{
|
||||
input::config::DeviceMode, lighting::config::LedMode, output::config::OutputMode, system,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
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 {
|
||||
Self::from_str(
|
||||
r#"{
|
||||
@ -99,7 +45,7 @@ impl Config {
|
||||
}
|
||||
|
||||
fn load_saved() -> Option<Self> {
|
||||
let config_path = Self::get_config_path()?;
|
||||
let config_path = system::get_config_path()?;
|
||||
if !config_path.exists() {
|
||||
return None;
|
||||
}
|
||||
@ -119,7 +65,7 @@ impl Config {
|
||||
|
||||
pub fn save(&self) -> Option<()> {
|
||||
info!("Config saving...");
|
||||
let config_path = Self::get_config_path()?;
|
||||
let config_path = system::get_config_path()?;
|
||||
info!("Config saving to {:?}", config_path);
|
||||
fs::write(config_path.as_path(), self.raw.as_str()).unwrap();
|
||||
info!("Config saved");
|
||||
|
@ -4,8 +4,8 @@ use std::sync::{atomic::Ordering, Arc};
|
||||
|
||||
use crate::{
|
||||
config::Config,
|
||||
device::{brokenithm::BrokenithmJob, config::DeviceMode, device::HidDeviceJob},
|
||||
lighting::{config::LedMode, led::LedJob},
|
||||
input::{brokenithm::BrokenithmJob, config::DeviceMode, device::HidDeviceJob},
|
||||
lighting::{config::LedMode, lighting::LedJob},
|
||||
output::{config::OutputMode, output::OutputJob},
|
||||
shared::{
|
||||
utils::LoopTimer,
|
||||
|
Before Width: | Height: | Size: 5.7 KiB After Width: | Height: | Size: 5.7 KiB |
Before Width: | Height: | Size: 4.9 KiB After Width: | Height: | Size: 4.9 KiB |
@ -9,12 +9,15 @@ mod config;
|
||||
mod shared;
|
||||
mod state;
|
||||
|
||||
mod device;
|
||||
mod input;
|
||||
mod lighting;
|
||||
mod output;
|
||||
|
||||
mod system;
|
||||
|
||||
mod context;
|
||||
mod manager;
|
||||
|
||||
pub use config::{list_ips, Config};
|
||||
pub use config::Config;
|
||||
pub use manager::Manager;
|
||||
pub use system::{get_brokenithm_qr_path, get_log_file_path, list_ips};
|
||||
|
@ -1,3 +1,3 @@
|
||||
pub mod config;
|
||||
|
||||
pub mod led;
|
||||
pub mod lighting;
|
||||
|
59
src-slider_io/src/system.rs
Normal file
59
src-slider_io/src/system.rs
Normal 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));
|
||||
}
|
@ -102,7 +102,7 @@ fn main() {
|
||||
|
||||
// Show logs
|
||||
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 {
|
||||
open::that(log_file_path.as_path()).ok();
|
||||
}
|
||||
@ -110,7 +110,7 @@ fn main() {
|
||||
|
||||
// Show brokenithm qr
|
||||
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 {
|
||||
open::that(brokenithm_qr_path.as_path()).ok();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user