Skip to main content

ListEntry

Struct ListEntry 

Source
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: Obis

What 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>

Source

pub const fn number(obis: Obis, unit: Unit, scaler: i8, value: i64) -> Self

A signed numeric entry (Integer on the wire): value × 10^scaler in unit, no status, time or signature. Power is signed; for energy registers see unsigned.

Set the other fields with struct update syntax: ListEntry { status: Some(0x0104), ..ListEntry::number(..) }.

Examples found in repository?
examples/readout.rs (line 18)
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::unsigned(Obis::ENERGY_IMPORT, Unit::WH, -1, 123_456_789),
17        ListEntry::unsigned(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

pub const fn unsigned(obis: Obis, unit: Unit, scaler: i8, value: u64) -> Self

An unsigned numeric entry (Unsigned on the wire), the encoding most meters use for energy registers; otherwise like number.

use sml_kit::{ListEntry, Obis, SmlValue, Unit};

const IMPORT: ListEntry<'static> = ListEntry::unsigned(Obis::ENERGY_IMPORT, Unit::WH, -1, 123_456_789);
assert_eq!(IMPORT.value, SmlValue::Uint(123_456_789));
assert_eq!(IMPORT.quantity().unwrap().to_string(), "12345678.9 Wh");
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::unsigned(Obis::ENERGY_IMPORT, Unit::WH, -1, 123_456_789),
17        ListEntry::unsigned(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

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::unsigned(Obis::ENERGY_IMPORT, Unit::WH, -1, 123_456_789),
17        ListEntry::unsigned(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<'_>

Source

pub fn write(&self, w: &mut Writer<'_>) -> Result<()>

Write the entry: status and value in the smallest width that holds them.

Source

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
Hide additional 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::unsigned(Obis::ENERGY_IMPORT, Unit::WH, -1, 123_456_789),
17        ListEntry::unsigned(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§

Source§

impl<'a> Clone for ListEntry<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for ListEntry<'a>

Source§

impl<'a> Debug for ListEntry<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Element<'a> for ListEntry<'a>

Source§

impl<'a> Eq for ListEntry<'a>

Source§

impl<'a> Hash for ListEntry<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> PartialEq for ListEntry<'a>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for ListEntry<'a>

Auto Trait Implementations§

§

impl<'a> Forget for ListEntry<'a>

§

impl<'a> Freeze for ListEntry<'a>
where &'a [u8]: Freeze,

§

impl<'a> Move for ListEntry<'a>

§

impl<'a> RefUnwindSafe for ListEntry<'a>

§

impl<'a> Send for ListEntry<'a>
where &'a [u8]: Send,

§

impl<'a> Sync for ListEntry<'a>

§

impl<'a> Unpin for ListEntry<'a>
where &'a [u8]: Unpin,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.