websockets based IPC added in order to bind GUI/app together

This commit is contained in:
Priler 2026-01-07 00:15:36 +05:00
parent 88ecf21b2c
commit 9b310fd831
15 changed files with 1069 additions and 47 deletions

View file

@ -1,8 +1,10 @@
use std::time::SystemTime;
use jarvis_core::{audio, audio_processing, commands, config, listener, recorder, stt, COMMANDS_LIST, intent};
use jarvis_core::{audio, audio_processing, commands, config, listener, recorder, stt, COMMANDS_LIST, intent, ipc::{self, IpcEvent}};
use rand::prelude::*;
use crate::should_stop;
pub fn start() -> Result<(), ()> {
// start the loop
main_loop()
@ -29,8 +31,18 @@ fn main_loop() -> Result<(), ()> {
}
}
// notify GUI we're ready
ipc::send(IpcEvent::Idle);
// the loop
'wake_word: loop {
// check for stop signal
if should_stop() {
info!("Stop signal received, shutting down...");
ipc::send(IpcEvent::Stopping);
break;
}
// read from microphone
recorder::read_microphone(&mut frame_buffer);
@ -45,6 +57,9 @@ fn main_loop() -> Result<(), ()> {
// recognize wake-word
match listener::data_callback(&frame_buffer) {
Some(_keyword_index) => {
// notify GUI
ipc::send(IpcEvent::WakeWordDetected);
// reset some things
stt::reset_wake_recognizer();
stt::reset_speech_recognizer();
@ -64,8 +79,16 @@ fn main_loop() -> Result<(), ()> {
.unwrap()
)));
// notify GUI we're listening
ipc::send(IpcEvent::Listening);
// wait for voice commands
'voice_recognition: loop {
// check for stop
if should_stop() {
break 'wake_word;
}
// read from microphone
recorder::read_microphone(&mut frame_buffer);
@ -88,6 +111,11 @@ fn main_loop() -> Result<(), ()> {
// something was recognized
info!("Recognized voice: {}", recognized_voice);
// notify GUI
ipc::send(IpcEvent::SpeechRecognized {
text: recognized_voice.clone(),
});
// filter recognized voice
// @TODO. Better recognized voice filtration.
recognized_voice = recognized_voice.to_lowercase();
@ -108,6 +136,8 @@ fn main_loop() -> Result<(), ()> {
start = SystemTime::now();
silence_frames = 0;
stt::reset_speech_recognizer();
ipc::send(IpcEvent::Listening);
continue 'voice_recognition;
}
@ -143,6 +173,12 @@ fn main_loop() -> Result<(), ()> {
// success
info!("Command executed successfully.");
// notify GUI
ipc::send(IpcEvent::CommandExecuted {
id: cmd_config.id.clone(),
success: true,
});
if chain {
// chain commands
start = SystemTime::now();
@ -158,6 +194,14 @@ fn main_loop() -> Result<(), ()> {
Err(msg) => {
// fail
error!("Error executing command: {}", msg);
ipc::send(IpcEvent::CommandExecuted {
id: cmd_config.id.clone(),
success: false,
});
ipc::send(IpcEvent::Error {
message: msg.to_string(),
});
}
}
}
@ -178,12 +222,17 @@ fn main_loop() -> Result<(), ()> {
// reset things
stt::reset_wake_recognizer();
audio_processing::reset();
ipc::send(IpcEvent::Idle);
}
}
None => (),
}
}
// cleanup
recorder::stop_recording().ok();
ipc::send(IpcEvent::Stopping);
Ok(())
}
@ -191,5 +240,6 @@ fn keyword_callback(keyword_index: i32) {}
pub fn close(code: i32) {
info!("Closing application.");
ipc::send(IpcEvent::Stopping);
std::process::exit(code);
}

View file

@ -1,9 +1,11 @@
use parking_lot::RwLock;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
// include core
use jarvis_core::{
audio, audio_processing, commands, config, db, listener, recorder, stt, intent,
ipc::{self, IpcAction},
APP_CONFIG_DIR, APP_LOG_DIR, COMMANDS_LIST, DB,
};
@ -20,6 +22,8 @@ mod app;
#[cfg(not(target_os = "macos"))]
mod tray;
static SHOULD_STOP: AtomicBool = AtomicBool::new(false);
fn main() -> Result<(), String> {
// initialize directories
config::init_dirs()?;
@ -96,6 +100,34 @@ fn main() -> Result<(), String> {
warn!("Audio processing init failed: {}", e);
}
// init IPC
info!("Initializing IPC...");
ipc::init();
ipc::set_action_handler(|action| {
match action {
IpcAction::Stop => {
info!("Received stop command from GUI");
SHOULD_STOP.store(true, Ordering::SeqCst);
}
IpcAction::ReloadCommands => {
info!("Received reload commands request");
// TODO: implement reload
}
IpcAction::SetMuted { muted } => {
info!("Received mute request: {}", muted);
// TODO: implement mute
}
_ => {}
}
});
// start WebSocket server for ipc
std::thread::spawn(|| {
let rt = tokio::runtime::Runtime::new().expect("Failed to create tokio runtime for IPC");
rt.block_on(ipc::start_server());
});
// start the app (in the background thread)
std::thread::spawn(|| {
let _ = app::start();
@ -104,4 +136,8 @@ fn main() -> Result<(), String> {
tray::init_blocking();
Ok(())
}
pub fn should_stop() -> bool {
SHOULD_STOP.load(Ordering::SeqCst)
}