sunspec/models/
model2.rs

1//! Basic Aggregator
2/// Basic Aggregator
3///
4/// Aggregates a collection of models for a given model id
5#[derive(Debug)]
6#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
7pub struct Model2 {
8    /// AID
9    ///
10    /// Aggregated model id
11    pub aid: u16,
12    /// N
13    ///
14    /// Number of aggregated models
15    pub n: u16,
16    /// UN
17    ///
18    /// Update Number.  Incrementing number each time the mapping is changed.  If the number is not changed from the last reading the direct access to a specific offset will result in reading the same logical model as before.  Otherwise the entire model must be read to refresh the changes
19    pub un: u16,
20    /// Status
21    ///
22    /// Enumerated status code
23    pub st: St,
24    /// Vendor Status
25    ///
26    /// Vendor specific status code
27    pub st_vnd: Option<u16>,
28    /// Event Code
29    ///
30    /// Bitmask event code
31    pub evt: Evt,
32    /// Vendor Event Code
33    ///
34    /// Vendor specific event code
35    pub evt_vnd: Option<EvtVnd>,
36    /// Control
37    ///
38    /// Control register for all aggregated devices
39    pub ctl: Option<Ctl>,
40    /// Vendor Control
41    ///
42    /// Vendor control register for all aggregated devices
43    pub ctl_vnd: Option<u32>,
44    /// Control Value
45    ///
46    /// Numerical value used as a parameter to the control
47    pub ctl_vl: Option<u32>,
48}
49#[allow(missing_docs)]
50impl Model2 {
51    pub const AID: crate::Point<Self, u16> = crate::Point::new(0, 1, false);
52    pub const N: crate::Point<Self, u16> = crate::Point::new(1, 1, false);
53    pub const UN: crate::Point<Self, u16> = crate::Point::new(2, 1, false);
54    pub const ST: crate::Point<Self, St> = crate::Point::new(3, 1, false);
55    pub const ST_VND: crate::Point<Self, Option<u16>> = crate::Point::new(4, 1, false);
56    pub const EVT: crate::Point<Self, Evt> = crate::Point::new(5, 2, false);
57    pub const EVT_VND: crate::Point<Self, Option<EvtVnd>> = crate::Point::new(7, 2, false);
58    pub const CTL: crate::Point<Self, Option<Ctl>> = crate::Point::new(9, 1, false);
59    pub const CTL_VND: crate::Point<Self, Option<u32>> = crate::Point::new(10, 2, false);
60    pub const CTL_VL: crate::Point<Self, Option<u32>> = crate::Point::new(12, 2, false);
61}
62impl crate::Model for Model2 {
63    const ID: u16 = 2;
64    fn from_data(data: &[u16]) -> Result<Self, crate::DecodeError> {
65        Ok(Self {
66            aid: Self::AID.from_data(data)?,
67            n: Self::N.from_data(data)?,
68            un: Self::UN.from_data(data)?,
69            st: Self::ST.from_data(data)?,
70            st_vnd: Self::ST_VND.from_data(data)?,
71            evt: Self::EVT.from_data(data)?,
72            evt_vnd: Self::EVT_VND.from_data(data)?,
73            ctl: Self::CTL.from_data(data)?,
74            ctl_vnd: Self::CTL_VND.from_data(data)?,
75            ctl_vl: Self::CTL_VL.from_data(data)?,
76        })
77    }
78    fn addr(models: &crate::Models) -> crate::ModelAddr<Self> {
79        models.m2
80    }
81}
82/// Status
83///
84/// Enumerated status code
85#[derive(Copy, Clone, Debug, Eq, PartialEq, strum::FromRepr)]
86#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
87#[repr(u16)]
88pub enum St {
89    #[allow(missing_docs)]
90    Off = 1,
91    #[allow(missing_docs)]
92    On = 2,
93    #[allow(missing_docs)]
94    Full = 3,
95    #[allow(missing_docs)]
96    Fault = 4,
97}
98impl crate::Value for St {
99    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
100        let value = u16::decode(data)?;
101        Self::from_repr(value).ok_or(crate::DecodeError::InvalidEnumValue)
102    }
103    fn encode(self) -> Box<[u16]> {
104        (self as u16).encode()
105    }
106}
107impl crate::Value for Option<St> {
108    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
109        let value = u16::decode(data)?;
110        if value != 65535 {
111            Ok(Some(
112                St::from_repr(value).ok_or(crate::DecodeError::InvalidEnumValue)?,
113            ))
114        } else {
115            Ok(None)
116        }
117    }
118    fn encode(self) -> Box<[u16]> {
119        if let Some(value) = self {
120            value.encode()
121        } else {
122            65535.encode()
123        }
124    }
125}
126bitflags::bitflags! {
127    #[doc = " Event Code"] #[doc = " "] #[doc = " Bitmask event code"] #[derive(Copy,
128    Clone, Debug, Eq, PartialEq)] #[cfg_attr(feature = "serde",
129    derive(::serde::Serialize, ::serde::Deserialize))] pub struct Evt : u32 {
130    #[allow(missing_docs)] const GroundFault = 1; #[allow(missing_docs)] const
131    InputOverVoltage = 2; #[allow(missing_docs)] const Reserved2 = 4;
132    #[allow(missing_docs)] const DcDisconnect = 8; #[allow(missing_docs)] const Reserved4
133    = 16; #[allow(missing_docs)] const Reserved5 = 32; #[allow(missing_docs)] const
134    ManualShutdown = 64; #[allow(missing_docs)] const OverTemperature = 128;
135    #[allow(missing_docs)] const Reserved8 = 256; #[allow(missing_docs)] const Reserved9
136    = 512; #[allow(missing_docs)] const Reserved10 = 1024; #[allow(missing_docs)] const
137    Reserved11 = 2048; #[allow(missing_docs)] const BlownFuse = 4096;
138    #[allow(missing_docs)] const UnderTemperature = 8192; #[allow(missing_docs)] const
139    MemoryLoss = 16384; #[allow(missing_docs)] const ArcDetection = 32768;
140    #[allow(missing_docs)] const TheftDetection = 65536; #[allow(missing_docs)] const
141    OutputOverCurrent = 131072; #[allow(missing_docs)] const OutputOverVoltage = 262144;
142    #[allow(missing_docs)] const OutputUnderVoltage = 524288; #[allow(missing_docs)]
143    const TestFailed = 1048576; }
144}
145impl crate::Value for Evt {
146    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
147        let value = u32::decode(data)?;
148        Ok(Self::from_bits_retain(value))
149    }
150    fn encode(self) -> Box<[u16]> {
151        self.bits().encode()
152    }
153}
154impl crate::Value for Option<Evt> {
155    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
156        let value = u32::decode(data)?;
157        if value != 4294967295u32 {
158            Ok(Some(Evt::from_bits_retain(value)))
159        } else {
160            Ok(None)
161        }
162    }
163    fn encode(self) -> Box<[u16]> {
164        if let Some(value) = self {
165            value.encode()
166        } else {
167            4294967295u32.encode()
168        }
169    }
170}
171bitflags::bitflags! {
172    #[doc = " Vendor Event Code"] #[doc = " "] #[doc = " Vendor specific event code"]
173    #[derive(Copy, Clone, Debug, Eq, PartialEq)] #[cfg_attr(feature = "serde",
174    derive(::serde::Serialize, ::serde::Deserialize))] pub struct EvtVnd : u32 {}
175}
176impl crate::Value for EvtVnd {
177    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
178        let value = u32::decode(data)?;
179        Ok(Self::from_bits_retain(value))
180    }
181    fn encode(self) -> Box<[u16]> {
182        self.bits().encode()
183    }
184}
185impl crate::Value for Option<EvtVnd> {
186    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
187        let value = u32::decode(data)?;
188        if value != 4294967295u32 {
189            Ok(Some(EvtVnd::from_bits_retain(value)))
190        } else {
191            Ok(None)
192        }
193    }
194    fn encode(self) -> Box<[u16]> {
195        if let Some(value) = self {
196            value.encode()
197        } else {
198            4294967295u32.encode()
199        }
200    }
201}
202/// Control
203///
204/// Control register for all aggregated devices
205#[derive(Copy, Clone, Debug, Eq, PartialEq, strum::FromRepr)]
206#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
207#[repr(u16)]
208pub enum Ctl {
209    #[allow(missing_docs)]
210    None = 0,
211    #[allow(missing_docs)]
212    Automatic = 1,
213    #[allow(missing_docs)]
214    ForceOff = 2,
215    #[allow(missing_docs)]
216    Test = 3,
217    #[allow(missing_docs)]
218    Throttle = 4,
219}
220impl crate::Value for Ctl {
221    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
222        let value = u16::decode(data)?;
223        Self::from_repr(value).ok_or(crate::DecodeError::InvalidEnumValue)
224    }
225    fn encode(self) -> Box<[u16]> {
226        (self as u16).encode()
227    }
228}
229impl crate::Value for Option<Ctl> {
230    fn decode(data: &[u16]) -> Result<Self, crate::DecodeError> {
231        let value = u16::decode(data)?;
232        if value != 65535 {
233            Ok(Some(
234                Ctl::from_repr(value).ok_or(crate::DecodeError::InvalidEnumValue)?,
235            ))
236        } else {
237            Ok(None)
238        }
239    }
240    fn encode(self) -> Box<[u16]> {
241        if let Some(value) = self {
242            value.encode()
243        } else {
244            65535.encode()
245        }
246    }
247}