mod tokio_utils;
|
mod serial_port;
|
|
|
|
use std::sync::Mutex;
|
use tauri::async_runtime::spawn;
|
use tauri::Manager;
|
use tokio_utils::{set_complete,setup,SetupState};
|
use std::fs::OpenOptions;
|
use std::io::Write;
|
use chrono::Local;
|
|
|
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
|
|
|
#[tauri::command]
|
fn greet(name: &str) -> String {
|
format!("Hello, {}! You've been greeted from Rust!", name)
|
}
|
|
#[tauri::command]
|
fn log_data(app_handle: tauri::AppHandle, data: String) -> Result<(), String> {
|
let app_data_path = app_handle.path().app_data_dir()
|
.or_else(|_| Err("Could not get app data directory".to_string()))?;
|
|
let log_dir = app_data_path.join("logs");
|
if !log_dir.exists() {
|
std::fs::create_dir_all(&log_dir)
|
.map_err(|e| format!("Failed to create log directory: {}", e))?;
|
}
|
|
let current_date = Local::now().format("%Y-%m-%d").to_string();
|
let log_file_path = log_dir.join(format!("log_{}.txt", current_date));
|
|
println!("log_file_path: {:?}", log_file_path);
|
let mut file = OpenOptions::new()
|
.create(true)
|
.append(true)
|
.open(&log_file_path)
|
.map_err(|e| format!("Failed to open log file: {}", e))?;
|
|
let timestamp = Local::now().format("[%Y-%m-%d %H:%M:%S]").to_string();
|
writeln!(file, "{} {}", timestamp, data)
|
.map_err(|e| format!("Failed to write to log file: {}", e))?;
|
|
Ok(())
|
}
|
|
|
|
|
|
|
|
// #[cfg_attr(mobile, tauri::mobile_entry_point)]
|
// pub fn run() {
|
// tauri::Builder::default()
|
// .plugin(tauri_plugin_opener::init())
|
// .invoke_handler(tauri::generate_handler![greet])
|
// .run(tauri::generate_context!())
|
// .expect("error while running tauri application");
|
// }
|
|
// Our main entrypoint in a version 2 mobile compatible app
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
pub fn run() {
|
// Don't write code before Tauri starts, write it in the
|
// setup hook instead!
|
tauri::Builder::default()
|
// Register a `State` to be managed by Tauri
|
// We need write access to it so we wrap it in a `Mutex`
|
.manage(Mutex::new(SetupState {
|
frontend_task: false,
|
backend_task: false,
|
}))
|
// Add a command we can use to check
|
.invoke_handler(tauri::generate_handler![
|
greet,
|
set_complete,
|
serial_port::open_serial_port,
|
serial_port::close_serial_port,
|
serial_port::write_serial_data,
|
serial_port::list_serial_ports,
|
log_data
|
])
|
// Use the setup hook to execute setup related tasks
|
// Runs before the main loop, so no windows are yet created
|
.setup(|app| {
|
// Spawn setup as a non-blocking task so the windows can be
|
// created and ran while it executes
|
spawn(setup(app.handle().clone()));
|
// The hook expects an Ok result
|
Ok(())
|
})
|
// Run the app
|
.run(tauri::generate_context!())
|
.expect("error while running tauri application");
|
}
|