Skip to main content

sunspec/models/
model806.rs

1//! Flow Battery Model
2/// Type alias for [`FlowBattery`].
3pub type Model806 = FlowBattery;
4/// Flow Battery Model
5#[derive(Debug)]
6#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
7pub struct FlowBattery {
8    /// Battery Points To Be Determined
9    pub bat_tbd: u16,
10    #[allow(missing_docs)]
11    pub battery_string: Vec<BatteryString>,
12}
13#[allow(missing_docs)]
14impl FlowBattery {
15    pub const BAT_TBD: crate::Point<Self, u16> = crate::Point::new(0, 1, false);
16}
17impl crate::Group for FlowBattery {
18    const LEN: u16 = 1;
19}
20impl FlowBattery {
21    fn parse_group(data: &[u16]) -> Result<(&[u16], Self), crate::DecodeError> {
22        let nested_data = data
23            .get(usize::from(<Self as crate::Group>::LEN)..)
24            .unwrap_or(&[]);
25        let (nested_data, battery_string) = BatteryString::parse_multiple(nested_data)?;
26        Ok((
27            nested_data,
28            Self {
29                bat_tbd: Self::BAT_TBD.from_data(data)?,
30                battery_string,
31            },
32        ))
33    }
34}
35#[allow(missing_docs)]
36#[derive(Debug)]
37#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
38pub struct BatteryString {
39    /// Battery String Points To Be Determined
40    pub bat_st_tbd: u16,
41}
42#[allow(missing_docs)]
43impl BatteryString {
44    pub const BAT_ST_TBD: crate::Point<Self, u16> = crate::Point::new(0, 1, false);
45}
46impl crate::Group for BatteryString {
47    const LEN: u16 = 1;
48}
49impl BatteryString {
50    fn parse_group(data: &[u16]) -> Result<(&[u16], Self), crate::DecodeError> {
51        let nested_data = data
52            .get(usize::from(<Self as crate::Group>::LEN)..)
53            .unwrap_or(&[]);
54        Ok((
55            nested_data,
56            Self {
57                bat_st_tbd: Self::BAT_ST_TBD.from_data(data)?,
58            },
59        ))
60    }
61    fn parse_multiple(data: &[u16]) -> Result<(&[u16], Vec<Self>), crate::DecodeError> {
62        let group_len = usize::from(<BatteryString as crate::Group>::LEN);
63        if group_len == 0 {
64            return Ok((data, Vec::new()));
65        }
66        if data.len() % group_len != 0 {
67            return Err(crate::DecodeError::OutOfBounds);
68        }
69        let group_count = data.len() / group_len;
70        let (data, groups) =
71            (0..group_count).try_fold((data, Vec::new()), |(data, mut groups), _| {
72                let (data, group) = BatteryString::parse_group(data)?;
73                groups.push(group);
74                Ok::<_, crate::DecodeError>((data, groups))
75            })?;
76        Ok((data, groups))
77    }
78}
79impl crate::Model for FlowBattery {
80    const ID: u16 = 806;
81    fn addr(models: &crate::Models) -> crate::ModelAddr<Self> {
82        models.m806
83    }
84    fn parse(data: &[u16]) -> Result<Self, crate::ParseError<Self>> {
85        let (_, model) = Self::parse_group(data)?;
86        Ok(model)
87    }
88}