Skip to content

Commit

Permalink
Fix parsing larger chunks of input
Browse files Browse the repository at this point in the history
  • Loading branch information
mthmulders committed Oct 26, 2023
1 parent be019c4 commit a5457b4
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 2 deletions.
45 changes: 45 additions & 0 deletions resources/test/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
00.000*kW)
1-0:62.7.0(00.000*kW)
0-1:24.1.0(003)
0-1:96.1.0(4730303839353635363131313938373233)
0-1:24.2.1(231026204004S)(00004.381*m3)
!BAD0
/ISK5\2M550T-1013

1-3:0.2.8(50)
0-0:1.0.0(231026204015S)
0-0:96.1.1(4530303534303038353539313239303233)
1-0:1.8.1(000032.159*kWh)
1-0:1.8.2(000002.167*kWh)
1-0:2.8.1(000002.376*kWh)
1-0:2.8.2(000000.000*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(00.302*kW)
1-0:2.7.0(00.000*kW)
0-0:96.7.21(00005)
0-0:96.7.9(00003)
1-0:99.97.0(1)(0-0:96.7.19)(230114121128W)(0000000324*s)
1-0:32.32.0(00000)
1-0:52.32.0(00000)
1-0:72.32.0(00000)
1-0:32.36.0(00003)
1-0:52.36.0(00003)
1-0:72.36.0(00003)
0-0:96.13.0()
1-0:32.7.0(234.3*V)
1-0:52.7.0(235.6*V)
1-0:72.7.0(230.8*V)
1-0:31.7.0(001*A)
1-0:51.7.0(000*A)
1-0:71.7.0(000*A)
1-0:21.7.0(00.250*kW)
1-0:41.7.0(00.042*kW)
1-0:61.7.0(00.000*kW)
1-0:22.7.0(00.000*kW)
1-0:42.7.0(00.000*kW)
1-0:62.7.0(00.000*kW)
0-1:24.1.0(003)
0-1:96.1.0(4730303839353635363131313938373233)
0-1:24.2.1(231026204004S)(00004.381*m3)
!3812
/ISK5\2M550T-1013
38 changes: 38 additions & 0 deletions resources/test/output1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/ISK5\2M550T-1013

1-3:0.2.8(50)
0-0:1.0.0(231026204015S)
0-0:96.1.1(4530303534303038353539313239303233)
1-0:1.8.1(000032.159*kWh)
1-0:1.8.2(000002.167*kWh)
1-0:2.8.1(000002.376*kWh)
1-0:2.8.2(000000.000*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(00.302*kW)
1-0:2.7.0(00.000*kW)
0-0:96.7.21(00005)
0-0:96.7.9(00003)
1-0:99.97.0(1)(0-0:96.7.19)(230114121128W)(0000000324*s)
1-0:32.32.0(00000)
1-0:52.32.0(00000)
1-0:72.32.0(00000)
1-0:32.36.0(00003)
1-0:52.36.0(00003)
1-0:72.36.0(00003)
0-0:96.13.0()
1-0:32.7.0(234.3*V)
1-0:52.7.0(235.6*V)
1-0:72.7.0(230.8*V)
1-0:31.7.0(001*A)
1-0:51.7.0(000*A)
1-0:71.7.0(000*A)
1-0:21.7.0(00.250*kW)
1-0:41.7.0(00.042*kW)
1-0:61.7.0(00.000*kW)
1-0:22.7.0(00.000*kW)
1-0:42.7.0(00.000*kW)
1-0:62.7.0(00.000*kW)
0-1:24.1.0(003)
0-1:96.1.0(4730303839353635363131313938373233)
0-1:24.2.1(231026204004S)(00004.381*m3)
!3812
30 changes: 28 additions & 2 deletions src/dsmr/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use super::settings;
use super::settings::ParityBitSetting;

use std::borrow::Cow;
use std::fs;
use std::io::BufRead;
use std::io::BufReader;
use std::path::PathBuf;
use std::str;
use std::time::Duration;

Expand All @@ -14,10 +16,11 @@ fn find_start_of_telegram(buffer: &str) -> Option<usize> {
fn find_end_of_telegram(buffer: &str, from: usize) -> Option<usize> {
match buffer[from..].find('!') {
Some(excl_mark) => {
let remainder = &buffer[(from + excl_mark + 1)..];
// Not all meters send the checksum; some simply have a last line with just '!'
match buffer[(excl_mark + 1)..].find('\n') {
match remainder.find('\n') {
Some(line_end) => {
let end = excl_mark + line_end + 1;
let end = from + excl_mark + line_end + 1;
log::trace!(
"Exclamation mark with line ending found at index {}, returning index {}",
excl_mark,
Expand Down Expand Up @@ -252,4 +255,27 @@ mod tests {
);
assert_eq!(result, "\r\n/ISk5\\2MT382-1000");
}

#[test]
fn eat_complete_telegram() {
let mut input = read_test_resource("input1.txt".into());
let mut consumer = TestConsumer::new();

let _result = eat_telegrams(&mut input, &mut consumer);

assert_eq!(consumer.invoked, true);
assert_eq!(consumer.telegram, read_test_resource("output1.txt".into()),);
}

fn read_test_resource(path: PathBuf) -> String {
let mut test_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
test_file.push("resources/test/");
test_file.push(path);

let mut binding = fs::read_to_string(test_file).expect("Failed to read file");
let text = binding.as_mut_str();
return String::from(text);

// return fs::read_to_string(test_file).expect("Failed to read file").as_mut_str();
}
}

0 comments on commit a5457b4

Please sign in to comment.