-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/strip-crc' into 'master'
CRC processing which works with long/short frames See merge request openhab/wmbus!23
- Loading branch information
Showing
13 changed files
with
421 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
org.openhab.binding.wmbus.tools/src/main/java/org/openhab/binding/wmbus/tools/Processor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* 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 | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.openhab.binding.wmbus.tools; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Generic purpose frame processor. | ||
* | ||
* @param <T> Type of frame representation. | ||
*/ | ||
public interface Processor<T> { | ||
|
||
String RSSI = "rssi"; | ||
|
||
T process(T frame, Map<String, Object> context); | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
....wmbus.tools/src/main/java/org/openhab/binding/wmbus/tools/processor/CalculateLength.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* 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 | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.openhab.binding.wmbus.tools.processor; | ||
|
||
import org.openhab.binding.wmbus.tools.Processor; | ||
|
||
import java.util.Map; | ||
|
||
public class CalculateLength implements Processor<String> { | ||
|
||
@Override | ||
public String process(String frame, Map<String, Object> context) { | ||
// remember of hex notation which doubles length | ||
Integer len = frame.length() / 2; | ||
return Integer.toHexString(len) + frame; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...nding.wmbus.tools/src/main/java/org/openhab/binding/wmbus/tools/processor/Processors.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 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 | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.openhab.binding.wmbus.tools.processor; | ||
|
||
import org.openhab.binding.wmbus.tools.Processor; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Helper type to orchestrate execution of several frame processors at once. | ||
* | ||
* @author Łukasz Dywicki - Initial contribution. | ||
*/ | ||
public class Processors { | ||
|
||
public static <T> T process(final T value, Map<String, Object> context, List<Processor<T>> processors) { | ||
if (processors.isEmpty()) { | ||
return value; | ||
} | ||
|
||
T result = value; | ||
for (Processor<T> processor : processors) { | ||
result = processor.process(result, context); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...mbus.tools/src/main/java/org/openhab/binding/wmbus/tools/processor/RecalculateLength.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* 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 | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.openhab.binding.wmbus.tools.processor; | ||
|
||
import org.openhab.binding.wmbus.tools.Processor; | ||
|
||
import java.util.Map; | ||
|
||
public class RecalculateLength implements Processor<String> { | ||
|
||
@Override | ||
public String process(String frame, Map<String, Object> context) { | ||
int inputByteLength = frame.length() / 2; | ||
|
||
String newFrame = ""; | ||
Integer numberOfBlocks; | ||
int ciField = parseInt(frame, 20, 22); | ||
|
||
if (ciField == 0x7A){ | ||
numberOfBlocks = (inputByteLength-14) / 16; | ||
inputByteLength = 14 + numberOfBlocks*16; | ||
newFrame += Integer.toHexString(inputByteLength); | ||
newFrame += frame.substring(2, 26); | ||
newFrame += Integer.toHexString(numberOfBlocks*16); | ||
newFrame += frame.substring(28, (inputByteLength * 2) + 2); | ||
return newFrame; | ||
} | ||
|
||
if (ciField == 0x72){ | ||
numberOfBlocks = (inputByteLength-22) / 16; | ||
inputByteLength = 22 + numberOfBlocks*16; | ||
newFrame += Integer.toHexString(inputByteLength); | ||
newFrame += frame.substring(2, 42); | ||
newFrame += Integer.toHexString(numberOfBlocks*16); | ||
newFrame += frame.substring(44, (inputByteLength * 2) + 2); | ||
return newFrame; | ||
} | ||
|
||
return newFrame; | ||
} | ||
|
||
private int parseInt(String frame, int startIndex, int endIndex) { | ||
return Integer.parseInt(frame.substring(startIndex, endIndex), 16); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
...ng.wmbus.tools/src/main/java/org/openhab/binding/wmbus/tools/processor/RssiProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* 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 | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.openhab.binding.wmbus.tools.processor; | ||
|
||
import org.openhab.binding.wmbus.tools.Processor; | ||
|
||
import java.util.Map; | ||
|
||
public class RssiProcessor implements Processor<String> { | ||
|
||
private final int rssiIndex; | ||
private final int rssiValue; | ||
|
||
public RssiProcessor(int rssiIndex, int rssiValue) { | ||
this.rssiIndex = rssiIndex; | ||
this.rssiValue = rssiValue; | ||
} | ||
|
||
@Override | ||
public String process(String frame, Map<String, Object> context) { | ||
if (rssiIndex != 0) { | ||
if (rssiIndex == 1) { // first byte starts from character at index 0. | ||
context.put(RSSI, Integer.parseUnsignedInt(frame.substring(0, 2), 16)); | ||
return frame.substring(2); | ||
} else if (rssiIndex == -1) { | ||
context.put(RSSI, Integer.parseUnsignedInt(frame.substring(frame.length() -2), 16)); | ||
return frame.substring(0, frame.length() - 2); | ||
} | ||
} | ||
|
||
context.put(RSSI, rssiValue); | ||
return frame; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...wmbus.tools/src/main/java/org/openhab/binding/wmbus/tools/processor/SkipCrcProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* 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 | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package org.openhab.binding.wmbus.tools.processor; | ||
|
||
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); | ||
} | ||
|
||
return strippedframe; | ||
} | ||
|
||
private int parseInt(String frame, int startIndex, int endIndex) { | ||
return Integer.parseInt(frame.substring(startIndex, endIndex), 16); | ||
} | ||
|
||
} |
Oops, something went wrong.