Skip to content

Commit

Permalink
Basic implementation for CRC processing which works with long/short f…
Browse files Browse the repository at this point in the history
…rames.

Related to #59.
  • Loading branch information
splatch committed Nov 12, 2020
1 parent 7a74890 commit 383ea00
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Copyright (c) 2010-2018 by the respective copyright holders.
*
* <p>
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -10,30 +10,47 @@

import org.openhab.binding.wmbus.tools.Processor;

import java.util.HashMap;
import java.util.Map;

public class SkipCrcProcessor implements Processor<String> {

@Override
public String process(String frame, Map<String, Object> context) {
String strippedframe = "";
int len = frame.length() / 2; // include all fields

//int lengthField = Integer.decode(frame.substring(0, 2));
int ciField = parseInt(frame, 22, 24);
boolean formatB = ciField == 0x72 || ciField == 0x7A;

if (formatB) {
strippedframe += frame.substring(0, Math.min(18, frame.length() - 4));
// block 1 + block 2 = 11 + ((16*n)+2) -- max 129 bytes
if (len <= 129) {
strippedframe += frame.substring(Math.min(22, frame.length()), frame.length() - 4);
}
// block 1 + block 2 + block 3= 11 + ((16*n)+2) + (16*n) -- bytes > 129
else {
strippedframe += frame.substring(22, 256);
strippedframe += frame.substring(Math.min(260, frame.length()), frame.length() - 4);
}
} else {
// assume that we starts counting from C-field
strippedframe += frame.substring(0, Math.min(18, frame.length()));
int position = 22;
while (position < frame.length()) {
strippedframe += frame.substring(position, Math.min(position + 32, frame.length()));
position += 36;
}
strippedframe = strippedframe.substring(0,strippedframe.length()-4);
}

strippedframe += frame.substring(Math.min(0, frame.length()), Math.min(18, frame.length()));
strippedframe += frame.substring(Math.min(22, frame.length()), Math.min(54, frame.length()));
strippedframe += frame.substring(Math.min(58, frame.length()), Math.min(90, frame.length()));

// String strippedframe = frame.substring(0, 18) + frame.substring(22, frame.length());
/*
* TODO: general implementation
*
*
* Integer position = 2;
* while (position < frame.length()) {
* strippedframe += frame.substring(position, position+16);
* position += 16 ;
* }
*/
return strippedframe;
}

private int parseInt(String frame, int startIndex, int endIndex) {
return Integer.parseInt(frame.substring(startIndex, endIndex), 16);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.openhab.binding.wmbus.tools.processor;

import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.junit.Test;

public class SkipCrcProcessorTest extends TestCase {

SkipCrcProcessor stripCrc = new SkipCrcProcessor();
Map<String, Object> context = new HashMap<>();

@Test
public void testFrameFormatB() {
String input = "11111111111111111100007A1111111111111111111111111111110000";
String output = "1111111111111111117A111111111111111111111111111111";
String result = stripCrc.process(input, context);

assertEquals(output, result);
}

@Test
public void testFrame1() {
String input = "111111111111111111000011111111111111111111111111110000";
String output = "1111111111111111111111111111111111111111111111";
String result = stripCrc.process(input, context);

assertEquals(output, result);
}

@Test
public void testFrame3() {
String input = "11111111111111111100001111111111111111111111111111111100001111111111110000";
String output = "11111111111111111111111111111111111111111111111111111111111111";
String result = stripCrc.process(input, context);

assertEquals(output, result);
}

}

0 comments on commit 383ea00

Please sign in to comment.