diff --git a/org.openhab.binding.wmbus.tools/src/main/java/org/openhab/binding/wmbus/tools/processor/SkipCrcProcessor.java b/org.openhab.binding.wmbus.tools/src/main/java/org/openhab/binding/wmbus/tools/processor/SkipCrcProcessor.java index 2e0ad96..357c963 100644 --- a/org.openhab.binding.wmbus.tools/src/main/java/org/openhab/binding/wmbus/tools/processor/SkipCrcProcessor.java +++ b/org.openhab.binding.wmbus.tools/src/main/java/org/openhab/binding/wmbus/tools/processor/SkipCrcProcessor.java @@ -1,6 +1,6 @@ /** * Copyright (c) 2010-2018 by the respective copyright holders. - * + *

* 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 @@ -10,6 +10,7 @@ import org.openhab.binding.wmbus.tools.Processor; +import java.util.HashMap; import java.util.Map; public class SkipCrcProcessor implements Processor { @@ -17,23 +18,39 @@ public class SkipCrcProcessor implements Processor { @Override public String process(String frame, Map 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); + } + } diff --git a/org.openhab.binding.wmbus.tools/src/test/java/org/openhab/binding/wmbus/tools/processor/SkipCrcProcessorTest.java b/org.openhab.binding.wmbus.tools/src/test/java/org/openhab/binding/wmbus/tools/processor/SkipCrcProcessorTest.java new file mode 100644 index 0000000..ef26bdd --- /dev/null +++ b/org.openhab.binding.wmbus.tools/src/test/java/org/openhab/binding/wmbus/tools/processor/SkipCrcProcessorTest.java @@ -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 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); + } + +} \ No newline at end of file