Skip to content

Commit

Permalink
If there are multiple probes, only look at data from the first.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonft committed Jan 20, 2024
1 parent 274edf9 commit dab6a41
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion meatnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 23 additions & 25 deletions meatweb/src/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use meatnet::{
response::{ReadLogs, ResponseMessage},
try_request_or_response_from, MessageType,
},
Mode,
Mode, SerialNumber,
};

use leptos::{ev, logging, prelude::*};
Expand All @@ -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,
Expand All @@ -43,7 +43,8 @@ pub enum ConnectionState {

pub fn process_bluetooth_event(
event: ev::CustomEvent,
set_temperature: WriteSignal<ConnectionState>,
set_state: WriteSignal<ConnectionState>,
get_state: ReadSignal<ConnectionState>,
set_history: WriteSignal<BTreeMap<u32, ReadLogs>>,
) {
let data = event
Expand All @@ -65,24 +66,30 @@ 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);
});
}
}
}
_ => (),
},
MessageType::Request(r) => match r.message {
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,
}));
}
Expand All @@ -105,18 +112,13 @@ pub fn setup_disconnect_handler(state: WriteSignal<ConnectionState>) {
logging::log!("disconnected");
}) as Box<dyn FnMut(Event)>);

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<ConnectionState>,
set_state: WriteSignal<ConnectionState>,
) -> BluetoothRemoteGattService {
let _predictive_probe_id = 1;
Expand Down Expand Up @@ -164,15 +166,11 @@ pub async fn get_service(
logging::log!("disconnected");
}) as Box<dyn FnMut(Event)>);

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();

Expand Down Expand Up @@ -202,11 +200,11 @@ pub async fn get_characteristics_and_listeners_from_service(
service: Uuid,
rx_characteristic: Uuid,
tx_characteristic: Uuid,
set_temperature: WriteSignal<ConnectionState>,
set_history: WriteSignal<BTreeMap<u32, ReadLogs>>,
set_state: WriteSignal<ConnectionState>,
get_state: ReadSignal<ConnectionState>,
set_history: WriteSignal<BTreeMap<u32, ReadLogs>>,
) -> 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(
Expand All @@ -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<dyn FnMut(_)>);

tx_characteristic
Expand Down
4 changes: 2 additions & 2 deletions meatweb/src/history.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion meatweb/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});
Expand Down

0 comments on commit dab6a41

Please sign in to comment.