frontend fixes & db rewrite with serde

This commit is contained in:
Priler 2026-01-04 09:00:51 +05:00
parent 61b7a79455
commit 091a41ac33
37 changed files with 910 additions and 663 deletions

View file

@ -1,33 +1,20 @@
use pv_recorder::RecorderBuilder;
use jarvis_core::recorder;
// use rodio::{Decoder, OutputStream, Sink};
use std::path::PathBuf;
use jarvis_core::audio;
#[tauri::command]
pub fn pv_get_audio_devices() -> Vec<String> {
let audio_devices = RecorderBuilder::default().get_audio_devices();
match audio_devices {
Ok(audio_devices) => audio_devices,
Err(err) => panic!("Failed to get audio devices: {}", err),
}
recorder::get_audio_devices()
}
#[tauri::command]
pub fn pv_get_audio_device_name(idx: i32) -> String {
let audio_devices = RecorderBuilder::default().get_audio_devices();
let mut first_device: String = String::new();
match audio_devices {
Ok(audio_devices) => {
for (_idx, device) in audio_devices.iter().enumerate() {
if idx as usize == _idx {
return device.to_string();
}
if _idx == 0 {
first_device = device.to_string()
}
}
}
Err(err) => panic!("Failed to get audio devices: {}", err),
};
// return first device as default, if none were matched
first_device
recorder::get_audio_device_name(idx)
}
#[tauri::command(async)]
pub fn play_sound(filename: &str) {
let path = PathBuf::from(filename);
audio::play_sound(&path);
}

View file

@ -1,19 +1,62 @@
use crate::DB;
use jarvis_core::{db, DB};
use crate::AppState;
#[tauri::command]
pub fn db_read(key: &str) -> String {
if let Some(value) = DB.lock().unwrap().get::<String>(key) {
return value
}
pub fn db_read(state: tauri::State<'_, AppState>, key: &str) -> String {
let settings = state.db.read();
String::from("")
match key {
"selected_microphone" => settings.microphone.to_string(),
"assistant_voice" => settings.voice.clone(),
"selected_wake_word_engine" => format!("{:?}", settings.wake_word_engine),
"speech_to_text_engine" => format!("{:?}", settings.speech_to_text_engine),
"api_key__picovoice" => settings.api_keys.picovoice.clone(),
"api_key__openai" => settings.api_keys.openai.clone(),
_ => String::new(),
}
}
#[tauri::command]
pub fn db_write(key: &str, val: &str) -> bool {
if let Ok(_) = DB.lock().unwrap().set(key, &val) {
true
} else {
false
pub fn db_write(state: tauri::State<'_, AppState>, key: &str, val: &str) -> bool {
let snapshot = {
let mut settings = state.db.write();
match key {
"selected_microphone" => {
if let Ok(v) = val.parse::<i32>() {
// info!("MICROPHONE changed: {}", v);
settings.microphone = v;
} else {
return false;
}
}
"assistant_voice" => {
settings.voice = val.to_string();
}
"selected_wake_word_engine" => {
match val.to_lowercase().as_str() {
"rustpotter" => settings.wake_word_engine = jarvis_core::config::structs::WakeWordEngine::Rustpotter,
"vosk" => settings.wake_word_engine = jarvis_core::config::structs::WakeWordEngine::Vosk,
"porcupine" => settings.wake_word_engine = jarvis_core::config::structs::WakeWordEngine::Porcupine,
_ => return false,
}
}
"api_key__picovoice" => {
settings.api_keys.picovoice = val.to_string();
}
"api_key__openai" => {
settings.api_keys.openai = val.to_string();
}
_ => return false,
}
settings.clone()
};
// save to disk
if let Err(e) = db::save_settings(&snapshot) {
info!("SETTINGS NOT SAVED");
}
true
}

View file

@ -1,5 +1,4 @@
use crate::config;
use crate::APP_LOG_DIR;
use jarvis_core::{config, APP_LOG_DIR};
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
@ -50,5 +49,7 @@ pub fn get_feedback_link() -> String {
#[tauri::command]
pub fn get_log_file_path() -> String {
format!("{}", APP_LOG_DIR.lock().unwrap())
APP_LOG_DIR.get()
.map(|p| p.display().to_string())
.unwrap_or_else(|| "unknown".to_string())
}

View file

@ -1,3 +1,8 @@
#[cfg(target_os = "linux")]
use std::fs::metadata;
#[cfg(target_os = "linux")]
use std::path::PathBuf;
use std::process::Command;
// taken from https://github.com/tauri-apps/tauri/issues/4062#issuecomment-1338048169

View file

@ -7,6 +7,7 @@ extern crate systemstat;
use std::thread;
use std::time::Duration;
use systemstat::{Platform, System};
use lazy_static::lazy_static;
lazy_static! {
static ref SYS: System = System::new();

View file

@ -1,29 +0,0 @@
use std::fs::File;
use std::io::BufReader;
use rodio::{Decoder, OutputStream, Sink};
#[tauri::command(async)]
pub fn play_sound(filename: &str, sleep: bool) {
// Get a output stream handle to the default physical sound device
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
// Load a sound from a file, using a path relative to Cargo.toml
// let filepath = format!("{PUBLIC_PATH}/sound/{filename}.wav");
let filepath = filename;
let file = BufReader::new(File::open(&filepath).unwrap());
// Decode that sound file into a source
let source = Decoder::new(file).unwrap();
// Play the sound directly on the device
println!("Playing {} ...", filepath);
// stream_handle.play_raw(source.convert_samples());
sink.append(source);
if sleep {
// The sound plays in a separate thread. This call will block the current thread until the sink
// has finished playing all its queued sounds.
sink.sleep_until_end();
}
}