pub struct ListEntry<'a> {
pub obis: Obis,
pub status: Option<u64>,
pub val_time: Option<Time>,
pub unit: Option<Unit>,
pub scaler: Option<i8>,
pub value: SmlValue<'a>,
pub value_signature: Option<&'a [u8]>,
}Expand description
SML_ListEntry: one named value.
Fields§
§obis: ObisWhat the value is.
status: Option<u64>Device-specific status bits.
val_time: Option<Time>When the value was captured.
unit: Option<Unit>The unit of a numeric value.
scaler: Option<i8>The decimal exponent of a numeric value.
value: SmlValue<'a>The value.
value_signature: Option<&'a [u8]>A signature over the value, when the device signs. Not verified by this crate.
Implementations§
Source§impl<'a> ListEntry<'a>
impl<'a> ListEntry<'a>
Sourcepub const fn number(obis: Obis, unit: Unit, scaler: i8, value: i64) -> Self
pub const fn number(obis: Obis, unit: Unit, scaler: i8, value: i64) -> Self
A numeric entry: value × 10^scaler in unit, no status, time or signature.
Examples found in repository?
examples/readout.rs (line 16)
11fn main() -> Result<(), sml_kit::Error> {
12 // What a basic meter pushes on its optical interface, roughly every second.
13 let entries = [
14 ListEntry::bytes(Obis::MANUFACTURER, b"SIM"),
15 ListEntry::bytes(Obis::DEVICE_ID, &[0x0A, 0x01, 0x53, 0x49, 0x4D, 0, 0, 0, 0, 1]),
16 ListEntry::number(Obis::ENERGY_IMPORT, Unit::WH, -1, 123_456_789),
17 ListEntry::number(Obis::ENERGY_EXPORT, Unit::WH, -1, 4_200),
18 ListEntry::number(Obis::POWER, Unit::W, 0, 1_337),
19 ];
20 let telegram = Telegram {
21 server_id: &[0x0A, 0x01, 0x53, 0x49, 0x4D, 0, 0, 0, 0, 1],
22 file_id: b"F1",
23 entries: &entries,
24 };
25
26 let mut payload = [0u8; 512];
27 let len = telegram.write(&mut payload)?;
28 let mut wire = [0u8; 600];
29 let wire_len = encode_frame(&payload[..len], &mut wire)?;
30 println!("{wire_len} bytes on the wire");
31
32 // The receiving side: bytes in, verified frames out, readings in one call.
33 let mut decoder = Decoder::new([0u8; 1024]);
34 let mut result = Ok(());
35 decoder.push_slice(&wire[..wire_len], |frame| {
36 let list = match frame.and_then(readout) {
37 Ok(list) => list,
38 Err(e) => return result = Err(e),
39 };
40 println!("meter {}", list.server_id);
41 for entry in list.entries {
42 match entry {
43 Ok(e) => match e.quantity() {
44 Some(q) => println!("{:<24} {q}", e.obis.to_string()),
45 None => println!("{:<24} {:02x?}", e.obis.to_string(), e.value),
46 },
47 Err(err) => result = Err(err),
48 }
49 }
50 });
51 result
52}Sourcepub const fn bytes(obis: Obis, value: &'a [u8]) -> Self
pub const fn bytes(obis: Obis, value: &'a [u8]) -> Self
A byte-string entry (identifiers, text).
Examples found in repository?
examples/readout.rs (line 14)
11fn main() -> Result<(), sml_kit::Error> {
12 // What a basic meter pushes on its optical interface, roughly every second.
13 let entries = [
14 ListEntry::bytes(Obis::MANUFACTURER, b"SIM"),
15 ListEntry::bytes(Obis::DEVICE_ID, &[0x0A, 0x01, 0x53, 0x49, 0x4D, 0, 0, 0, 0, 1]),
16 ListEntry::number(Obis::ENERGY_IMPORT, Unit::WH, -1, 123_456_789),
17 ListEntry::number(Obis::ENERGY_EXPORT, Unit::WH, -1, 4_200),
18 ListEntry::number(Obis::POWER, Unit::W, 0, 1_337),
19 ];
20 let telegram = Telegram {
21 server_id: &[0x0A, 0x01, 0x53, 0x49, 0x4D, 0, 0, 0, 0, 1],
22 file_id: b"F1",
23 entries: &entries,
24 };
25
26 let mut payload = [0u8; 512];
27 let len = telegram.write(&mut payload)?;
28 let mut wire = [0u8; 600];
29 let wire_len = encode_frame(&payload[..len], &mut wire)?;
30 println!("{wire_len} bytes on the wire");
31
32 // The receiving side: bytes in, verified frames out, readings in one call.
33 let mut decoder = Decoder::new([0u8; 1024]);
34 let mut result = Ok(());
35 decoder.push_slice(&wire[..wire_len], |frame| {
36 let list = match frame.and_then(readout) {
37 Ok(list) => list,
38 Err(e) => return result = Err(e),
39 };
40 println!("meter {}", list.server_id);
41 for entry in list.entries {
42 match entry {
43 Ok(e) => match e.quantity() {
44 Some(q) => println!("{:<24} {q}", e.obis.to_string()),
45 None => println!("{:<24} {:02x?}", e.obis.to_string(), e.value),
46 },
47 Err(err) => result = Err(err),
48 }
49 }
50 });
51 result
52}Source§impl ListEntry<'_>
impl ListEntry<'_>
Sourcepub fn write(&self, w: &mut Writer<'_>) -> Result<()>
pub fn write(&self, w: &mut Writer<'_>) -> Result<()>
Write the entry: status and value in the smallest width that holds them.
Sourcepub fn quantity(&self) -> Option<Quantity>
pub fn quantity(&self) -> Option<Quantity>
The value as an exact Quantity, when it is numeric.
Examples found in repository?
examples/decode.rs (line 261)
260fn line(e: &ListEntry<'_>) -> String {
261 let value = match (e.quantity(), e.value) {
262 (Some(q), _) => q.to_string(),
263 (None, SmlValue::Bytes(b)) if [Obis::DEVICE_ID, Obis::SERIAL].contains(&e.obis) => {
264 ServerId::new(b).to_string()
265 }
266 // Printable ASCII is shown quoted and escaped; nothing a meter sends reaches the
267 // terminal as a control character.
268 (None, SmlValue::Bytes(b))
269 if !b.is_empty() && b.iter().all(|c| c.is_ascii_graphic() || *c == b' ') =>
270 {
271 format!("\"{}\"", b.escape_ascii())
272 }
273 (None, SmlValue::Bytes(b)) => b.iter().map(|c| format!("{c:02x}")).collect(),
274 (None, SmlValue::Time(t)) => t.to_string(),
275 (None, other) => format!("{other:?}"),
276 };
277 let name = e.obis.name().unwrap_or("");
278 format!("{:<24} {:<36} {value}", e.obis.to_string(), name)
279}
280
281/// Print a parameter tree, one node per line, indented by depth.
282fn tree(t: &sml_kit::Tree<'_>, depth: usize) {
283 let name = Obis::from_bytes(t.name).map_or_else(
284 |_| hex(t.name),
285 |o| format!("{o}{}", o.name().map(|n| format!(" ({n})")).unwrap_or_default()),
286 );
287 let value = match t.value {
288 None => String::new(),
289 Some(sml_kit::ProcParValue::Value(SmlValue::Bytes(b))) => format!("= {}", text(b)),
290 Some(sml_kit::ProcParValue::Value(v)) => format!("= {v:?}"),
291 Some(sml_kit::ProcParValue::Entry(e)) => format!(
292 "= {}{}",
293 e.quantity().map_or_else(|| format!("{:?}", e.value), |q| q.to_string()),
294 if e.value_signature.is_some() { " (signed, not verified)" } else { "" }
295 ),
296 Some(sml_kit::ProcParValue::Period(p)) => {
297 format!("= {}", p.quantity().map_or_else(|| format!("{:?}", p.value), |q| q.to_string()))
298 }
299 Some(sml_kit::ProcParValue::Time(t)) => format!("= {t}"),
300 Some(sml_kit::ProcParValue::Tupel(t)) => {
301 format!("= A+ {} A- {}", t.a_plus.quantity(), t.a_minus.quantity())
302 }
303 };
304 println!("{:indent$}{name} {value}", "", indent = 2 * depth + 1);
305 for child in t.children.into_iter().flatten() {
306 match child {
307 Ok(c) => tree(&c, depth + 1),
308 Err(e) => eprintln!("{:indent$}child error: {e}", "", indent = 2 * depth + 3),
309 }
310 }
311}More examples
examples/readout.rs (line 43)
11fn main() -> Result<(), sml_kit::Error> {
12 // What a basic meter pushes on its optical interface, roughly every second.
13 let entries = [
14 ListEntry::bytes(Obis::MANUFACTURER, b"SIM"),
15 ListEntry::bytes(Obis::DEVICE_ID, &[0x0A, 0x01, 0x53, 0x49, 0x4D, 0, 0, 0, 0, 1]),
16 ListEntry::number(Obis::ENERGY_IMPORT, Unit::WH, -1, 123_456_789),
17 ListEntry::number(Obis::ENERGY_EXPORT, Unit::WH, -1, 4_200),
18 ListEntry::number(Obis::POWER, Unit::W, 0, 1_337),
19 ];
20 let telegram = Telegram {
21 server_id: &[0x0A, 0x01, 0x53, 0x49, 0x4D, 0, 0, 0, 0, 1],
22 file_id: b"F1",
23 entries: &entries,
24 };
25
26 let mut payload = [0u8; 512];
27 let len = telegram.write(&mut payload)?;
28 let mut wire = [0u8; 600];
29 let wire_len = encode_frame(&payload[..len], &mut wire)?;
30 println!("{wire_len} bytes on the wire");
31
32 // The receiving side: bytes in, verified frames out, readings in one call.
33 let mut decoder = Decoder::new([0u8; 1024]);
34 let mut result = Ok(());
35 decoder.push_slice(&wire[..wire_len], |frame| {
36 let list = match frame.and_then(readout) {
37 Ok(list) => list,
38 Err(e) => return result = Err(e),
39 };
40 println!("meter {}", list.server_id);
41 for entry in list.entries {
42 match entry {
43 Ok(e) => match e.quantity() {
44 Some(q) => println!("{:<24} {q}", e.obis.to_string()),
45 None => println!("{:<24} {:02x?}", e.obis.to_string(), e.value),
46 },
47 Err(err) => result = Err(err),
48 }
49 }
50 });
51 result
52}Trait Implementations§
impl<'a> Copy for ListEntry<'a>
impl<'a> Element<'a> for ListEntry<'a>
impl<'a> Eq for ListEntry<'a>
impl<'a> StructuralPartialEq for ListEntry<'a>
Auto Trait Implementations§
impl<'a> Forget for ListEntry<'a>
impl<'a> Freeze for ListEntry<'a>
impl<'a> Move for ListEntry<'a>
impl<'a> RefUnwindSafe for ListEntry<'a>
impl<'a> Send for ListEntry<'a>
impl<'a> Sync for ListEntry<'a>
impl<'a> Unpin for ListEntry<'a>
impl<'a> UnsafeUnpin for ListEntry<'a>where
&'a [u8]: UnsafeUnpin,
impl<'a> UnwindSafe for ListEntry<'a>where
&'a [u8]: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more