逻辑优化, 添加配置(未完成), db记录逻辑接入
This commit is contained in:
parent
8a12c47a1f
commit
b2de036c61
25
Cargo.lock
generated
25
Cargo.lock
generated
@ -376,6 +376,12 @@ dependencies = [
|
||||
"mach2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.69"
|
||||
@ -769,6 +775,12 @@ dependencies = [
|
||||
"semver",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "0.3.3"
|
||||
@ -816,6 +828,17 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.120"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serialport"
|
||||
version = "4.4.0"
|
||||
@ -1109,6 +1132,8 @@ dependencies = [
|
||||
"log",
|
||||
"once_cell 0.1.8",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serialport",
|
||||
]
|
||||
|
||||
|
@ -16,6 +16,8 @@ chrono = "0.4.38"
|
||||
log = "0.4.22"
|
||||
once_cell = "0.1.8"
|
||||
lazy_static = "1.5.0"
|
||||
serde= { version = "1.0.203", features = ["derive"] }
|
||||
serde_json="1.0.120"
|
||||
|
||||
[build-dependencies]
|
||||
fl2rust = "0.5.19"
|
||||
|
17
src/models/config_model.rs
Normal file
17
src/models/config_model.rs
Normal file
@ -0,0 +1,17 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub(crate) struct ConfigModel{
|
||||
pub current_line: usize,
|
||||
pub excel_path: String,
|
||||
pub last_com: String,
|
||||
}
|
||||
|
||||
impl Default for ConfigModel {
|
||||
fn default() -> Self {
|
||||
ConfigModel {
|
||||
current_line: 0,
|
||||
excel_path: "".to_string(),
|
||||
last_com: "".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
@ -1 +1,2 @@
|
||||
pub(crate) mod work_model;
|
||||
pub(crate) mod work_model;
|
||||
pub(crate) mod config_model;
|
13
src/services/config_service.rs
Normal file
13
src/services/config_service.rs
Normal file
@ -0,0 +1,13 @@
|
||||
use anyhow::Result;
|
||||
use crate::models::config_model::ConfigModel;
|
||||
|
||||
pub(crate) struct ConfigService;
|
||||
|
||||
impl ConfigService {
|
||||
pub fn save_config(model: ConfigModel) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
pub fn load_config() -> Result<ConfigModel> {
|
||||
todo!()
|
||||
}
|
||||
}
|
@ -57,6 +57,10 @@ impl ExcelService {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn roll_back(&mut self){
|
||||
self.current_line -= 1;
|
||||
}
|
||||
|
||||
pub fn get_next_work_model(&mut self) -> Result<WorkModel, String> {
|
||||
let mut work_model = WorkModel::default();
|
||||
if let Some(imei) = self.sheet.get((self.current_line, 0)) {
|
||||
|
@ -3,4 +3,5 @@ pub(crate) mod sqlite_service;
|
||||
pub(crate) mod excel_service;
|
||||
pub(crate) mod ui_service;
|
||||
pub(crate) mod work_service;
|
||||
pub(crate) mod log_service;
|
||||
pub(crate) mod log_service;
|
||||
pub(crate) mod config_service;
|
@ -19,7 +19,9 @@ impl Default for SqliteService {
|
||||
impl SqliteService {
|
||||
pub fn new(conn_str: String) -> Result<Self> {
|
||||
let conn = Connection::open(conn_str)?;
|
||||
Ok(Self { conn })
|
||||
let this = Self { conn };
|
||||
this.create_table()?;
|
||||
return Ok(this);
|
||||
}
|
||||
|
||||
pub fn create_table(&self) -> Result<()> {
|
||||
@ -45,13 +47,13 @@ impl SqliteService {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn check_sn(&self, sn: &str) -> Result<bool> {
|
||||
pub fn check_sn_has_insert(&self, sn: &str) -> Result<bool> {
|
||||
let mut stmt = self.conn.prepare("SELECT sn FROM write_results WHERE sn = ?1")?;
|
||||
let mut rows = stmt.query(&[&sn])?;
|
||||
Ok(rows.next()?.is_some())
|
||||
}
|
||||
pub fn check_imei(&self, imei: &str) -> Result<bool> {
|
||||
let mut stmt = self.conn.prepare("SELECT sn FROM write_results WHERE sn = ?1")?;
|
||||
pub fn check_imei_has_insert(&self, imei: &str) -> Result<bool> {
|
||||
let mut stmt = self.conn.prepare("SELECT imei FROM write_results WHERE imei = ?1")?;
|
||||
let mut rows = stmt.query(&[&imei])?;
|
||||
Ok(rows.next()?.is_some())
|
||||
}
|
||||
|
@ -28,12 +28,20 @@ impl UiService {
|
||||
self.init_log();
|
||||
}
|
||||
|
||||
fn init_exit_callback(&mut self){
|
||||
let ui_rc = Rc::clone(&self.ui);
|
||||
let work_service_rc = Rc::clone(&self.work_service);
|
||||
ui_rc.borrow_mut().main_window.set_callback(move |_|{
|
||||
let mut work_service = work_service_rc.borrow_mut();
|
||||
work_service.save_config();
|
||||
});
|
||||
}
|
||||
|
||||
fn init_set_line_btn(&mut self) {
|
||||
let ui_rc = Rc::clone(&self.ui);
|
||||
let ui_rc_clone = Rc::clone(&ui_rc);
|
||||
let work_service_rc = Rc::clone(&self.work_service);
|
||||
ui_rc.borrow_mut().set_lines_btn.set_callback(move |_| {
|
||||
let mut work_service = work_service_rc.borrow_mut();
|
||||
let mut ui_clone = ui_rc_clone.clone();
|
||||
let mut set_dialog = UserInterface1::make_set_line_window();
|
||||
let work_service_rc_clone = work_service_rc.clone();
|
||||
@ -81,7 +89,9 @@ impl UiService {
|
||||
let current_line = &work_service.get_current_line().to_string();
|
||||
ui.current_line_textbox.set_value(current_line);
|
||||
}
|
||||
Err(msg) => { fltk::dialog::message_default(&msg) }
|
||||
Err(msg) => {
|
||||
work_service.roll_back();
|
||||
fltk::dialog::message_default(&msg) }
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -89,6 +99,10 @@ impl UiService {
|
||||
|
||||
fn init_log(&mut self) {
|
||||
let ui_rc = Rc::clone(&self.ui);
|
||||
if self.work_service.borrow_mut().config_model.last_com != ""{
|
||||
ui_rc.borrow_mut().com_textbox.set_value(&self.work_service.borrow_mut().config_model.last_com);
|
||||
}
|
||||
|
||||
let text_buffer = TextBuffer::default();
|
||||
ui_rc.borrow_mut().log_content.set_buffer(text_buffer.clone());
|
||||
let text_buffer_arc = Arc::new(Mutex::new(text_buffer));
|
||||
|
@ -1,23 +1,31 @@
|
||||
use std::ptr::null;
|
||||
use anyhow::Result;
|
||||
use log::Log;
|
||||
use crate::models::work_model::WorkModel;
|
||||
use crate::services::{serial_service::SerialService, sqlite_service::SqliteService, excel_service::ExcelService};
|
||||
use crate::models::config_model::ConfigModel;
|
||||
use crate::services::{serial_service::SerialService, sqlite_service::SqliteService, excel_service::ExcelService, config_service::ConfigService};
|
||||
use crate::services::log_service::{FltkLogger, init_logger, LOGGER};
|
||||
|
||||
pub struct WorkService {
|
||||
pub(crate) serial_service: Option<SerialService>,
|
||||
sqlite_service: SqliteService,
|
||||
pub(crate) excel_service: Option<ExcelService>,
|
||||
sqlite_service: SqliteService,
|
||||
pub(crate) config_model: ConfigModel,
|
||||
}
|
||||
|
||||
impl WorkService {
|
||||
pub fn new() -> Self {
|
||||
init_logger().unwrap();
|
||||
WorkService {
|
||||
let config = WorkService::load_config().unwrap_or_else(|_| ConfigModel::default());
|
||||
let mut this = WorkService {
|
||||
serial_service: Option::None,
|
||||
sqlite_service: SqliteService::new("data.db".to_string()).unwrap(),
|
||||
excel_service: Option::None,
|
||||
config_model: config,
|
||||
};
|
||||
if this.config_model.excel_path != "" {
|
||||
this.init_excel_service(this.config_model.excel_path.clone()).unwrap();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
pub fn get_port_names() -> Vec<String> {
|
||||
//maybe should not use unwrap
|
||||
@ -27,6 +35,7 @@ impl WorkService {
|
||||
pub fn init_excel_service(&mut self, path: String) -> Result<bool, String> {
|
||||
return if let Ok(excel_service) = ExcelService::new(&path) {
|
||||
self.excel_service = Some(excel_service);
|
||||
self.config_model.excel_path = path;
|
||||
Ok(true)
|
||||
} else {
|
||||
Err("Cant Init ExcelService".to_string())
|
||||
@ -36,6 +45,15 @@ impl WorkService {
|
||||
pub fn write_and_check(&mut self, model: WorkModel) -> Result<bool, String> {
|
||||
let msg = &format!("{}:{}:{}", model.imei, model.sn, model.line_number);
|
||||
log::info!("Write Start");
|
||||
log::info!("Check Imei From DB");
|
||||
match self.sqlite_service.check_imei_has_insert(&model.imei) {
|
||||
Ok(true) => {
|
||||
log::error!("Imei Exist");
|
||||
self.serial_service = None;
|
||||
return Err("Imei Exist".to_string());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if let Err(msg) = self.write_imei(model.imei.clone()) {
|
||||
log::error!("Write Imei Error:{}", msg);
|
||||
return Err(msg);
|
||||
@ -58,6 +76,10 @@ impl WorkService {
|
||||
}
|
||||
log::info!("Check End");
|
||||
self.serial_service = None;
|
||||
match self.sqlite_service.insert(model) {
|
||||
Err(msg) => { log::error!("Insert Error:{}",msg); }
|
||||
_ => {}
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
@ -68,24 +90,30 @@ impl WorkService {
|
||||
return 0;
|
||||
}
|
||||
|
||||
pub fn set_current_line(&mut self, line: usize)->Result<(), String>{
|
||||
if let Some(service) = &mut self.excel_service{
|
||||
pub fn set_current_line(&mut self, line: usize) -> Result<(), String> {
|
||||
if let Some(service) = &mut self.excel_service {
|
||||
return service.set_current_line(line);
|
||||
}else{
|
||||
} else {
|
||||
Err("Cant Set Line".to_string())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn get_next_work_model(&mut self) -> Option<WorkModel> {
|
||||
if let Some(service) = &mut self.excel_service {
|
||||
if let Ok(model) = service.get_next_work_model() {
|
||||
self.config_model.current_line = service.get_current_line();
|
||||
return Some(model);
|
||||
} else { return None; }
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
pub fn roll_back(&mut self) {
|
||||
return if let Some(service) = &mut self.excel_service {
|
||||
service.roll_back();
|
||||
};
|
||||
}
|
||||
|
||||
pub fn init_sqlite_service(&mut self, db_path: String) -> Result<bool, String> {
|
||||
return if let Ok(sqlite_service) = SqliteService::new(db_path) {
|
||||
self.sqlite_service = sqlite_service;
|
||||
@ -98,6 +126,7 @@ impl WorkService {
|
||||
pub fn init_serial_service(&mut self, port_name: String) -> Result<bool, String> {
|
||||
return if let Ok(serial_service) = SerialService::new(&port_name) {
|
||||
self.serial_service = Some(*serial_service);
|
||||
self.config_model.last_com = port_name;
|
||||
Ok(true)
|
||||
} else {
|
||||
Err("Cant Init SerialService".to_string())
|
||||
@ -134,4 +163,12 @@ impl WorkService {
|
||||
}
|
||||
return Err("Cant Init ServialService".to_string());
|
||||
}
|
||||
|
||||
pub fn save_config(&self) {
|
||||
ConfigService::save_config(self.config_model.clone()).unwrap()
|
||||
}
|
||||
|
||||
fn load_config() -> Result<ConfigModel> {
|
||||
return ConfigService::load_config();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user