Skip to content

Commit

Permalink
correctly parsing exponent for cubic meter
Browse files Browse the repository at this point in the history
  • Loading branch information
maebli committed Mar 15, 2024
1 parent a05c5ad commit 5ff87ac
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 72 deletions.
121 changes: 54 additions & 67 deletions src/user_data/value_information.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use arrayvec::ArrayVec;

use super::variable_user_data::Exponent;

#[derive(Debug, PartialEq)]
pub enum ValueInformation {
Primary(u8),
Expand Down Expand Up @@ -190,80 +192,65 @@ pub enum VIFExtension {

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Unit {
Hms,
DMY,
Wh,
Wh1e1,
Wh1e2,
KWh,
KWh1e1,
KWh1e2,
MWh,
MWh1e1,
MWh1e2,
KJ,
KJ1e1,
KJ1e2,
MJ,
MJ1e1,
MJ1e2,
GJ,
GJ1e1,
GJ1e2,
W,
W1e1,
W1e2,
KW,
KW1e1,
KW1e2,
MW,
MW1e1,
MW1e2,
KJH,
KJH1e1,
KJH1e2,
MJH,
MJH1e1,
MJH1e2,
GJH,
GJH1e1,
GJH1e2,
Ml,
Ml1e1,
Ml1e2,
HourMinuteSecond,
DayMonthYear,
WattHour,
KiloWattHour,
MegaWattHour,
KiloJoul,
MegaJoul,
GigaJoul,
Watt,
KiloWatt,
MegaWat,
KiloJoulHour,
MegaJoulHour,
GigaJoulHour,
MegaLiter,
Liter,
L1e1,
L1e2,
M3,
M31e1,
M31e2,
MlH,
MlH1e1,
MlH1e2,
LH,
LH1e1,
LH1e2,
M3H,
M3H1e1,
M3H1e2,
Celsius1e3,
UnitsForHCA,
Reserved3A,
Reserved3B,
Reserved3C,
Reserved3D,
SameButHistoric,
CubicMeter,
MegaLiterHour,
LiterHour,
CubicMeterHour,
Celsius,
HCA,
Reserved,
WithoutUnits,
}

impl TryFrom<ValueInformation> for Unit {
impl TryFrom<&ValueInformation> for Unit {
type Error = ValueInformationError;

fn try_from(value_information: &ValueInformation) -> Result<Self, ValueInformationError> {
match value_information {
ValueInformation::Primary(x) => match x {
0x12..=0x16 => Ok(Unit::CubicMeter),
_ => todo!("Implement the rest of the units: {:?}", x),
},
ValueInformation::PlainText => todo!(),
ValueInformation::Extended(_) => todo!(),
ValueInformation::Any => todo!(),
ValueInformation::ManufacturerSpecific => todo!(),
}
}
}

impl From<isize> for Exponent {
fn from(value: isize) -> Self {
match value {
value => Exponent { inner: value },
}
}
}

impl TryFrom<&ValueInformation> for Exponent {
type Error = ValueInformationError;

fn try_from(value_information: ValueInformation) -> Result<Self, ValueInformationError> {
fn try_from(value_information: &ValueInformation) -> Result<Self, ValueInformationError> {
match value_information {
ValueInformation::Primary(x) => match x {
0x13 => Ok(Unit::Liter),
_ => todo!(),
0x12..=0x16 => Ok(Exponent::from((x & 0b111) as isize - 3)),
_ => todo!("Implement the rest of the units: {:?}", x),
},
ValueInformation::PlainText => todo!(),
ValueInformation::Extended(_) => todo!(),
Expand Down Expand Up @@ -291,6 +278,6 @@ mod tests {
let result = ValueInformation::try_from(data.as_slice()).unwrap();
assert_eq!(result, ValueInformation::Primary(0x13));
assert_eq!(result.get_size(), 1);
assert_eq!(Unit::try_from(result).unwrap(), Unit::Liter);
assert_eq!(Unit::try_from(&result).unwrap(), Unit::CubicMeter);
}
}
16 changes: 11 additions & 5 deletions src/user_data/variable_user_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ pub struct DataRecord {
function: FunctionField,
storage_number: u64,
unit: Unit,
exponent: isize,
exponent: Exponent,
quantity: Quantity,
value: f64,
size: usize,
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Exponent {
pub inner: isize,
}

#[derive(Debug, Copy, PartialEq, Clone)]
enum Quantity {
/* TODO */
Expand Down Expand Up @@ -66,8 +71,8 @@ impl TryFrom<&[u8]> for DataRecord {
Ok(DataRecord {
function: data_information.function_field,
storage_number: data_information.storage_number,
unit: Unit::try_from(value_information)?,
exponent: 0,
unit: Unit::try_from(&value_information)?,
exponent: Exponent::try_from(&value_information)?,
quantity: Quantity::Some,
value,
size: total_size,
Expand Down Expand Up @@ -127,6 +132,7 @@ mod tests {

#[test]
fn test_parse_variable_data() {
use crate::user_data::variable_user_data::Exponent;
use crate::user_data::{
data_information::FunctionField, value_information::Unit, variable_user_data::Quantity,
DataRecord, DataRecords,
Expand All @@ -141,8 +147,8 @@ mod tests {
Some(&DataRecord {
function: FunctionField::InstantaneousValue,
storage_number: 0,
unit: Unit::Liter,
exponent: 0,
unit: Unit::CubicMeter,
exponent: Exponent::from(-3),
quantity: Quantity::Some,
value: 12565.0,
size: 5,
Expand Down

0 comments on commit 5ff87ac

Please sign in to comment.