From 0af21ed426013ecac6404dc836c560a0654fca47 Mon Sep 17 00:00:00 2001 From: heroichornet Date: Fri, 29 Mar 2024 10:40:31 +0100 Subject: [PATCH] WIP adding additional layers of abstraction in accordance with normative descriptions Currently tests are failing. Starting from v0.1.0 semantic versioning should be strictly introduced and also branching for new features with a clear branching naming scheme --- src/user_data/data_information.rs | 29 +++++++++++++++++------------ src/user_data/value_information.rs | 13 +++++++------ src/user_data/variable_user_data.rs | 4 ++-- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/user_data/data_information.rs b/src/user_data/data_information.rs index 47efba2..3924c7e 100644 --- a/src/user_data/data_information.rs +++ b/src/user_data/data_information.rs @@ -1,16 +1,21 @@ +pub struct DataInformationBlock { + pub _data_information_field: DataInformationField, + pub _data_information_field_extension: Option, +} + #[derive(Debug, Clone, PartialEq)] -pub struct DataInformation { +pub struct DataInformationField { pub storage_number: u64, pub function_field: FunctionField, pub data_field_coding: DataFieldCoding, - pub data_information_extension: Option, + pub data_information_extension: Option, pub size: usize, } const MAXIMUM_DATA_INFORMATION_SIZE: usize = 11; #[derive(Debug, Clone, PartialEq)] -pub struct DataInformationExtension {} +pub struct DataInformationExtensionField {} #[derive(Debug, PartialEq)] pub enum DataInformationError { @@ -19,7 +24,7 @@ pub enum DataInformationError { DataTooShort, } -impl TryFrom<&[u8]> for DataInformation { +impl TryFrom<&[u8]> for DataInformationField { type Error = DataInformationError; fn try_from(data: &[u8]) -> Result { @@ -79,12 +84,12 @@ impl TryFrom<&[u8]> for DataInformation { _ => unreachable!(), // This case should never occur due to the 4-bit width }; - Ok(DataInformation { + Ok(DataInformationField { storage_number, function_field, data_field_coding, data_information_extension: if extension_bit { - Some(DataInformationExtension {}) + Some(DataInformationExtensionField {}) } else { None }, @@ -93,7 +98,7 @@ impl TryFrom<&[u8]> for DataInformation { } } -impl DataInformation { +impl DataInformationField { pub fn get_size(&self) -> usize { self.size } @@ -272,10 +277,10 @@ mod tests { #[test] fn test_data_information() { let data = [0x13 as u8]; - let result = DataInformation::try_from(data.as_slice()); + let result = DataInformationField::try_from(data.as_slice()); assert_eq!( result, - Ok(DataInformation { + Ok(DataInformationField { storage_number: 0, function_field: FunctionField::MaximumValue, data_field_coding: DataFieldCoding::Integer24Bit, @@ -290,7 +295,7 @@ mod tests { let data = [ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, ]; - let result = DataInformation::try_from(data.as_slice()); + let result = DataInformationField::try_from(data.as_slice()); assert_eq!(result, Err(DataInformationError::DataTooLong)); } @@ -299,14 +304,14 @@ mod tests { let data = [ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, ]; - let result = DataInformation::try_from(data.as_slice()); + let result = DataInformationField::try_from(data.as_slice()); assert_ne!(result, Err(DataInformationError::DataTooLong)); } #[test] fn test_short_data_information() { let data = [0xFF]; - let result = DataInformation::try_from(data.as_slice()); + let result = DataInformationField::try_from(data.as_slice()); assert_eq!(result, Err(DataInformationError::DataTooShort)); } } diff --git a/src/user_data/value_information.rs b/src/user_data/value_information.rs index 7856c9c..954318c 100644 --- a/src/user_data/value_information.rs +++ b/src/user_data/value_information.rs @@ -280,7 +280,6 @@ impl TryFrom<&ValueInformation> for Unit { _ => todo!("Implement the rest of the units: {:?}", x), }, ValueInformation::PlainText(_) => Ok(Unit::PlainText), - ValueInformation::PlainText(_) => Ok(Unit::PlainText), ValueInformation::Extended(x) => match x { VIFExtension::EnergyMWh(_) => Ok(Unit::MegaWattHour), VIFExtension::EnergyGJ(_) => Ok(Unit::GigaJoul), @@ -340,13 +339,11 @@ mod tests { } // - // To solve this descrpeancy the parser needs to be configurable + // To solve this issue the parser needs to be configurable // it should try to parse according to mbus and if it fails it should try to parse // with the wrong, but common, method - #[test] fn test_plain_text_vif_common_none_norm_conform() { - use crate::user_data::value_information::VIFExtension; use crate::user_data::value_information::ValueInformation; use arrayvec::ArrayVec; // This is how the VIF is encoded in the test vectors @@ -354,8 +351,9 @@ mod tests { // the MBUS Norm which explicitly states that the VIIFE should be after the VIF // not aftter the ASCII plain text and its size // VIF LEN(3) 'R' 'H' '%' VIFE - //0xFC, 0x03, 0x48, 0x52, 0x25, 0x74, + // 0xFC, 0x03, 0x48, 0x52, 0x25, 0x74, // %RH + // VIFE = 0x74 => E111 0nnn Multiplicative correction factor for value (not unit): 10nnn–6 => 10^-2 let data = [0xFC, 0x03, 0x48, 0x52, 0x25, 0x74]; let mut a = ArrayVec::::new(); a.try_extend_from_slice(&data[2..5]).unwrap(); @@ -371,8 +369,11 @@ mod tests { use arrayvec::ArrayVec; // This is the ascii conform method of encoding the VIF // VIF VIFE LEN(3) 'R' 'H' '%' - //0xFC, 0x74, 0x03, 0x48, 0x52, 0x25, + // 0xFC, 0x74, 0x03, 0x48, 0x52, 0x25, // %RH + // Combinable (orthogonal) VIFE-Code extension table + // VIFE = 0x74 => E111 0nnn Multiplicative correction factor for value (not unit): 10nnn–6 => 10^-2 + // let data = [0xFC, 0x74, 0x03, 0x48, 0x52, 0x25]; let mut a = ArrayVec::::new(); a.try_extend_from_slice(&data[2..5]).unwrap(); diff --git a/src/user_data/variable_user_data.rs b/src/user_data/variable_user_data.rs index 305b92d..15a8c13 100644 --- a/src/user_data/variable_user_data.rs +++ b/src/user_data/variable_user_data.rs @@ -1,5 +1,5 @@ use super::data_information::FunctionField; -use super::data_information::{self, DataInformation}; +use super::data_information::{self, DataInformationField}; use super::value_information::{self, Unit, ValueInformation}; use super::DataRecords; @@ -124,7 +124,7 @@ impl From for DataRecordError { impl TryFrom<&[u8]> for DataRecord { type Error = DataRecordError; fn try_from(data: &[u8]) -> Result { - let data_information = DataInformation::try_from(data)?; + let data_information = DataInformationField::try_from(data)?; let value_information = ValueInformation::try_from(&data[1..])?; let value_and_data_information_size = data_information.get_size() + value_information.get_size();