2022-01-30 17:22:55 +08:00
|
|
|
use directories::ProjectDirs;
|
2022-02-07 02:01:34 +08:00
|
|
|
use image::Luma;
|
2022-02-06 17:56:50 +08:00
|
|
|
use log::{info, warn};
|
2022-02-07 02:01:34 +08:00
|
|
|
use qrcode::QrCode;
|
2022-01-28 20:17:34 +08:00
|
|
|
use serde_json::Value;
|
2022-02-12 23:43:28 +08:00
|
|
|
use std::{error::Error, fs, path::PathBuf};
|
2022-02-12 23:23:22 +08:00
|
|
|
|
2022-02-12 23:43:28 +08:00
|
|
|
use crate::{device::config::DeviceMode, lighting::config::LedMode, output::config::OutputMode};
|
2022-02-12 23:23:22 +08:00
|
|
|
|
|
|
|
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));
|
2022-02-09 14:00:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 23:23:22 +08:00
|
|
|
Ok(ips)
|
2022-01-28 20:17:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Config {
|
|
|
|
pub raw: String,
|
|
|
|
pub device_mode: DeviceMode,
|
|
|
|
pub output_mode: OutputMode,
|
|
|
|
pub led_mode: LedMode,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
2022-01-29 04:23:16 +08:00
|
|
|
pub fn from_str(s: &str) -> Option<Config> {
|
|
|
|
let v: Value = serde_json::from_str(s).ok()?;
|
2022-01-28 20:17:34 +08:00
|
|
|
|
2022-01-29 04:23:16 +08:00
|
|
|
Some(Config {
|
2022-01-28 20:17:34 +08:00
|
|
|
raw: s.to_string(),
|
2022-02-12 23:43:28 +08:00
|
|
|
device_mode: DeviceMode::from_serde_value(&v)?,
|
|
|
|
output_mode: OutputMode::from_serde_value(&v)?,
|
|
|
|
led_mode: LedMode::from_serde_value(&v)?,
|
2022-01-29 04:23:16 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-05 18:00:06 +08:00
|
|
|
pub fn get_log_file_path() -> Option<Box<PathBuf>> {
|
2022-02-05 18:07:18 +08:00
|
|
|
let project_dir = ProjectDirs::from("me", "impress labs", "slidershim").unwrap();
|
2022-02-05 18:00:06 +08:00
|
|
|
let config_dir = project_dir.config_dir();
|
2022-02-07 15:56:03 +08:00
|
|
|
fs::create_dir_all(config_dir).ok()?;
|
2022-02-05 18:00:06 +08:00
|
|
|
|
|
|
|
let log_path = config_dir.join("log.txt");
|
|
|
|
|
|
|
|
return Some(Box::new(log_path));
|
|
|
|
}
|
|
|
|
|
2022-02-07 02:01:34 +08:00
|
|
|
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();
|
2022-02-07 15:56:03 +08:00
|
|
|
fs::create_dir_all(config_dir).ok()?;
|
2022-02-07 02:01:34 +08:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:56:03 +08:00
|
|
|
fn get_config_path() -> Option<Box<PathBuf>> {
|
2022-02-05 18:07:18 +08:00
|
|
|
let project_dir = ProjectDirs::from("me", "impress labs", "slidershim").unwrap();
|
2022-01-29 04:23:16 +08:00
|
|
|
let config_dir = project_dir.config_dir();
|
2022-02-07 15:56:03 +08:00
|
|
|
fs::create_dir_all(config_dir).ok()?;
|
2022-01-29 04:23:16 +08:00
|
|
|
|
|
|
|
let config_path = config_dir.join("config.json");
|
|
|
|
|
|
|
|
return Some(Box::new(config_path));
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:56:03 +08:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::from_str(
|
|
|
|
r#"{
|
|
|
|
"deviceMode": "none",
|
2022-02-09 14:00:36 +08:00
|
|
|
"devicePolling": "100",
|
2022-02-07 15:56:03 +08:00
|
|
|
"outputMode": "none",
|
|
|
|
"ledMode": "none",
|
|
|
|
"keyboardSensitivity": 20,
|
|
|
|
"outputWebsocketUrl": "localhost:3000",
|
2022-02-09 14:00:36 +08:00
|
|
|
"outputPolling": "100",
|
2022-02-07 15:56:03 +08:00
|
|
|
"ledSensitivity": 20,
|
|
|
|
"ledWebsocketUrl": "localhost:3001",
|
|
|
|
"ledSerialPort": "COM5"
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2022-01-29 04:23:16 +08:00
|
|
|
fn load_saved() -> Option<Self> {
|
2022-02-07 15:56:03 +08:00
|
|
|
let config_path = Self::get_config_path()?;
|
2022-01-29 04:23:16 +08:00
|
|
|
if !config_path.exists() {
|
|
|
|
return None;
|
2022-01-28 20:17:34 +08:00
|
|
|
}
|
2022-01-30 17:22:55 +08:00
|
|
|
info!("Config file found at {:?}", config_path);
|
2022-02-05 11:44:42 +08:00
|
|
|
let saved_data = fs::read_to_string(config_path.as_path()).ok()?;
|
2022-01-29 04:23:16 +08:00
|
|
|
return Self::from_str(saved_data.as_str());
|
|
|
|
}
|
|
|
|
|
2022-02-07 15:56:03 +08:00
|
|
|
pub fn load() -> Self {
|
2022-01-29 04:23:16 +08:00
|
|
|
Self::load_saved()
|
2022-02-06 17:56:50 +08:00
|
|
|
.or_else(|| {
|
|
|
|
warn!("Config loading from file failed, using default");
|
2022-02-07 15:56:03 +08:00
|
|
|
Some(Self::default())
|
2022-02-06 17:56:50 +08:00
|
|
|
})
|
2022-01-29 04:23:16 +08:00
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save(&self) -> Option<()> {
|
2022-01-30 17:22:55 +08:00
|
|
|
info!("Config saving...");
|
2022-02-07 15:56:03 +08:00
|
|
|
let config_path = Self::get_config_path()?;
|
2022-01-30 17:22:55 +08:00
|
|
|
info!("Config saving to {:?}", config_path);
|
2022-01-29 04:23:16 +08:00
|
|
|
fs::write(config_path.as_path(), self.raw.as_str()).unwrap();
|
2022-01-30 17:22:55 +08:00
|
|
|
info!("Config saved");
|
2022-02-07 15:56:03 +08:00
|
|
|
|
2022-01-29 04:23:16 +08:00
|
|
|
Some(())
|
2022-01-28 20:17:34 +08:00
|
|
|
}
|
|
|
|
}
|