2024-12-11 14:30:30 +08:00
|
|
|
#![windows_subsystem = "windows"]
|
|
|
|
|
2024-12-09 10:33:01 +08:00
|
|
|
mod download_wrapper;
|
2024-12-10 18:03:31 +08:00
|
|
|
mod mes_service;
|
2024-12-09 10:33:01 +08:00
|
|
|
|
2024-12-11 11:40:05 +08:00
|
|
|
use crate::mes_service::MesService;
|
2024-12-10 18:03:31 +08:00
|
|
|
use crossbeam::channel;
|
|
|
|
use crossbeam::channel::Sender;
|
2024-12-11 11:40:05 +08:00
|
|
|
use download_wrapper::DownloadType;
|
2024-12-10 18:03:31 +08:00
|
|
|
use iced::application::Title;
|
2024-12-11 14:07:30 +08:00
|
|
|
use iced::widget::progress_bar::Style;
|
2024-12-06 17:57:22 +08:00
|
|
|
use iced::widget::text_editor::Action::Scroll;
|
|
|
|
use iced::widget::text_editor::Edit;
|
2024-12-11 14:07:30 +08:00
|
|
|
use iced::widget::{
|
|
|
|
button, checkbox, column, container, progress_bar, radio, row, text_editor, text_input,
|
|
|
|
};
|
2024-12-11 14:13:19 +08:00
|
|
|
use iced::{event, time, window, Border, Color, Element, Event, Length, Subscription, Task};
|
2024-12-10 19:54:35 +08:00
|
|
|
use serde_json::Map;
|
2024-12-11 11:40:05 +08:00
|
|
|
use std::any::Any;
|
|
|
|
use std::fs;
|
|
|
|
use std::io::Write;
|
2024-12-11 14:07:30 +08:00
|
|
|
use std::sync::{Arc, LazyLock, Mutex};
|
2024-12-10 18:03:31 +08:00
|
|
|
|
|
|
|
pub fn main() -> iced::Result {
|
2024-12-06 17:57:22 +08:00
|
|
|
iced::application("WisunDownload V0.1", MainWindow::update, MainWindow::view)
|
2024-12-09 10:33:01 +08:00
|
|
|
.subscription(MainWindow::subscription)
|
|
|
|
.exit_on_close_request(false)
|
2024-12-06 17:57:22 +08:00
|
|
|
.window_size((830.0, 600.0))
|
|
|
|
.run()
|
|
|
|
}
|
|
|
|
|
2024-12-11 14:07:30 +08:00
|
|
|
static LOGS: LazyLock<Arc<Mutex<Vec<String>>>> = LazyLock::new(|| Arc::new(Mutex::new(Vec::new())));
|
2024-12-10 18:03:31 +08:00
|
|
|
|
2024-12-06 17:57:22 +08:00
|
|
|
struct MainWindow {
|
|
|
|
is_online: bool,
|
|
|
|
mysql_config: MysqlConfig,
|
|
|
|
selection: Option<DownloadType>,
|
|
|
|
label: String,
|
|
|
|
log_content: text_editor::Content,
|
2024-12-10 19:54:35 +08:00
|
|
|
sender: Sender<Message>,
|
|
|
|
receiver: channel::Receiver<Message>,
|
2024-12-11 14:07:30 +08:00
|
|
|
result_background: Color
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
2024-12-09 10:33:01 +08:00
|
|
|
impl Default for MainWindow {
|
2024-12-10 18:03:31 +08:00
|
|
|
fn default() -> Self {
|
2024-12-11 11:40:05 +08:00
|
|
|
let (sender, receiver) = channel::unbounded();
|
2024-12-10 18:03:31 +08:00
|
|
|
Self {
|
|
|
|
is_online: true,
|
|
|
|
mysql_config: MysqlConfig::default(),
|
|
|
|
selection: None,
|
|
|
|
label: String::new(),
|
|
|
|
log_content: text_editor::Content::default(),
|
2024-12-11 14:07:30 +08:00
|
|
|
result_background: Color::from_rgb8(255,255,255),
|
2024-12-10 18:03:31 +08:00
|
|
|
sender,
|
2024-12-11 11:40:05 +08:00
|
|
|
receiver,
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
|
|
|
}
|
2024-12-09 10:33:01 +08:00
|
|
|
}
|
|
|
|
|
2024-12-06 17:57:22 +08:00
|
|
|
#[derive(Debug, Clone, Default)]
|
2024-12-10 18:03:31 +08:00
|
|
|
struct MysqlConfig {
|
2024-12-06 17:57:22 +08:00
|
|
|
ip: String,
|
|
|
|
port: String,
|
|
|
|
username: String,
|
|
|
|
password: String,
|
|
|
|
database: String,
|
|
|
|
work_order: String,
|
|
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
2024-12-10 18:03:31 +08:00
|
|
|
enum Message {
|
2024-12-06 17:57:22 +08:00
|
|
|
Start(),
|
|
|
|
IpChanged(String),
|
|
|
|
PortChanged(String),
|
|
|
|
UsernameChanged(String),
|
|
|
|
PasswordChanged(String),
|
|
|
|
DatabaseChanged(String),
|
|
|
|
WorkOrderChanged(String),
|
|
|
|
LabelChanged(String),
|
|
|
|
TypeSelected(DownloadType),
|
|
|
|
OnlineChecked(bool),
|
|
|
|
AddLog(String),
|
2024-12-09 10:33:01 +08:00
|
|
|
LogContentChanged(text_editor::Action),
|
|
|
|
SaveConfig,
|
2024-12-11 14:07:30 +08:00
|
|
|
DownloadEnd((String, bool)),
|
2024-12-10 18:03:31 +08:00
|
|
|
WindowEvent(Event),
|
2024-12-11 11:40:05 +08:00
|
|
|
ClearLog,
|
2024-12-10 18:03:31 +08:00
|
|
|
Tick,
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
2024-12-10 18:03:31 +08:00
|
|
|
pub fn add_log<'a>(msg: String) {
|
|
|
|
let time_now = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
|
|
|
|
let really_msg = format!("{time_now} [Info]: {msg}");
|
2024-12-11 14:07:30 +08:00
|
|
|
LOGS.lock().unwrap().push(really_msg);
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
|
2024-12-10 18:03:31 +08:00
|
|
|
impl MainWindow {
|
|
|
|
fn update(&mut self, message: Message) -> Task<Message> {
|
|
|
|
match message {
|
2024-12-06 17:57:22 +08:00
|
|
|
Message::Start() => {
|
2024-12-11 14:07:30 +08:00
|
|
|
self.result_background = Color::from_rgb8(255,255,255);
|
2024-12-10 18:03:31 +08:00
|
|
|
self.start(self.sender.clone());
|
2024-12-09 10:33:01 +08:00
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
Message::IpChanged(ip) => {
|
|
|
|
self.mysql_config.ip = ip;
|
2024-12-09 10:33:01 +08:00
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
Message::PortChanged(port) => {
|
|
|
|
self.mysql_config.port = port;
|
2024-12-09 10:33:01 +08:00
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
Message::UsernameChanged(username) => {
|
2024-12-09 10:33:01 +08:00
|
|
|
self.mysql_config.username = username;
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
Message::PasswordChanged(password) => {
|
2024-12-09 10:33:01 +08:00
|
|
|
self.mysql_config.password = password;
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
Message::DatabaseChanged(database) => {
|
2024-12-09 10:33:01 +08:00
|
|
|
self.mysql_config.database = database;
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
Message::WorkOrderChanged(work_order) => {
|
2024-12-09 10:33:01 +08:00
|
|
|
self.mysql_config.work_order = work_order;
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
Message::TypeSelected(download_type) => {
|
2024-12-09 10:33:01 +08:00
|
|
|
self.selection = Some(download_type);
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
Message::LabelChanged(label) => {
|
2024-12-09 10:33:01 +08:00
|
|
|
self.label = label;
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
Message::OnlineChecked(is_online) => {
|
2024-12-09 10:33:01 +08:00
|
|
|
self.is_online = is_online;
|
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-11 14:07:30 +08:00
|
|
|
Message::ClearLog => {
|
2024-12-11 11:40:05 +08:00
|
|
|
self.log_content.perform(text_editor::Action::SelectAll);
|
2024-12-11 14:07:30 +08:00
|
|
|
self.log_content
|
|
|
|
.perform(text_editor::Action::Edit(Edit::Backspace));
|
2024-12-11 11:40:05 +08:00
|
|
|
Task::none()
|
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
Message::AddLog(log) => {
|
|
|
|
for c in log.chars() {
|
2024-12-11 11:40:05 +08:00
|
|
|
self.log_content
|
|
|
|
.perform(text_editor::Action::Edit(Edit::Insert(c)))
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
self.log_content
|
|
|
|
.perform(text_editor::Action::Edit(Edit::Enter));
|
2024-12-06 17:57:22 +08:00
|
|
|
let line_size = self.log_content.lines().count();
|
2024-12-11 11:40:05 +08:00
|
|
|
self.log_content.perform(Scroll {
|
|
|
|
lines: line_size as i32,
|
|
|
|
});
|
2024-12-09 10:33:01 +08:00
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
Message::Tick => {
|
2024-12-11 14:07:30 +08:00
|
|
|
for log in LOGS.lock().unwrap().iter() {
|
2024-12-10 18:03:31 +08:00
|
|
|
for c in log.chars() {
|
2024-12-11 11:40:05 +08:00
|
|
|
self.log_content
|
|
|
|
.perform(text_editor::Action::Edit(Edit::Insert(c)))
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
self.log_content
|
|
|
|
.perform(text_editor::Action::Edit(Edit::Enter));
|
2024-12-10 18:03:31 +08:00
|
|
|
let line_size = self.log_content.lines().count();
|
2024-12-11 11:40:05 +08:00
|
|
|
self.log_content.perform(Scroll {
|
|
|
|
lines: line_size as i32,
|
|
|
|
});
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-11 14:07:30 +08:00
|
|
|
LOGS.lock().unwrap().clear();
|
2024-12-10 18:03:31 +08:00
|
|
|
|
|
|
|
match self.receiver.try_recv() {
|
2024-12-11 11:40:05 +08:00
|
|
|
Ok(event) => match event {
|
|
|
|
Message::DownloadEnd((label, res)) => {
|
|
|
|
let _ = self.update(Message::DownloadEnd((label, res)));
|
2024-12-10 19:54:35 +08:00
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
Err(channel::TryRecvError::Empty) => {}
|
2024-12-10 18:03:31 +08:00
|
|
|
Err(channel::TryRecvError::Disconnected) => {
|
|
|
|
println!("Disconnected");
|
|
|
|
}
|
|
|
|
};
|
2024-12-09 10:33:01 +08:00
|
|
|
Task::none()
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
Message::LogContentChanged(action) => match action {
|
|
|
|
Scroll { lines } => {
|
|
|
|
self.log_content.perform(Scroll { lines });
|
|
|
|
Task::none()
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
_ => Task::none(),
|
|
|
|
},
|
2024-12-11 14:07:30 +08:00
|
|
|
Message::DownloadEnd((label, res)) => {
|
|
|
|
if !fs::exists("./uilog").unwrap() {
|
2024-12-11 11:40:05 +08:00
|
|
|
fs::create_dir("./uilog").expect("Cant Create UiLog Folder");
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
2024-12-11 14:07:30 +08:00
|
|
|
let mut file = fs::OpenOptions::new()
|
|
|
|
.create(true)
|
|
|
|
.append(true)
|
|
|
|
.open(format!("./uilog/{label}.txt"))
|
|
|
|
.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);
|
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
Task::none()
|
|
|
|
}
|
|
|
|
Message::WindowEvent(event) => {
|
|
|
|
//println!("{:?}", event);
|
2024-12-11 14:13:19 +08:00
|
|
|
if let Event::Window(window::Event::Opened { position: _position, size: _size }) = event {
|
2024-12-10 19:54:35 +08:00
|
|
|
println!("Opened");
|
|
|
|
self.read_config();
|
2024-12-11 14:13:19 +08:00
|
|
|
return Task::none()
|
2024-12-10 19:54:35 +08:00
|
|
|
};
|
2024-12-10 18:03:31 +08:00
|
|
|
if let Event::Window(window::Event::CloseRequested) = event {
|
|
|
|
println!("Request Close");
|
2024-12-10 19:54:35 +08:00
|
|
|
self.save_config();
|
2024-12-10 18:03:31 +08:00
|
|
|
return window::get_latest().and_then(window::close);
|
|
|
|
};
|
|
|
|
Task::none()
|
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
_ => Task::none(),
|
2024-12-06 17:57:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-10 18:03:31 +08:00
|
|
|
fn view(&self) -> Element<Message> {
|
2024-12-11 11:40:05 +08:00
|
|
|
let ip_input = text_input("IP Address", &self.mysql_config.ip).on_input(Message::IpChanged);
|
|
|
|
let port_input = text_input("Port", &self.mysql_config.port).on_input(Message::PortChanged);
|
|
|
|
let username_input =
|
|
|
|
text_input("Username", &self.mysql_config.username).on_input(Message::UsernameChanged);
|
|
|
|
let password_input =
|
|
|
|
text_input("Password", &self.mysql_config.password).on_input(Message::PasswordChanged);
|
|
|
|
let database_input =
|
|
|
|
text_input("Database", &self.mysql_config.database).on_input(Message::DatabaseChanged);
|
|
|
|
let work_order_input = text_input("WorkOrder", &self.mysql_config.work_order)
|
|
|
|
.on_input(Message::WorkOrderChanged);
|
|
|
|
let label_input = text_input("label", &self.label)
|
|
|
|
.on_input(Message::LabelChanged)
|
|
|
|
.on_submit(Message::Start())
|
|
|
|
.width(Length::Fill);
|
2024-12-06 17:57:22 +08:00
|
|
|
let content = column![
|
|
|
|
row![ip_input, port_input].spacing(5),
|
|
|
|
row![username_input, password_input].spacing(5),
|
|
|
|
row![database_input, work_order_input].spacing(5),
|
|
|
|
row![
|
2024-12-11 11:40:05 +08:00
|
|
|
radio(
|
|
|
|
"App",
|
|
|
|
DownloadType::App,
|
|
|
|
self.selection,
|
|
|
|
Message::TypeSelected
|
|
|
|
),
|
|
|
|
radio(
|
|
|
|
"Cal",
|
|
|
|
DownloadType::Rail,
|
|
|
|
self.selection,
|
|
|
|
Message::TypeSelected
|
|
|
|
),
|
2024-12-11 14:07:30 +08:00
|
|
|
checkbox("online", self.is_online).on_toggle(Message::OnlineChecked),
|
2024-12-11 11:40:05 +08:00
|
|
|
]
|
|
|
|
.spacing(10),
|
|
|
|
row![label_input, button("Start").on_press(Message::Start())].spacing(10),
|
|
|
|
text_editor(&self.log_content)
|
|
|
|
.on_action(Message::LogContentChanged)
|
2024-12-11 14:07:30 +08:00
|
|
|
.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(),
|
|
|
|
}
|
|
|
|
}),
|
2024-12-11 11:40:05 +08:00
|
|
|
]
|
|
|
|
.spacing(10)
|
|
|
|
.padding(5);
|
2024-12-06 17:57:22 +08:00
|
|
|
let container = container(content).padding(20);
|
|
|
|
container.into()
|
|
|
|
}
|
|
|
|
|
2024-12-09 10:33:01 +08:00
|
|
|
fn subscription(&self) -> Subscription<Message> {
|
2024-12-10 18:03:31 +08:00
|
|
|
Subscription::batch(vec![
|
|
|
|
event::listen().map(Message::WindowEvent),
|
2024-12-11 11:40:05 +08:00
|
|
|
time::every(time::Duration::from_millis(50)).map(|_| Message::Tick),
|
2024-12-10 18:03:31 +08:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
2024-12-11 11:40:05 +08:00
|
|
|
fn start(&mut self, sender: Sender<Message>) -> () {
|
2024-12-10 18:03:31 +08:00
|
|
|
let mes_config = self.mysql_config.clone();
|
|
|
|
let label = self.label.clone();
|
|
|
|
let download_type: DownloadType = self.selection.unwrap();
|
|
|
|
let online = self.is_online.clone();
|
2024-12-11 11:40:05 +08:00
|
|
|
if label == "" {
|
2024-12-10 20:04:24 +08:00
|
|
|
add_log("请输入Label".to_string());
|
|
|
|
return;
|
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
std::thread::spawn(move || {
|
|
|
|
let mut download_wrapper = download_wrapper::DownloadWrapper::new();
|
|
|
|
let mut mes_service: MesService;
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::ClearLog).unwrap();
|
2024-12-10 18:03:31 +08:00
|
|
|
if online {
|
|
|
|
add_log("当前为在线模式, 正在连接数据库".to_string());
|
2024-12-11 11:40:05 +08:00
|
|
|
match MesService::new(
|
|
|
|
mes_config.ip.clone(),
|
|
|
|
mes_config.port.clone(),
|
|
|
|
mes_config.username.clone(),
|
|
|
|
mes_config.password.clone(),
|
|
|
|
mes_config.database.clone(),
|
|
|
|
) {
|
2024-12-10 20:04:24 +08:00
|
|
|
Ok(service) => mes_service = service,
|
2024-12-11 11:40:05 +08:00
|
|
|
Err(e) => {
|
2024-12-10 20:04:24 +08:00
|
|
|
add_log(format!("连接数据库失败: {:?}", e.to_string()));
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 20:04:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2024-12-10 18:03:31 +08:00
|
|
|
add_log("连接成功".to_string());
|
|
|
|
add_log("正在过站检测".to_string());
|
2024-12-11 11:40:05 +08:00
|
|
|
let check_result = mes_service.check_station(
|
|
|
|
mes_config.work_order.clone(),
|
|
|
|
label.clone(),
|
|
|
|
download_type,
|
|
|
|
);
|
2024-12-10 18:03:31 +08:00
|
|
|
if let Err(res) = check_result {
|
2024-12-10 20:04:24 +08:00
|
|
|
add_log(format!("过站检测失败: {:?}", res.to_string()));
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-10 18:03:31 +08:00
|
|
|
} else if let Ok(res) = check_result {
|
|
|
|
if !res {
|
|
|
|
add_log("过站检测失败".to_string());
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
|
|
|
add_log("过站检测成功".to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
add_log("正在检查JLink连接".to_string());
|
2024-12-11 14:13:19 +08:00
|
|
|
if let Err(_) = download_wrapper.check_jlink() {
|
2024-12-10 18:03:31 +08:00
|
|
|
add_log("JLink检查失败, 请检查是否安装驱动或连接是否异常".to_string());
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
|
|
|
add_log("JLink检查成功".to_string());
|
|
|
|
add_log("正在检查设备连接".to_string());
|
2024-12-11 14:13:19 +08:00
|
|
|
if let Err(_) = download_wrapper.check_device() {
|
2024-12-10 18:03:31 +08:00
|
|
|
add_log("设备检查失败, 请检查设备是否连接".to_string());
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
|
|
|
add_log("设备检查成功".to_string());
|
|
|
|
match download_type {
|
2024-12-11 11:40:05 +08:00
|
|
|
DownloadType::Bootloader => {}
|
|
|
|
DownloadType::App => {
|
2024-12-10 18:03:31 +08:00
|
|
|
add_log("正在擦除Flash".to_string());
|
2024-12-11 14:13:19 +08:00
|
|
|
if let Err(_) = download_wrapper.erase(Some(label.clone())) {
|
2024-12-10 18:03:31 +08:00
|
|
|
add_log("擦除失败".to_string());
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
|
|
|
add_log("擦除成功".to_string());
|
|
|
|
add_log("正在下载BootLoader".to_string());
|
2024-12-11 14:13:19 +08:00
|
|
|
if let Err(_) =
|
2024-12-11 14:07:30 +08:00
|
|
|
download_wrapper.download(DownloadType::Bootloader, Some(label.clone()))
|
|
|
|
{
|
2024-12-10 18:03:31 +08:00
|
|
|
add_log("下载BootLoader失败".to_string());
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
add_log("下载BootLoader成功".to_string());
|
2024-12-10 18:03:31 +08:00
|
|
|
add_log("正在下载App".to_string());
|
2024-12-11 14:13:19 +08:00
|
|
|
if let Err(_) = download_wrapper.download(download_type, Some(label.clone())) {
|
2024-12-10 18:03:31 +08:00
|
|
|
add_log("下载App失败".to_string());
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
add_log("下载App成功".to_string());
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
DownloadType::Rail => {
|
2024-12-10 18:03:31 +08:00
|
|
|
add_log("正在下载Rail".to_string());
|
2024-12-11 14:13:19 +08:00
|
|
|
if let Err(_) = download_wrapper.download(download_type, Some(label.clone())) {
|
2024-12-10 18:03:31 +08:00
|
|
|
add_log("下载Rail失败".to_string());
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
add_log("下载Rail成功".to_string());
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return if online {
|
2024-12-11 11:40:05 +08:00
|
|
|
match MesService::new(
|
|
|
|
mes_config.ip.clone(),
|
|
|
|
mes_config.port.clone(),
|
|
|
|
mes_config.username.clone(),
|
|
|
|
mes_config.password.clone(),
|
|
|
|
mes_config.database.clone(),
|
|
|
|
) {
|
2024-12-10 20:04:24 +08:00
|
|
|
Ok(service) => mes_service = service,
|
2024-12-11 11:40:05 +08:00
|
|
|
Err(e) => {
|
2024-12-10 20:04:24 +08:00
|
|
|
add_log(format!("连接数据库失败: {:?}", e.to_string()));
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 20:04:24 +08:00
|
|
|
return;
|
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
}
|
|
|
|
add_log("正在上传数据".to_string());
|
|
|
|
let update_result =
|
|
|
|
mes_service.update_station(mes_config.work_order, label.clone(), download_type);
|
2024-12-10 18:03:31 +08:00
|
|
|
if let Err(e) = update_result {
|
2024-12-10 20:04:24 +08:00
|
|
|
add_log(format!("上传失败: {:?}", e.to_string()));
|
2024-12-11 11:40:05 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, false))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-10 18:03:31 +08:00
|
|
|
}
|
2024-12-11 14:07:30 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, true))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-10 18:03:31 +08:00
|
|
|
} else {
|
|
|
|
add_log("当前为离线模式".to_string());
|
2024-12-11 14:07:30 +08:00
|
|
|
sender.send(Message::DownloadEnd((label, true))).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
2024-12-11 11:40:05 +08:00
|
|
|
};
|
2024-12-10 18:03:31 +08:00
|
|
|
});
|
2024-12-09 10:33:01 +08:00
|
|
|
}
|
2024-12-10 19:54:35 +08:00
|
|
|
|
2024-12-11 11:40:05 +08:00
|
|
|
fn save_config(&mut self) {
|
2024-12-10 19:54:35 +08:00
|
|
|
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());
|
2024-12-11 11:40:05 +08:00
|
|
|
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(),
|
|
|
|
);
|
2024-12-10 19:54:35 +08:00
|
|
|
config.insert("is_online".to_string(), self.is_online.clone().into());
|
2024-12-11 11:40:05 +08:00
|
|
|
config.insert(
|
|
|
|
"selection".to_string(),
|
|
|
|
self.selection.unwrap().to_string().into(),
|
|
|
|
);
|
2024-12-10 19:54:35 +08:00
|
|
|
let config_str = serde_json::to_string(&config).unwrap();
|
2024-12-11 11:40:05 +08:00
|
|
|
fs::write("./config.json", config_str).unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
fn read_config(&mut self) {
|
2024-12-11 14:07:30 +08:00
|
|
|
if !fs::exists("./config.json").unwrap() {
|
2024-12-10 19:54:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
let config_str = fs::read_to_string("./config.json").unwrap();
|
2024-12-10 19:54:35 +08:00
|
|
|
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();
|
2024-12-11 11:40:05 +08:00
|
|
|
match config["selection"].as_str().unwrap() {
|
|
|
|
"BootLoader" => {
|
2024-12-10 19:54:35 +08:00
|
|
|
self.selection = Some(DownloadType::Bootloader);
|
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
"App" => {
|
2024-12-10 19:54:35 +08:00
|
|
|
self.selection = Some(DownloadType::App);
|
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
"Rail" => {
|
2024-12-10 19:54:35 +08:00
|
|
|
self.selection = Some(DownloadType::Rail);
|
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
_ => {
|
2024-12-10 19:54:35 +08:00
|
|
|
self.selection = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-12-11 11:40:05 +08:00
|
|
|
}
|