feat: Play sound
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use std::sync:: Mutex;
|
||||
use std::io::ErrorKind;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
use tauri::State;
|
||||
use tauri::Manager;
|
||||
use std::fs::File;
|
||||
use std::io::BufReader;
|
||||
use rodio::{Decoder, OutputStream, Sink};
|
||||
|
||||
#[derive(Default)]
|
||||
struct PortMap(Mutex<HashMap<String, Box<dyn serialport::SerialPort>>>);
|
||||
|
||||
#[derive(Clone, serde::Serialize)]
|
||||
struct PayloadPortState {
|
||||
open: bool,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn write_serial(input: &str, port_name: &str, port_map: State<'_, PortMap>) -> Result<(), String> {
|
||||
match port_map.0.lock().unwrap().get(port_name) {
|
||||
@@ -24,11 +34,12 @@ async fn write_serial(input: &str, port_name: &str, port_map: State<'_, PortMap>
|
||||
}
|
||||
}
|
||||
None => {
|
||||
Ok(())
|
||||
Err(String::from("Port is closed"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[tauri::command]
|
||||
fn get_serial_ports() -> Vec<String> {
|
||||
let ports = serialport::available_ports().expect("No ports found");
|
||||
@@ -42,16 +53,29 @@ fn get_serial_ports() -> Vec<String> {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn open_port(_app_handle: tauri::AppHandle, port_map: State<'_, PortMap>) -> Result<(), String> {
|
||||
match serialport::new("/dev/ttyUSB0", 9600).timeout(Duration::from_millis(10)).open() {
|
||||
Ok(port) => {
|
||||
port_map.0.lock().unwrap().insert("/dev/ttyUSB0".to_string(), port.try_clone().expect("Error cloning"));
|
||||
fn close_port(app_handle: tauri::AppHandle, port_name: &str, port_map: State<'_, PortMap>) {
|
||||
match port_map.0.lock().unwrap().remove(port_name) {
|
||||
Some(_) => {
|
||||
println!("Port {} closed", port_name);
|
||||
app_handle.emit_all("port-state", PayloadPortState { open: false }).unwrap();
|
||||
},
|
||||
None => println!("Port {} was already closed", port_name),
|
||||
}
|
||||
}
|
||||
|
||||
println!("Port is open");
|
||||
#[tauri::command]
|
||||
async fn open_port(app_handle: tauri::AppHandle, port_name: &str, baud: u32, port_map: State<'_, PortMap>) -> Result<(), String> {
|
||||
match serialport::new(port_name, baud).timeout(Duration::from_millis(10)).open() {
|
||||
Ok(port) => {
|
||||
port_map.0.lock().unwrap().insert(port_name.to_string(), port.try_clone().expect("Error cloning"));
|
||||
|
||||
let mut clone = port.try_clone().expect("Failed to clone");
|
||||
let mut buffer: Vec<u8> = vec![0; 1024];
|
||||
|
||||
println!("Port {} is open", port_name);
|
||||
|
||||
app_handle.emit_all("port-state", PayloadPortState { open: true }).unwrap();
|
||||
|
||||
loop {
|
||||
match clone.read(buffer.as_mut_slice()) {
|
||||
Ok(bytes_read) => {
|
||||
@@ -60,6 +84,10 @@ async fn open_port(_app_handle: tauri::AppHandle, port_map: State<'_, PortMap>)
|
||||
let data = String::from_utf8_lossy(data).to_string();
|
||||
if !data.trim().is_empty() {
|
||||
println!("{}", data.trim());
|
||||
if data.trim() == "1" {
|
||||
play_sound()
|
||||
// app_handle.emit_all("button-on", PayloadPortState { open: true }).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -70,14 +98,27 @@ async fn open_port(_app_handle: tauri::AppHandle, port_map: State<'_, PortMap>)
|
||||
}
|
||||
},
|
||||
Err(err) => return Err(err.to_string()),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
fn play_sound() {
|
||||
thread::spawn(|| {
|
||||
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
|
||||
let sink = Sink::try_new(&stream_handle).unwrap();
|
||||
|
||||
let file = BufReader::new(File::open("assets/audio.mp3").unwrap());
|
||||
let source = Decoder::new(file).unwrap();
|
||||
|
||||
sink.append(source);
|
||||
|
||||
sink.sleep_until_end();
|
||||
});
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.manage(PortMap::default())
|
||||
.invoke_handler(tauri::generate_handler![get_serial_ports, write_serial, open_port])
|
||||
.invoke_handler(tauri::generate_handler![get_serial_ports, write_serial, open_port, close_port])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
Reference in New Issue
Block a user