-
Notifications
You must be signed in to change notification settings - Fork 3
/
tagtable.rs
95 lines (87 loc) · 2.81 KB
/
tagtable.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::rfid::ScanResult;
use crate::block;
use tui::layout::Rect;
use tui::buffer::Buffer;
use tui::widgets::{Widget, Table, Row};
use tui::style::{Color, Style, Modifier};
use gs1::{epc, epc::tid::mdid_name, epc::tid::tmid_name};
fn render_row(item: &ScanResult) -> Vec<String> {
let mut epc_str = match epc::decode_binary(&item.epc) {
Ok(val) => val.to_uri(),
Err(_) => hex::encode_upper(&item.epc)
};
if epc_str == "urn:epc:id:unprogrammed" {
epc_str = hex::encode_upper(&item.epc);
}
vec![
epc_str,
match item.tid {
Some(tid) => mdid_name(&tid.mdid).to_string(),
None => "".to_string()
},
match item.tid {
Some(tid) => match tmid_name(&tid.mdid, &tid.tmid) {
"Unknown" => format!("0x{:X}", &tid.tmid),
found => found.to_string()
},
None => "".to_string()
},
match item.xtid_header {
Some(_) => "Y",
None => ""
}.to_string(),
match item.serial {
Some(_) => "Y",
None => ""
}.to_string(),
match item.rssi {
Some(val) => format!("{}", val),
None => "".to_string()
},
match item.antenna {
Some(val) => format!("{}", val),
None => "".to_string()
},
format!("{}s", item.last_seen.elapsed().as_secs())
]
}
pub(crate) struct TagTable<'a> {
pub items: &'a Vec<&'a ScanResult>,
pub selected: Option<Vec<u8>>
}
impl<'a> TagTable<'a> {
pub fn new(items: &'a Vec<&'a ScanResult>, selected: Option<Vec<u8>>) -> TagTable<'a> {
TagTable {
items: items,
selected: selected
}
}
}
impl<'a> Widget for TagTable<'a> {
fn draw(&mut self, area: Rect, buf: &mut Buffer) {
let header = ["ID", "Manufacturer", "Model", "XTID", "Serial", "RSSI", "Ant", "Age"];
let selected_style = Style::default().fg(Color::Yellow);
let normal_style = Style::default();
let rows = self.items.iter().map(|item| {
let mut style = normal_style;
if item.last_seen.elapsed().as_secs() > 2 {
style = style.fg(Color::Gray);
}
match &self.selected {
Some(selected) => {
if &item.epc == selected {
style = selected_style;
}
}
None => {}
};
let cols = render_row(item).into_iter();
Row::StyledData(cols, style)
});
Table::new(header.into_iter(), rows)
.header_style(Style::default().modifier(Modifier::BOLD))
.block(block("Tags"))
.widths(&[50, 25, 10, 6, 6, 8, 6, 9])
.draw(area, buf);
}
}