添加测试结束后颜色显示, 方便判断, 修复部分Bug

This commit is contained in:
JIe 2024-12-11 14:07:30 +08:00
parent 85939e7fc4
commit 410a2ad5e1
3 changed files with 56 additions and 30 deletions

View File

@ -204,7 +204,7 @@ impl DownloadWrapper {
} }
fn save_log(& self, label: String, log: String) -> Result<bool, DownloadError> { fn save_log(& self, label: String, log: String) -> Result<bool, DownloadError> {
if !fs::exists("./log") { if !fs::exists("./log").unwrap() {
fs::create_dir("./log").expect("Cant Create Log Folder"); fs::create_dir("./log").expect("Cant Create Log Folder");
} }
let mut file = fs::OpenOptions::new() let mut file = fs::OpenOptions::new()

View File

@ -6,15 +6,18 @@ use crossbeam::channel;
use crossbeam::channel::Sender; use crossbeam::channel::Sender;
use download_wrapper::DownloadType; use download_wrapper::DownloadType;
use iced::application::Title; use iced::application::Title;
use iced::widget::progress_bar::Style;
use iced::widget::text_editor::Action::Scroll; use iced::widget::text_editor::Action::Scroll;
use iced::widget::text_editor::Edit; use iced::widget::text_editor::Edit;
use iced::widget::{button, checkbox, column, container, radio, row, text_editor, text_input}; use iced::widget::{
use iced::{event, time, window, Element, Event, Length, Subscription, Task}; button, checkbox, column, container, progress_bar, radio, row, text_editor, text_input,
};
use iced::{event, time, window, Background, Border, Color, Element, Event, Length, Subscription, Task};
use serde_json::Map; use serde_json::Map;
use std::any::Any; use std::any::Any;
use std::fs; use std::fs;
use std::sync::{Arc, LazyLock, Mutex};
use std::io::Write; use std::io::Write;
use std::sync::{Arc, LazyLock, Mutex};
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
iced::application("WisunDownload V0.1", MainWindow::update, MainWindow::view) iced::application("WisunDownload V0.1", MainWindow::update, MainWindow::view)
@ -24,7 +27,7 @@ pub fn main() -> iced::Result {
.run() .run()
} }
static logs: LazyLock<Arc<Mutex<Vec<String>>>> = LazyLock::new(|| Arc::new(Mutex::new(Vec::new()))); static LOGS: LazyLock<Arc<Mutex<Vec<String>>>> = LazyLock::new(|| Arc::new(Mutex::new(Vec::new())));
struct MainWindow { struct MainWindow {
is_online: bool, is_online: bool,
@ -34,6 +37,7 @@ struct MainWindow {
log_content: text_editor::Content, log_content: text_editor::Content,
sender: Sender<Message>, sender: Sender<Message>,
receiver: channel::Receiver<Message>, receiver: channel::Receiver<Message>,
result_background: Color
} }
impl Default for MainWindow { impl Default for MainWindow {
@ -45,6 +49,7 @@ impl Default for MainWindow {
selection: None, selection: None,
label: String::new(), label: String::new(),
log_content: text_editor::Content::default(), log_content: text_editor::Content::default(),
result_background: Color::from_rgb8(255,255,255),
sender, sender,
receiver, receiver,
} }
@ -84,13 +89,14 @@ enum Message {
pub fn add_log<'a>(msg: String) { pub fn add_log<'a>(msg: String) {
let time_now = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); let time_now = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
let really_msg = format!("{time_now} [Info]: {msg}"); let really_msg = format!("{time_now} [Info]: {msg}");
logs.lock().unwrap().push(really_msg); LOGS.lock().unwrap().push(really_msg);
} }
impl MainWindow { impl MainWindow {
fn update(&mut self, message: Message) -> Task<Message> { fn update(&mut self, message: Message) -> Task<Message> {
match message { match message {
Message::Start() => { Message::Start() => {
self.result_background = Color::from_rgb8(255,255,255);
self.start(self.sender.clone()); self.start(self.sender.clone());
Task::none() Task::none()
} }
@ -132,7 +138,8 @@ impl MainWindow {
} }
Message::ClearLog => { Message::ClearLog => {
self.log_content.perform(text_editor::Action::SelectAll); self.log_content.perform(text_editor::Action::SelectAll);
self.log_content.perform(text_editor::Action::Edit(Edit::Delete)); self.log_content
.perform(text_editor::Action::Edit(Edit::Backspace));
Task::none() Task::none()
} }
Message::AddLog(log) => { Message::AddLog(log) => {
@ -149,7 +156,7 @@ impl MainWindow {
Task::none() Task::none()
} }
Message::Tick => { Message::Tick => {
for log in logs.lock().unwrap().iter() { for log in LOGS.lock().unwrap().iter() {
for c in log.chars() { for c in log.chars() {
self.log_content self.log_content
.perform(text_editor::Action::Edit(Edit::Insert(c))) .perform(text_editor::Action::Edit(Edit::Insert(c)))
@ -161,7 +168,7 @@ impl MainWindow {
lines: line_size as i32, lines: line_size as i32,
}); });
} }
logs.lock().unwrap().clear(); LOGS.lock().unwrap().clear();
match self.receiver.try_recv() { match self.receiver.try_recv() {
Ok(event) => match event { Ok(event) => match event {
@ -185,11 +192,20 @@ impl MainWindow {
_ => Task::none(), _ => Task::none(),
}, },
Message::DownloadEnd((label, res)) => { Message::DownloadEnd((label, res)) => {
if !fs::exists("./uilog"){ if !fs::exists("./uilog").unwrap() {
fs::create_dir("./uilog").expect("Cant Create UiLog Folder"); fs::create_dir("./uilog").expect("Cant Create UiLog Folder");
} }
let mut file = fs::OpenOptions::new().create(true).append(true).open(format!("./uilog/{label}.txt")).unwrap(); let mut file = fs::OpenOptions::new()
.create(true)
.append(true)
.open(format!("./uilog/{label}.txt"))
.unwrap();
writeln!(file, "{:?}", self.log_content.text().clone()).unwrap(); writeln!(file, "{:?}", self.log_content.text().clone()).unwrap();
if res{
self.result_background = Color::from_rgb8(0, 255,0);
}else{
self.result_background = Color::from_rgb8(255, 0,0);
}
Task::none() Task::none()
} }
Message::WindowEvent(event) => { Message::WindowEvent(event) => {
@ -242,17 +258,23 @@ impl MainWindow {
self.selection, self.selection,
Message::TypeSelected Message::TypeSelected
), ),
checkbox("online", self.is_online).on_toggle(Message::OnlineChecked) checkbox("online", self.is_online).on_toggle(Message::OnlineChecked),
] ]
.spacing(10), .spacing(10),
row![label_input, button("Start").on_press(Message::Start())].spacing(10), row![label_input, button("Start").on_press(Message::Start())].spacing(10),
text_editor(&self.log_content) text_editor(&self.log_content)
.on_action(Message::LogContentChanged) .on_action(Message::LogContentChanged)
.height(Length::Fill) .height(Length::Fill),
progress_bar(0.0..=100.0, 0.0).style(|_|{
Style {
background: self.result_background.into(),
bar: self.result_background.into(),
border: Border::default(),
}
}),
] ]
.spacing(10) .spacing(10)
.padding(5); .padding(5);
let container = container(content).padding(20); let container = container(content).padding(20);
container.into() container.into()
} }
@ -338,7 +360,9 @@ impl MainWindow {
} }
add_log("擦除成功".to_string()); add_log("擦除成功".to_string());
add_log("正在下载BootLoader".to_string()); add_log("正在下载BootLoader".to_string());
if let Err(e) = download_wrapper.download(DownloadType::Bootloader,Some(label.clone())) { if let Err(e) =
download_wrapper.download(DownloadType::Bootloader, Some(label.clone()))
{
add_log("下载BootLoader失败".to_string()); add_log("下载BootLoader失败".to_string());
sender.send(Message::DownloadEnd((label, false))).unwrap(); sender.send(Message::DownloadEnd((label, false))).unwrap();
return; return;
@ -386,11 +410,11 @@ impl MainWindow {
sender.send(Message::DownloadEnd((label, false))).unwrap(); sender.send(Message::DownloadEnd((label, false))).unwrap();
return; return;
} }
sender.send(Message::DownloadEnd((label, false))).unwrap(); sender.send(Message::DownloadEnd((label, true))).unwrap();
return; return;
} else { } else {
add_log("当前为离线模式".to_string()); add_log("当前为离线模式".to_string());
sender.send(Message::DownloadEnd((label, false))).unwrap(); sender.send(Message::DownloadEnd((label, true))).unwrap();
return; return;
}; };
}); });
@ -425,7 +449,7 @@ impl MainWindow {
fs::write("./config.json", config_str).unwrap(); fs::write("./config.json", config_str).unwrap();
} }
fn read_config(&mut self) { fn read_config(&mut self) {
if !fs::exists("./config.json"){ if !fs::exists("./config.json").unwrap() {
return; return;
} }
let config_str = fs::read_to_string("./config.json").unwrap(); let config_str = fs::read_to_string("./config.json").unwrap();

View File

@ -36,9 +36,7 @@ impl MesService {
download_type: DownloadType, download_type: DownloadType,
) -> Result<bool, Error> { ) -> Result<bool, Error> {
match download_type { match download_type {
DownloadType::App=> { DownloadType::App => Ok(true),
Ok(true)
}
DownloadType::Rail => { DownloadType::Rail => {
let mut conn = self.pool.get_conn()?; let mut conn = self.pool.get_conn()?;
let check_result: Vec<String> = conn.query(format!( let check_result: Vec<String> = conn.query(format!(
@ -49,7 +47,7 @@ impl MesService {
} }
Ok(check_result[0] == "1") Ok(check_result[0] == "1")
} }
DownloadType::Bootloader => Err(Error::msg("Not implemented yet")) DownloadType::Bootloader => Err(Error::msg("Not implemented yet")),
} }
} }
@ -60,16 +58,20 @@ impl MesService {
download_type: DownloadType, download_type: DownloadType,
) -> Result<(), Error> { ) -> Result<(), Error> {
match download_type { match download_type {
DownloadType::Bootloader => { DownloadType::Bootloader => {}
}
DownloadType::App => { DownloadType::App => {
let mut conn = self.pool.get_conn()?; let mut conn = self.pool.get_conn()?;
let work_order_id: Vec<u32> = conn.query(format!( let work_order_id: Vec<u32> = conn.query(format!(
"SELECT `id` FROM wisun_ordertables WHERE `OrderId` = '{work_order}'" "SELECT `id` FROM wisun_ordertables WHERE `OrderId` = '{work_order}'"
))?; ))?;
if let Some(work_order_id) = work_order_id.first() { if let Some(work_order_id) = work_order_id.first() {
let _: Vec<String> = conn.exec(format!("INSERT OR UPDATE INTO {work_order} (`ID`,`Barcode`,`IMEI_1`, `Station1`) VALUES ({work_order_id},{label},{label}, '1')").to_string(), ())?; let _: Vec<String> = conn.exec(
format!(
"INSERT INTO {work_order} (`ID`, `Barcode`, `IMEI_1`, `Station1`) VALUES ({work_order_id}, {label}, {label}, '1') \
ON DUPLICATE KEY UPDATE `Barcode` = VALUES(`Barcode`), `IMEI_1` = VALUES(`IMEI_1`), `Station1` = VALUES(`Station1`)"
),
()
)?;
} else { } else {
return Ok(()); return Ok(());
} }