拓展自定义事件类型, 增强程序拓展性, 配置保存, 读取逻辑完成.
This commit is contained in:
		
							
								
								
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @@ -5047,8 +5047,6 @@ dependencies = [ | ||||
|  "chrono", | ||||
|  "crossbeam", | ||||
|  "iced", | ||||
|  "lazy_static", | ||||
|  "log", | ||||
|  "mysql", | ||||
|  "serde", | ||||
|  "serde_json", | ||||
|   | ||||
| @@ -11,8 +11,6 @@ serde_json = "1.0.132" | ||||
| mysql = "25.0.1" | ||||
| chrono="0.4.38" | ||||
| tokio = { version = "1.42", features = ["full"] } | ||||
| log = "0.4.22" | ||||
| lazy_static = "1.5.0" | ||||
| time = "0.3.37" | ||||
| crossbeam = "0.8" | ||||
|  | ||||
|   | ||||
| @@ -18,6 +18,16 @@ pub enum DownloadType { | ||||
|     Rail, | ||||
| } | ||||
|  | ||||
| impl ToString for DownloadType { | ||||
|     fn to_string(&self) -> String { | ||||
|         match self { | ||||
|             DownloadType::Bootloader => "BootLoader".to_string(), | ||||
|             DownloadType::App => "App".to_string(), | ||||
|             DownloadType::Rail => "Rail".to_string(), | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||||
| pub enum DownloadError { | ||||
|     JlinkNotFindError, | ||||
|   | ||||
							
								
								
									
										112
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										112
									
								
								src/main.rs
									
									
									
									
									
								
							| @@ -2,6 +2,7 @@ mod download_wrapper; | ||||
| mod mes_service; | ||||
|  | ||||
| use std::any::Any; | ||||
| use std::fs; | ||||
| use crossbeam::channel; | ||||
| use std::sync::{Arc, Mutex, LazyLock}; | ||||
| use crossbeam::channel::Sender; | ||||
| @@ -10,7 +11,7 @@ use iced::{event, window, Element, Event, Length, Subscription, Task, time}; | ||||
| use iced::application::Title; | ||||
| use iced::widget::text_editor::Action::Scroll; | ||||
| use iced::widget::text_editor::Edit; | ||||
|  | ||||
| use serde_json::Map; | ||||
| use download_wrapper::DownloadType; | ||||
| use crate::mes_service::MesService; | ||||
|  | ||||
| @@ -32,13 +33,13 @@ struct MainWindow { | ||||
|     selection: Option<DownloadType>, | ||||
|     label: String, | ||||
|     log_content: text_editor::Content, | ||||
|     sender: Sender<bool>, | ||||
|     receiver: crossbeam::channel::Receiver<bool>, | ||||
|     sender: Sender<Message>, | ||||
|     receiver: channel::Receiver<Message>, | ||||
| } | ||||
|  | ||||
| impl Default for MainWindow { | ||||
|     fn default() -> Self { | ||||
|         let (sender, receiver) = crossbeam::channel::unbounded(); | ||||
|         let (sender, receiver) =channel::unbounded(); | ||||
|         Self { | ||||
|             is_online: true, | ||||
|             mysql_config: MysqlConfig::default(), | ||||
| @@ -150,8 +151,18 @@ impl MainWindow { | ||||
|                 logs.lock().unwrap().clear(); | ||||
|  | ||||
|                 match self.receiver.try_recv() { | ||||
|                     Ok(res) => { | ||||
|                         self.update(Message::DownloadEnd(res)); | ||||
|                     Ok(event) => { | ||||
|                         match event{ | ||||
|                             Message::DownloadEnd(res)=>{ | ||||
|                                 if res{ | ||||
|                                     add_log("下载成功 By Ui Thread".to_string()); | ||||
|                                 }else{ | ||||
|                                     add_log("下载失败 By Ui Thread".to_string()); | ||||
|                                 } | ||||
|                             } | ||||
|                             _=>{ | ||||
|                             } | ||||
|                         } | ||||
|                     } | ||||
|                     Err(channel::TryRecvError::Empty) => { | ||||
|                     } | ||||
| @@ -182,8 +193,14 @@ impl MainWindow { | ||||
|             } | ||||
|             Message::WindowEvent(event) => { | ||||
|                 //println!("{:?}", event); | ||||
|                 if let Event::Window(window::Event::Opened { position, size }) = event { | ||||
|                     println!("Opened"); | ||||
|                     self.read_config(); | ||||
|                     return Task::none(); | ||||
|                 }; | ||||
|                 if let Event::Window(window::Event::CloseRequested) = event { | ||||
|                     println!("Request Close"); | ||||
|                     self.save_config(); | ||||
|                     return window::get_latest().and_then(window::close); | ||||
|                 }; | ||||
|                 Task::none() | ||||
| @@ -247,7 +264,7 @@ impl MainWindow { | ||||
|         ]) | ||||
|     } | ||||
|  | ||||
|     fn start(&mut self, sender: Sender<bool>) -> (){ | ||||
|     fn start(&mut self, sender: Sender<Message>) -> (){ | ||||
|         let mes_config = self.mysql_config.clone(); | ||||
|         let label = self.label.clone(); | ||||
|         let download_type: DownloadType = self.selection.unwrap(); | ||||
| @@ -263,11 +280,13 @@ impl MainWindow { | ||||
|                 let check_result = mes_service.check_station(mes_config.work_order.clone(), label.clone(), download_type); | ||||
|                 if let Err(res) = check_result { | ||||
|                     add_log(format!("过站检测失败: {:?}", res)); | ||||
|                     sender.send(false).unwrap(); | ||||
|                     sender.send(Message::DownloadEnd(false)).unwrap(); | ||||
|                     return; | ||||
|                 } else if let Ok(res) = check_result { | ||||
|                     if !res { | ||||
|                         add_log("过站检测失败".to_string()); | ||||
|                         sender.send(false).unwrap(); | ||||
|                         sender.send(Message::DownloadEnd(false)).unwrap(); | ||||
|                         return; | ||||
|                     } | ||||
|                     add_log("过站检测成功".to_string()); | ||||
|                 } | ||||
| @@ -275,13 +294,15 @@ impl MainWindow { | ||||
|             add_log("正在检查JLink连接".to_string()); | ||||
|             if let Err(e) = download_wrapper.check_jlink() { | ||||
|                 add_log("JLink检查失败, 请检查是否安装驱动或连接是否异常".to_string()); | ||||
|                 sender.send(false).unwrap(); | ||||
|                 sender.send(Message::DownloadEnd(false)).unwrap(); | ||||
|                 return; | ||||
|             } | ||||
|             add_log("JLink检查成功".to_string()); | ||||
|             add_log("正在检查设备连接".to_string()); | ||||
|             if let Err(e) = download_wrapper.check_device() { | ||||
|                 add_log("设备检查失败, 请检查设备是否连接".to_string()); | ||||
|                 sender.send(false).unwrap(); | ||||
|                 sender.send(Message::DownloadEnd(false)).unwrap(); | ||||
|                 return; | ||||
|             } | ||||
|             add_log("设备检查成功".to_string()); | ||||
|             match download_type { | ||||
| @@ -289,13 +310,15 @@ impl MainWindow { | ||||
|                     add_log("正在擦除Flash".to_string()); | ||||
|                     if let Err(e) = download_wrapper.erase(){ | ||||
|                         add_log("擦除失败".to_string()); | ||||
|                         sender.send(false).unwrap(); | ||||
|                         sender.send(Message::DownloadEnd(false)).unwrap(); | ||||
|                         return; | ||||
|                     } | ||||
|                     add_log("擦除成功".to_string()); | ||||
|                     add_log("正在下载BootLoader".to_string()); | ||||
|                     if let Err(e) = download_wrapper.download(download_type){ | ||||
|                         add_log("下载BootLoader失败".to_string()); | ||||
|                         sender.send(false).unwrap(); | ||||
|                         sender.send(Message::DownloadEnd(false)).unwrap(); | ||||
|                         return; | ||||
|                     } | ||||
|                     add_log("下载成功".to_string()); | ||||
|                 } | ||||
| @@ -303,7 +326,8 @@ impl MainWindow { | ||||
|                     add_log("正在下载App".to_string()); | ||||
|                     if let Err(e) = download_wrapper.download(download_type){ | ||||
|                         add_log("下载App失败".to_string()); | ||||
|                         sender.send(false).unwrap(); | ||||
|                         sender.send(Message::DownloadEnd(false)).unwrap(); | ||||
|                         return; | ||||
|                     } | ||||
|                     add_log("下载成功".to_string()); | ||||
|                 } | ||||
| @@ -311,7 +335,8 @@ impl MainWindow { | ||||
|                     add_log("正在下载Rail".to_string()); | ||||
|                     if let Err(e) = download_wrapper.download(download_type){ | ||||
|                         add_log("下载Rail失败".to_string()); | ||||
|                         sender.send(false).unwrap(); | ||||
|                         sender.send(Message::DownloadEnd(false)).unwrap(); | ||||
|                         return; | ||||
|                     } | ||||
|                     add_log("下载成功".to_string()); | ||||
|                 } | ||||
| @@ -323,13 +348,64 @@ impl MainWindow { | ||||
|                 let update_result = mes_service.update_station(mes_config.work_order, label, download_type); | ||||
|                 if let Err(e) = update_result { | ||||
|                     add_log(format!("上传失败: {:?}", e)); | ||||
|                     sender.send(false).unwrap(); | ||||
|                     sender.send(Message::DownloadEnd(false)).unwrap(); | ||||
|                     return; | ||||
|  | ||||
|                 } | ||||
|                 sender.send(true).unwrap(); | ||||
|                 sender.send(Message::DownloadEnd(true)).unwrap(); | ||||
|                 return; | ||||
|             } else { | ||||
|                 add_log("当前为离线模式".to_string()); | ||||
|                 sender.send(true).unwrap(); | ||||
|                 sender.send(Message::DownloadEnd(true)).unwrap(); | ||||
|                 return; | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     fn save_config(&mut self){ | ||||
|         let mut config = Map::new(); | ||||
|         config.insert("ip".to_string(), self.mysql_config.ip.clone().into()); | ||||
|         config.insert("port".to_string(), self.mysql_config.port.clone().into()); | ||||
|         config.insert("username".to_string(), self.mysql_config.username.clone().into()); | ||||
|         config.insert("password".to_string(), self.mysql_config.password.clone().into()); | ||||
|         config.insert("database".to_string(), self.mysql_config.database.clone().into()); | ||||
|         config.insert("work_order".to_string(), self.mysql_config.work_order.clone().into()); | ||||
|         config.insert("is_online".to_string(), self.is_online.clone().into()); | ||||
|         config.insert("selection".to_string(), self.selection.unwrap().to_string().into()); | ||||
|         let config_str = serde_json::to_string(&config).unwrap(); | ||||
|         fs::write("config.json", config_str).unwrap(); | ||||
|     } | ||||
|     fn read_config(&mut self){ | ||||
|         if !fs::exists("config.json").unwrap(){ | ||||
|             return; | ||||
|         } | ||||
|         let config_str = fs::read_to_string("config.json").unwrap(); | ||||
|         let config: Map<String, serde_json::Value> = serde_json::from_str(&config_str).unwrap(); | ||||
|         self.mysql_config.ip = config["ip"].as_str().unwrap().to_string(); | ||||
|         self.mysql_config.port = config["port"].as_str().unwrap().to_string(); | ||||
|         self.mysql_config.username = config["username"].as_str().unwrap().to_string(); | ||||
|         self.mysql_config.password = config["password"].as_str().unwrap().to_string(); | ||||
|         self.mysql_config.database = config["database"].as_str().unwrap().to_string(); | ||||
|         self.mysql_config.work_order = config["work_order"].as_str().unwrap().to_string(); | ||||
|         self.is_online = config["is_online"].as_bool().unwrap(); | ||||
|         match config["selection"].as_str().unwrap(){ | ||||
|             "BootLoader"=>{ | ||||
|                 self.selection = Some(DownloadType::Bootloader); | ||||
|             } | ||||
|             "App"=>{ | ||||
|                 self.selection = Some(DownloadType::App); | ||||
|             } | ||||
|             "Rail"=>{ | ||||
|                 self.selection = Some(DownloadType::Rail); | ||||
|             } | ||||
|             _=>{ | ||||
|                 self.selection = None; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|     fn save_ui_log(&mut self){ | ||||
|         todo!() | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user