Skip to content

Commit

Permalink
Add getting history and graphing
Browse files Browse the repository at this point in the history
  • Loading branch information
simonft committed Jan 15, 2024
1 parent 1a000dd commit 1188dca
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 77 deletions.
146 changes: 146 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions meatnet/src/uart/node/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ struct SetProbeId {}

#[derive(Debug, PartialEq, DekuRead)]
pub struct ReadSessionInformation {
probe_serial_number: SerialNumber,
probe_session_id: u32,
probe_sample_period: u16,
pub probe_serial_number: SerialNumber,
pub probe_session_id: u32,
pub probe_sample_period: u16,
}

#[derive(Debug, PartialEq, DekuRead)]
Expand Down
2 changes: 2 additions & 0 deletions meatweb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ thaw = "0.1.7"
range-set-blaze = "0.1.14"
itertools = "0.12.0"
gloo = { version = "0.11.0", features = ["timers"]}
charming = { version = "0.3.1", features = ["wasm"] }
chrono = "0.4.31"

[dependencies.web-sys]
version = "0.3.66"
Expand Down
2 changes: 2 additions & 0 deletions meatweb/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

<head>
<link rel="stylesheet" href="/main.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts-gl.min.js"></script>
</head>

<body></body>
Expand Down
10 changes: 4 additions & 6 deletions meatweb/src/bluetooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ use web_sys::{

const NODE_UART_UUID: Uuid = uuid::uuid!("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");

const UART_RX_CHARACTERISTIC_UUID: Uuid = uuid::uuid!("6E400002-B5A3-F393-E0A9-E50E24DCCA9E");
const UART_TX_CHARACTERISTIC_UUID: Uuid = uuid::uuid!("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");

use meatnet::{
temperature::IsTemperature,
uart::node::{
Expand Down Expand Up @@ -94,7 +91,10 @@ pub fn process_bluetooth_event(
_ => logging::log!("{:#?}", r),
},
},
Err(e) => logging::log!("error: {}", e),
Err(e) => {
logging::log!("{:02x?}", vec_data);
logging::log!("error: {}", e);
}
}
}

Expand Down Expand Up @@ -215,8 +215,6 @@ pub async fn get_characteristics_and_listeners_from_service(
.await
.unwrap();

logging::log!("{:#?}", "finished connecting");

listener_func.forget();

CharacteristicsAndListenerResult {
Expand Down
69 changes: 69 additions & 0 deletions meatweb/src/chart.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::collections::BTreeMap;

use charming::{
component::Axis, datatype::DataPointItem, element::AxisType, series::Line, Chart, Echarts,
WasmRenderer,
};
use chrono::{Duration, Local, Timelike as _};
use itertools::Itertools;
use leptos::{ReadSignal, SignalSet as _, SignalWith as _, WriteSignal};

use crate::history::LogItem;

pub fn chart_handler(
history: BTreeMap<u32, LogItem>,
set_chart: WriteSignal<Option<Echarts>>,
get_chart: ReadSignal<Option<Echarts>>,
) {
let max_value = match history.last_key_value() {
Some((k, _)) => *k,
None => 0,
};

let elapsed = Duration::seconds(max_value as i64 * 5);

let mut start_time = Local::now() - elapsed;
let extra_seconds = start_time.second() % 5;
start_time -= Duration::seconds(extra_seconds as i64);

let data = (0..max_value).map(|i| {
let key = history.get(&i);
(
(start_time + Duration::seconds(i as i64 * 5))
.format("%H:%M:%S")
.to_string(),
match key {
Some(key) => key.temperature.get_celsius() as f64,
None => f64::NAN,
},
)
});

let chart = Chart::new()
.x_axis(
Axis::new()
.type_(AxisType::Category)
.data(data.clone().map(|(k, _)| k).collect_vec()),
)
.y_axis(Axis::new().type_(AxisType::Value))
.series(Line::new().data(data.map(|(k, v)| DataPointItem::from((v, k))).collect_vec()));

let mut updated = false;
get_chart.with(|optional_echarts| match optional_echarts {
Some(echarts) => {
WasmRenderer::update(echarts, &chart);
updated = true
}
None => (),
});

if !updated {
set_chart.set(match WasmRenderer::new(800, 600).render("chart", &chart) {
Ok(value) => Some(value),
Err(error) => {
println!("Error rendering chart: {:?}", error);
None
}
})
}
}
Loading

0 comments on commit 1188dca

Please sign in to comment.