Skip to main content

sml_kit/
status.rs

1//! The energy direction a meter reports in the status word of its energy registers.
2//!
3//! Many meters send the instantaneous power (`1-0:16.7.0`) without a sign and put the
4//! direction, import (+A) or export (−A), in a status bit of `1.8.0`. Two layouts are in
5//! the field, and the value alone does not tell them apart, so the caller names the one
6//! the meter uses:
7//!
8//! | Layout | Bit | Specified by | Seen on |
9//! |---|---|---|---|
10//! | [`StatusWord::Edl`] | 5 (`0x20`) | FNN Lastenheft EDL 1.2, Tab. 7 | EMH ED300L, EMH eHZ, ISKRA MT175 |
11//! | [`StatusWord::Mme`] | 11 (`0x800`) | manufacturer manuals (4-byte status) | DZG DVS74/DWSB20, Holley DTZ541 (corpus), Elster AS2020 |
12//!
13//! ```
14//! use sml_kit::{Direction, GetListResponse, ListEntries, ListEntry, Obis, ServerId, StatusWord, Unit};
15//!
16//! // An ED300L feeding in: power without a sign, bit 5 set on 1.8.0.
17//! let entries = [
18//!     ListEntry { status: Some(0x1b0), ..ListEntry::number(Obis::ENERGY_IMPORT, Unit::WH, -1, 12_345) },
19//!     ListEntry::number(Obis::POWER, Unit::W, 0, 230),
20//! ];
21//! let list = GetListResponse {
22//!     client_id: None,
23//!     server_id: ServerId::new(b"\x09\x01EMH\x00\x00\x41\xf0\x45"),
24//!     list_name: None,
25//!     act_sensor_time: None,
26//!     entries: ListEntries::from_slice(&entries),
27//!     list_signature: None,
28//!     act_gateway_time: None,
29//! };
30//! assert_eq!(list.direction(StatusWord::Edl), Some(Direction::Export));
31//! assert_eq!(list.signed_power(StatusWord::Edl).unwrap().to_string(), "-230 W");
32//! ```
33
34use core::fmt;
35
36use crate::{GetListResponse, Obis, Quantity};
37
38/// Where a meter's status word carries the energy direction.
39#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
40#[non_exhaustive]
41pub enum StatusWord {
42    /// Bit 5 (`0x20`): "Energierichtung, 0 = +A, 1 = −A" (FNN Lastenheft EDL 1.2, Tab. 7).
43    /// EDL21 meters such as the EMH ED300L and eHZ and the ISKRA MT175.
44    Edl,
45    /// Bit 11 (`0x800`) of the 4-byte status of newer meters (bits 12–14 are the phases).
46    /// DZG DVS74 and DWSB20, Holley DTZ541, Elster AS2020.
47    Mme,
48}
49
50impl StatusWord {
51    const fn mask(self) -> u64 {
52        match self {
53            Self::Edl => 1 << 5,
54            Self::Mme => 1 << 11,
55        }
56    }
57}
58
59/// The direction of energy flow, from the meter's point of view.
60#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
61pub enum Direction {
62    /// Drawn from the grid (+A).
63    Import,
64    /// Fed into the grid (−A).
65    Export,
66}
67
68impl fmt::Display for Direction {
69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70        f.write_str(match self {
71            Self::Import => "import (+A)",
72            Self::Export => "export (−A)",
73        })
74    }
75}
76
77impl GetListResponse<'_> {
78    /// The direction the status word of `1.8.0` reports (of `2.8.0` when `1.8.0` has
79    /// none), read in `layout`. `None` when neither register carries a status.
80    ///
81    /// The meter samples the bit with its registers and reports import at standstill.
82    pub fn direction(&self, layout: StatusWord) -> Option<Direction> {
83        let status = [Obis::ENERGY_IMPORT, Obis::ENERGY_EXPORT]
84            .into_iter()
85            .find_map(|obis| self.get(obis).and_then(|e| e.status))?;
86        Some(if status & layout.mask() == 0 { Direction::Import } else { Direction::Export })
87    }
88
89    /// The power (`16.7.0`) with the sign from the status word: a power without a sign
90    /// is negated when the meter reports export.
91    ///
92    /// A value the meter already sent negative is returned as sent. If the status then
93    /// says import, the two disagree: on a DZG meter that is the DVS74 sign bug
94    /// (`ParseOptions::with_dzg_power`), see the Meter quirks page.
95    pub fn signed_power(&self, layout: StatusWord) -> Option<Quantity> {
96        let power = self.quantity(Obis::POWER)?;
97        Some(match self.direction(layout) {
98            Some(Direction::Export) if power.mantissa > 0 => Quantity { mantissa: -power.mantissa, ..power },
99            _ => power,
100        })
101    }
102}