1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::collections::hash_map::HashMap;

use crate::VEError;

// Data types
type Watt = i32;
type Percent = f32;
type Volt = f32;
type Minute = i32;

// Type conversion errors
impl From<std::num::ParseIntError> for VEError {
    fn from(src: std::num::ParseIntError) -> VEError {
        VEError::Parse(format!("Error parsing integer: {}", src))
    }
}

impl From<std::num::ParseFloatError> for VEError {
    fn from(src: std::num::ParseFloatError) -> VEError {
        VEError::Parse(format!("Error parsing float: {}", src))
    }
}

/// Data for BMV 600 battery monitor series
// struct Bmv600 {}

/// Data for BMV 700 battery monitor series
#[derive(Debug)]
pub struct Bmv700 {
    /// Main (channel 1) battery voltage. Labelled `V`
    /// Units: V
    /// Available on: BMV 600, BMV 700, MPPT, Inverter
    pub voltage: Volt,

    /// Instantaneous power. Labelled `P`
    /// Units: W
    /// Available on: BMV 700
    pub power: Watt,

    /// Consumed Amp Hours. Labelled `CE`
    /// Units: mAh (When the BMV is not synchronised, these statistics have no meaning, so "---" will be sent instead of a value)
    pub consumed: Option<String>,

    /// State of charge. Labelled `SOC`
    /// Unit: Percent (When the BMV is not synchronised, these statistics have no meaning, so "---" will be sent instead of a value)
    /// Available on: BMV 600, BMV 700
    pub soc: Option<Percent>,

    /// Time-to-go. Labelled `TTG`
    /// Units: Minutes (When the battery is not discharging the time-to-go is infinite. This is represented as -1)
    /// Available on: BMV 600, BMV 700
    pub ttg: Minute,
}

/// Data for all MPPT solar charge controller
// struct MPPT {}

/// Data for Phoenix Inverters
// struct PhoenixInverter {}

/// Data for Phoenix Chargers
// struct PhoenixCharger {}

/// Data for all devices
// pub struct Everything {}

/// "When the BMV is not synchronised, these statistics have no meaning, so "---" will be sent instead of a value"
fn convert_percentage(rawkeys: &HashMap<&str, &str>, label: &str) -> Result<Option<Percent>, VEError> {
    let raw = *(*rawkeys).get(label).ok_or(VEError::MissingField(label.into()))?;

    if raw == "---" {
        Ok(None)
    } else {
        Ok(Some(raw.parse::<Percent>()? / 10.0))
    }
}

fn convert_volt(rawkeys: &HashMap<&str, &str>, label: &str) -> Result<Volt, VEError> {
    let raw = (*rawkeys).get(label).ok_or(VEError::MissingField(label.into()))?;
    let cleaned = raw.parse::<Volt>()? / 10.0;
    Ok(cleaned)
}

fn convert_watt(rawkeys: &HashMap<&str, &str>, label: &str) -> Result<Watt, VEError> {
    let raw = (*rawkeys).get(label).ok_or(VEError::MissingField(label.into()))?;
    let cleaned = raw.parse::<Watt>()?;
    Ok(cleaned)
}

fn convert_string(rawkeys: &HashMap<&str, &str>, label: &str) -> Result<String, VEError> {
    let raw = *(*rawkeys).get(label).ok_or(VEError::MissingField(label.into()))?;
    Ok(raw.into())
}

fn convert_ttg(rawkeys: &HashMap<&str, &str>, label: &str) -> Result<Minute, VEError> {
    let raw = *(*rawkeys).get(label).ok_or(VEError::MissingField(label.into()))?;
    let cleaned = raw.parse::<Minute>()?;
    Ok(cleaned)
}

pub fn mapraw(fields: &Vec<crate::parser::Field>) -> Result<Bmv700, VEError> {
    // Convert from list into map
    let mut hm: HashMap<&str, &str> = HashMap::new();
    for f in fields {
        hm.insert(f.key, f.value);
    }

    Ok(Bmv700 {
        voltage: convert_volt(&hm, "V")?,
        power: convert_watt(&hm, "P")?,
        consumed: Some(convert_string(&hm, "CE")?),
        soc: convert_percentage(&hm, "SOC")?,
        ttg: convert_ttg(&hm, "TTG")?,
    })
}

#[test]
fn test_mapping() {
    let raw = crate::parser::parse(
        "\r\nP\t123\r\nCE\t53\r\nSOC\t452\r\nTTG\t60\r\nRelay\tOFF\r\nAlarm\tOFF\r\nV\t232\r\nChecksum\t12".as_bytes(),
    )
    .unwrap();
    let data = mapraw(&raw).unwrap();
    assert_eq!(data.power, 123);
    assert_eq!(data.consumed, Some("53".into()));
    assert_eq!(data.soc, Some(45.2));
    assert_eq!(data.ttg, 60);
    assert_eq!(data.voltage, 23.2);
}