Skip to content

Commit

Permalink
Update monitor tool to use metadata to plan screen layout
Browse files Browse the repository at this point in the history
  • Loading branch information
tkornack authored Dec 18, 2024
2 parents 7377e4b + c4a311c commit f9c2177
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 72 deletions.
48 changes: 24 additions & 24 deletions Cargo.lock

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

51 changes: 33 additions & 18 deletions twinleaf-tools/src/bin/tio-monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use twinleaf::{

use getopts::Options;
use std::{env, io::stdout, time::Duration};
use std::collections::HashMap;

use futures::{future::FutureExt, select, StreamExt};
use futures_timer::Delay;
Expand Down Expand Up @@ -57,39 +58,41 @@ fn tio_parseopts(opts: Options, args: &[String]) -> (getopts::Matches, String, D

async fn run_monitor() {
let mut reader = EventStream::new();
let mut stdout = stdout();

let args: Vec<String> = env::args().collect();

let opts = tio_opts();
let (_matches, root, route) = tio_parseopts(opts, &args);

let proxy = proxy::Interface::new(&root);
let device = proxy.device_full(route).unwrap();
let mut device = Device::new(device);

let mut stdout = stdout();

let mut device = Device::new(device);
let meta = device.get_metadata();
let mut positions: HashMap<u8, usize> = HashMap::new();
for (_id, stream) in &meta.streams {
positions.insert(stream.stream.stream_id, stream.stream.n_columns);
}

'drawing: loop {
let mut delay = Delay::new(Duration::from_nanos(1)).fuse();
let mut event = reader.next().fuse();

let sample = device.next();

//write in title
let name = format!(
"Device Name: {} Serial: {} Session ID: {}",
sample.device.name, sample.device.serial_number, sample.device.session_id
);

//write in device info
let name = format!("Device Name: {} Serial: {} Session ID: {}", sample.device.name, sample.device.serial_number, sample.device.session_id);
_ = stdout.execute(MoveToRow(0));
println!("\r\n{}", name);
_ = stdout.execute(MoveToNextLine(1));
println!("\r{:?}", name);

select! {
_= delay => {
for col in &sample.columns{
let width = sample.columns.iter().map(|col| col.desc.name.len().clone()).max().unwrap();
let unit_width = sample.columns.iter().map(|col| col.desc.units.len().clone()).max().unwrap();
let string = format!(
" {}: {}",
" {:<width$}({:<unit_width$}) {}",
col.desc.name,
col.desc.units,
match col.value {
ColumnData::Int(x) => format!("{}", x),
ColumnData::UInt(x) => format!("{:.3}", x),
Expand All @@ -98,12 +101,24 @@ async fn run_monitor() {
}
);

if sample.stream.stream_id == 2 && col.desc.name == sample.columns[0].desc.name.clone() {
_ = stdout.execute(MoveToNextLine(1));
if col.desc.name == sample.columns[0].desc.name.clone(){
match positions.get(&sample.stream.stream_id) {
Some(_row) => {
let mut row_position = 0;
for pos in positions.keys() {
if &sample.stream.stream_id > pos {
_ = stdout.execute(MoveToNextLine(1));
row_position += positions[pos];
}
}
_ = stdout.execute(MoveDown((row_position + 1).try_into().unwrap()));
},
None => println!("\rError, stream not found")
};
}

_ = stdout.execute(Clear(ClearType::CurrentLine));
println!("{}\r", string);

println!("\r{}", string);
}
},
some_event = event => {
Expand Down
24 changes: 24 additions & 0 deletions twinleaf-tools/src/bin/tio-tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,26 @@ fn dump(args: &[String]) {
}
}

fn meta_dump(args: &[String]) {
use twinleaf::data::Device;
let opts = tio_opts();
let (_matches, root, route) = tio_parseopts(opts, args);

let proxy = proxy::Interface::new(&root);
let device = proxy.device_full(route).unwrap();
let mut device = Device::new(device);

let meta = device.get_metadata();
println!("{:?}", meta.device);
for (_id, stream) in meta.streams {
println!("{:?}", stream.stream);
println!("{:?}", stream.segment);
for col in stream.columns {
println!("{:?}", col);
}
}
}

fn print_sample(sample: &twinleaf::data::Sample) {
use twinleaf::data::ColumnData;
if sample.meta_changed {
Expand Down Expand Up @@ -597,6 +617,9 @@ fn main() {
"data-dump" => {
data_dump(&args[2..]); //.unwrap();
}
"meta-dump" => {
meta_dump(&args[2..]); //.unwrap();
}
_ => {
// TODO: do usage right
println!("Usage:");
Expand All @@ -611,6 +634,7 @@ fn main() {
println!(" tio-tool rpc-dump [-r url] [-s sensor] <rpc-name>");
println!(" tio-tool firmware-upgrade [-r url] [-s sensor] <firmware_image.bin>");
println!(" tio-tool data-dump [-r url] [-s sensor]");
println!(" tio-tool meta-dump [-r url] [-s sensor]");
}
}
}
Loading

0 comments on commit f9c2177

Please sign in to comment.