Skip to main content

Telegram

Struct Telegram 

Source
pub struct Telegram<'a> {
    pub server_id: &'a [u8],
    pub file_id: &'a [u8],
    pub entries: &'a [ListEntry<'a>],
}
Expand description

A simulated meter telegram.

Fields§

§server_id: &'a [u8]

The meter’s server ID (10 bytes on German meters).

§file_id: &'a [u8]

The file ID shared by the three messages.

§entries: &'a [ListEntry<'a>]

The entries of the GetListResponse.

Implementations§

Source§

impl Telegram<'_>

Source

pub fn write(&self, out: &mut [u8]) -> Result<usize>

Write the three messages into out. Returns the number of bytes written: the unframed payload.

Examples found in repository?
examples/readout.rs (line 27)
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§

Source§

impl<'a> Clone for Telegram<'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 Telegram<'a>

Source§

impl<'a> Debug for Telegram<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Forget for Telegram<'a>

§

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

§

impl<'a> Move for Telegram<'a>

§

impl<'a> RefUnwindSafe for Telegram<'a>

§

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

§

impl<'a> Sync for Telegram<'a>

§

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

§

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

§

impl<'a> UnwindSafe for Telegram<'a>
where &'a [u8]: UnwindSafe, &'a [ListEntry<'a>]: 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.