From e766b560c769ecbfc3bbdbd9d58ad09c0a46f68c Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Mon, 18 Nov 2024 16:45:46 +0100 Subject: [PATCH] Streaming data works! --- src-tauri/src/main.rs | 36 +++++++++++++++++++++--------------- src/App.vue | 2 ++ src/components/Graph.vue | 15 +++++++++++---- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1f69f4d..5abae96 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -52,12 +52,8 @@ fn random_data() -> Vec { data } -#[tauri::command] -fn create_data_listener(websockets_uri: String, app: tauri::AppHandle) { +fn create_data_listener(websockets_uri: String, data: Arc>>) { tokio::spawn(async move { - // Variable to hold the ECG data - let mut ecg_data: Vec = Vec::with_capacity(2 ^ 16); - // Create the websockts connection let mut ws = match websockets::WebSocket::connect(websockets_uri.as_str()).await { Ok(k) => k, @@ -83,12 +79,15 @@ fn create_data_listener(websockets_uri: String, app: tauri::AppHandle) { debug!("Got ECG voltage: {}", voltage); - // Filter out so we discard ol data - ecg_data = ecg_data + let mut ecg_data = data.lock().expect("Failed to get lock"); + + // Filter out so we discard old data + *ecg_data = ecg_data + .clone() .into_iter() .filter(|data| { Utc::now().signed_duration_since(data.timestamp) - <= Duration::seconds(10) + <= Duration::seconds(5) }) .collect(); @@ -97,8 +96,7 @@ fn create_data_listener(websockets_uri: String, app: tauri::AppHandle) { timestamp: Utc::now(), }); - app.emit("ecg-data", ecg_data.clone()) - .expect("Failed to emit app event"); + drop(ecg_data); } } Err(e) => { @@ -109,17 +107,25 @@ fn create_data_listener(websockets_uri: String, app: tauri::AppHandle) { }); } +#[tauri::command] +async fn get_ecg_data(data: State<'_, Arc>>>) -> Result, Error> { + Ok(data.lock().expect("Failed to get lock").clone()) +} + #[tokio::main] async fn main() { ::env_logger::init(); + // Variable to hold the ECG data + let ecg_data: Arc>> = Arc::new(Mutex::new(Vec::with_capacity(2 ^ 13))); + + // Create the websockets listener + create_data_listener("ws://192.168.128.50:81".to_string(), ecg_data.clone()); + tauri::Builder::default() .plugin(tauri_plugin_shell::init()) - .invoke_handler(tauri::generate_handler![ - greet, - random_data, - create_data_listener - ]) + .invoke_handler(tauri::generate_handler![greet, random_data, get_ecg_data]) + .manage(ecg_data) .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/src/App.vue b/src/App.vue index c59ce7b..6340cea 100644 --- a/src/App.vue +++ b/src/App.vue @@ -3,6 +3,7 @@ // Check out https://vuejs.org/api/sfc-script-setup.html#script-setup import Greet from "./components/Greet.vue"; import Graph from "./components/Graph.vue"; +/* import { invoke } from "@tauri-apps/api/core"; import { onMounted } from "vue"; @@ -11,6 +12,7 @@ onMounted(async () => { websocketsUri: "ws://192.168.128.50:81", }); }); +*/