add config and read files from binary

This commit is contained in:
Cooper Quintin
2024-06-11 16:46:47 -07:00
parent 81f545e90e
commit de8cf51649
5 changed files with 39 additions and 17 deletions

View File

@@ -7,6 +7,7 @@ struct ConfigFile {
qmdl_store_path: Option<String>,
port: Option<u16>,
readonly_mode: Option<bool>,
ui_level: Option<u8>,
}
#[derive(Debug)]
@@ -14,6 +15,7 @@ pub struct Config {
pub qmdl_store_path: String,
pub port: u16,
pub readonly_mode: bool,
pub ui_level: u8,
}
impl Default for Config {
@@ -22,6 +24,7 @@ impl Default for Config {
qmdl_store_path: "/data/rayhunter/qmdl".to_string(),
port: 8080,
readonly_mode: false,
ui_level: 1,
}
}
}
@@ -34,6 +37,7 @@ pub fn parse_config<P>(path: P) -> Result<Config, RayhunterError> where P: AsRef
if let Some(path) = parsed_config.qmdl_store_path { config.qmdl_store_path = path }
if let Some(port) = parsed_config.port { config.port = port }
if let Some(readonly_mode) = parsed_config.readonly_mode { config.readonly_mode = readonly_mode }
if let Some(ui_level) = parsed_config.ui_level { config.ui_level = ui_level }
}
Ok(config)
}

View File

@@ -28,6 +28,8 @@ use tokio::sync::oneshot::error::TryRecvError;
use tokio::task::JoinHandle;
use tokio_util::task::TaskTracker;
use std::net::SocketAddr;
use std::thread::sleep;
use std::time::Duration;
use tokio::net::TcpListener;
use tokio::sync::{RwLock, oneshot};
use std::sync::Arc;
@@ -120,7 +122,15 @@ fn run_ctrl_c_thread(
})
}
async fn update_ui(task_tracker: &TaskTracker, mut ui_shutdown_rx: oneshot::Receiver<()>){
async fn update_ui(task_tracker: &TaskTracker, config: &config::Config, mut ui_shutdown_rx: oneshot::Receiver<()>){
let image = match config.ui_level {
0 => {info!("silent UI!"); return},
1 => "subtle.png",
2 => "orca.gif",
_ => "orca.gif"
};
info!("spawning UI with {}", image);
task_tracker.spawn_blocking(move || {
let mut fb: Framebuffer = Framebuffer::new();
loop {
@@ -133,7 +143,8 @@ async fn update_ui(task_tracker: &TaskTracker, mut ui_shutdown_rx: oneshot::Rece
Err(e) => panic!("what the fuck {e}")
}
fb.draw_img("/data/rayhunter/orca.gif");
fb.draw_img(image);
sleep(Duration::from_millis(100));
}
}).await.unwrap();
@@ -164,7 +175,7 @@ async fn main() -> Result<(), RayhunterError> {
let (server_shutdown_tx, server_shutdown_rx) = oneshot::channel::<()>();
run_ctrl_c_thread(&task_tracker, tx.clone(), server_shutdown_tx, ui_shutdown_tx, qmdl_store_lock.clone());
run_server(&task_tracker, &config, qmdl_store_lock.clone(), server_shutdown_rx, tx).await;
update_ui(&task_tracker, ui_shutdown_rx).await;
update_ui(&task_tracker, &config, ui_shutdown_rx).await;
task_tracker.close();
task_tracker.wait().await;

View File

@@ -1,5 +1,5 @@
use image::{io::Reader as ImageReader, AnimationDecoder, imageops::FilterType, codecs::gif::GifDecoder, DynamicImage};
use std::{io::BufReader, fs::File, time::Duration};
use image::{codecs::gif::GifDecoder, imageops::FilterType, AnimationDecoder, DynamicImage};
use std::{io::Cursor, time::Duration};
use include_dir::{include_dir, Dir};
const FB_PATH:&str = "/dev/fb0";
@@ -30,9 +30,18 @@ impl Framebuffer<'_>{
fn write(&mut self, img: DynamicImage) {
let resized_img = img.resize( self.dimensions.width, self.dimensions.height, FilterType::CatmullRom);
let width = self.dimensions.width.min(resized_img.width());
let height = self.dimensions.height.min(resized_img.height());
let mut width = img.width();
let mut height = img.height();
let resized_img: DynamicImage;
if height > self.dimensions.height ||
width > self.dimensions.width {
resized_img = img.resize( self.dimensions.width, self.dimensions.height, FilterType::CatmullRom);
width = self.dimensions.width.min(resized_img.width());
height = self.dimensions.height.min(resized_img.height());
} else {
resized_img = img;
}
let img_rgba8 = resized_img.as_rgba8().unwrap();
let mut buf = Vec::new();
for y in 0..height {
@@ -49,12 +58,11 @@ impl Framebuffer<'_>{
pub fn draw_img(&mut self, img_name: &str) {
//let img_path = IMAGE_DIR.get_file(img_name).unwrap().path();
let img_path = img_name;
if img_path.ends_with(".gif") {
let img = IMAGE_DIR.get_file(img_name).unwrap();
if img_name.ends_with(".gif") {
// this is dumb and i'm sure there's a better way to loop this
let stream = BufReader::new(File::open(&img_path).unwrap());
let decoder = GifDecoder::new(stream).unwrap();
let cursor = Cursor::new(img.contents());
let decoder = GifDecoder::new(cursor).unwrap();
for maybe_frame in decoder.into_frames() {
let frame = maybe_frame.unwrap();
let (numerator, _) = frame.delay().numer_denom_ms();
@@ -63,8 +71,7 @@ impl Framebuffer<'_>{
std::thread::sleep(Duration::from_millis(numerator as u64));
}
} else {
let img_reader = ImageReader::open(img_path).unwrap();
let img = img_reader.decode().unwrap();
let img = image::load_from_memory(img.contents()).unwrap();
self.write(img);
}
}