From dab6a41b3b17af9cacaac41b0a7d9b24aee0d1ff Mon Sep 17 00:00:00 2001 From: Simon Fondrie-Teitler Date: Sat, 20 Jan 2024 12:43:33 -0500 Subject: [PATCH] If there are multiple probes, only look at data from the first. --- meatnet/src/lib.rs | 2 +- meatweb/src/bluetooth.rs | 48 +++++++++++++++++++--------------------- meatweb/src/history.rs | 4 ++-- meatweb/src/main.rs | 2 +- 4 files changed, 27 insertions(+), 29 deletions(-) diff --git a/meatnet/src/lib.rs b/meatnet/src/lib.rs index 2eca1b7..f0513b6 100644 --- a/meatnet/src/lib.rs +++ b/meatnet/src/lib.rs @@ -203,7 +203,7 @@ pub enum ProductType { MeatNetRepeater, } -#[derive(Debug, PartialEq, DekuRead, DekuWrite, Clone, Serialize, Deserialize)] +#[derive(Debug, PartialEq, DekuRead, DekuWrite, Clone, Copy, Serialize, Deserialize)] #[deku(endian = "little")] pub struct SerialNumber { pub number: u32, diff --git a/meatweb/src/bluetooth.rs b/meatweb/src/bluetooth.rs index 4725dab..6fb826e 100644 --- a/meatweb/src/bluetooth.rs +++ b/meatweb/src/bluetooth.rs @@ -18,7 +18,7 @@ use meatnet::{ response::{ReadLogs, ResponseMessage}, try_request_or_response_from, MessageType, }, - Mode, + Mode, SerialNumber, }; use leptos::{ev, logging, prelude::*}; @@ -28,7 +28,7 @@ pub struct CurrentState { pub core_temperature: Temperature, pub surface_temperature: Temperature, pub ambient_temperature: Temperature, - pub serial_number: u32, + pub serial_number: SerialNumber, pub log_start: u32, pub log_end: u32, pub mode: Mode, @@ -43,7 +43,8 @@ pub enum ConnectionState { pub fn process_bluetooth_event( event: ev::CustomEvent, - set_temperature: WriteSignal, + set_state: WriteSignal, + get_state: ReadSignal, set_history: WriteSignal>, ) { let data = event @@ -65,9 +66,14 @@ pub fn process_bluetooth_event( #[allow(clippy::single_match)] MessageType::Response(r) => match r.message { ResponseMessage::ReadLogs(m) => { - set_history.update(|history| { - history.insert(m.sequence_number, m); - }); + // We only support one probe right now. + if let ConnectionState::Connected(current_state) = get_state.get_untracked() { + if current_state.serial_number == m.probe_serial_number { + set_history.update(|history| { + history.insert(m.sequence_number, m); + }); + } + } } _ => (), }, @@ -75,14 +81,15 @@ pub fn process_bluetooth_event( RequestMessage::HeartbeatMessage(_) => {} RequestMessage::SyncThermometerList(_) => {} RequestMessage::ProbeStatusMessage(m) => { - if m.status.mode == Mode::Normal { - set_temperature.set(ConnectionState::Connected(CurrentState { + // We only support one probe right now. + if m.status.mode == Mode::Normal && m.status.probe_id == 0 { + set_state.set(ConnectionState::Connected(CurrentState { core_temperature: *m.status.get_core_temperature(), surface_temperature: *m.status.get_surface_temperature(), ambient_temperature: *m.status.get_ambient_temperature(), log_start: m.status.log_start, log_end: m.status.log_end, - serial_number: m.probe_serial_number.number, + serial_number: m.probe_serial_number, mode: m.status.mode, })); } @@ -105,18 +112,13 @@ pub fn setup_disconnect_handler(state: WriteSignal) { logging::log!("disconnected"); }) as Box); - bluetooth.set_ongattserverdisconnected(Some( - disconnected_func - .as_ref() - .unchecked_ref(), - )); + bluetooth.set_ongattserverdisconnected(Some(disconnected_func.as_ref().unchecked_ref())); disconnected_func.forget(); } pub async fn get_service( uuid: &Uuid, - set_temperature: WriteSignal, set_state: WriteSignal, ) -> BluetoothRemoteGattService { let _predictive_probe_id = 1; @@ -164,15 +166,11 @@ pub async fn get_service( logging::log!("disconnected"); }) as Box); - device.set_ongattserverdisconnected(Some( - disconnected_func - .as_ref() - .unchecked_ref(), - )); + device.set_ongattserverdisconnected(Some(disconnected_func.as_ref().unchecked_ref())); disconnected_func.forget(); - set_temperature.set(ConnectionState::Connecting); + set_state.set(ConnectionState::Connecting); let gatt = device.gatt().unwrap(); @@ -202,11 +200,11 @@ pub async fn get_characteristics_and_listeners_from_service( service: Uuid, rx_characteristic: Uuid, tx_characteristic: Uuid, - set_temperature: WriteSignal, - set_history: WriteSignal>, set_state: WriteSignal, + get_state: ReadSignal, + set_history: WriteSignal>, ) -> BluetoothRemoteGattCharacteristic { - let service = get_service(&service, set_temperature, set_state).await; + let service = get_service(&service, set_state).await; let rx_characteristic = BluetoothRemoteGattCharacteristic::from( wasm_bindgen_futures::JsFuture::from( @@ -225,7 +223,7 @@ pub async fn get_characteristics_and_listeners_from_service( ); let listener_func = wasm_bindgen::closure::Closure::wrap(Box::new(move |ev| { - process_bluetooth_event(ev, set_temperature, set_history) + process_bluetooth_event(ev, set_state, get_state, set_history) }) as Box); tx_characteristic diff --git a/meatweb/src/history.rs b/meatweb/src/history.rs index eb47a30..79c007a 100644 --- a/meatweb/src/history.rs +++ b/meatweb/src/history.rs @@ -1,7 +1,7 @@ use deku::DekuContainerWrite as _; use gloo::timers::future::TimeoutFuture; use leptos::{Action, ReadSignal, Signal, SignalGetUntracked}; -use meatnet::{uart::node::request, uart::node::response, EncapsulatableMessage as _, SerialNumber}; +use meatnet::{uart::node::request, uart::node::response, EncapsulatableMessage as _,}; use range_set_blaze::RangeSetBlaze; use web_sys::BluetoothRemoteGattCharacteristic; use std::collections::BTreeMap; @@ -46,7 +46,7 @@ pub async fn request_log_updates( .unwrap_or(*range.end()); let mut data = request::ReadLogs { - probe_serial_number: SerialNumber { number: state.serial_number }, + probe_serial_number: state.serial_number, sequence_number_start: start, sequence_number_end: end, } diff --git a/meatweb/src/main.rs b/meatweb/src/main.rs index 5cebefb..d9a62f1 100644 --- a/meatweb/src/main.rs +++ b/meatweb/src/main.rs @@ -42,7 +42,7 @@ fn App() -> impl IntoView { let get_characteristics_and_listeners = create_action(move |_| { async move { - get_characteristics_and_listeners_from_service(NODE_UART_UUID, UART_RX_CHARACTERISTIC_UUID, UART_TX_CHARACTERISTIC_UUID, set_state, set_history, set_state) + get_characteristics_and_listeners_from_service(NODE_UART_UUID, UART_RX_CHARACTERISTIC_UUID, UART_TX_CHARACTERISTIC_UUID, set_state, state, set_history, ) .await } });