tbel: add parseBytesToDouble
This commit is contained in:
parent
50d3a6d925
commit
6d0b16e41c
@ -16,6 +16,7 @@
|
|||||||
package org.thingsboard.script.api.tbel;
|
package org.thingsboard.script.api.tbel;
|
||||||
|
|
||||||
import com.google.common.primitives.Bytes;
|
import com.google.common.primitives.Bytes;
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
import org.mvel2.ExecutionContext;
|
import org.mvel2.ExecutionContext;
|
||||||
import org.mvel2.ParserConfiguration;
|
import org.mvel2.ParserConfiguration;
|
||||||
import org.mvel2.execution.ExecutionArrayList;
|
import org.mvel2.execution.ExecutionArrayList;
|
||||||
@ -31,6 +32,7 @@ import java.nio.ByteBuffer;
|
|||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -90,6 +92,14 @@ public class TbUtils {
|
|||||||
List.class, int.class, boolean.class)));
|
List.class, int.class, boolean.class)));
|
||||||
parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat",
|
parserConfig.addImport("parseBytesToFloat", new MethodStub(TbUtils.class.getMethod("parseBytesToFloat",
|
||||||
List.class, int.class)));
|
List.class, int.class)));
|
||||||
|
parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble",
|
||||||
|
byte[].class, int.class, boolean.class)));
|
||||||
|
parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble",
|
||||||
|
byte[].class, int.class)));
|
||||||
|
parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble",
|
||||||
|
List.class, int.class, boolean.class)));
|
||||||
|
parserConfig.addImport("parseBytesToDouble", new MethodStub(TbUtils.class.getMethod("parseBytesToDouble",
|
||||||
|
List.class, int.class)));
|
||||||
parserConfig.addImport("toFixed", new MethodStub(TbUtils.class.getMethod("toFixed",
|
parserConfig.addImport("toFixed", new MethodStub(TbUtils.class.getMethod("toFixed",
|
||||||
double.class, int.class)));
|
double.class, int.class)));
|
||||||
parserConfig.addImport("hexToBytes", new MethodStub(TbUtils.class.getMethod("hexToBytes",
|
parserConfig.addImport("hexToBytes", new MethodStub(TbUtils.class.getMethod("hexToBytes",
|
||||||
@ -329,6 +339,39 @@ public class TbUtils {
|
|||||||
return parseBytesToFloat(Bytes.toArray(data), offset, bigEndian);
|
return parseBytesToFloat(Bytes.toArray(data), offset, bigEndian);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static double parseBytesToDouble(byte[] data, int offset) {
|
||||||
|
return parseBytesToDouble(data, offset, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double parseBytesToDouble(byte[] data, int offset, boolean bigEndian) {
|
||||||
|
if (data != null && data.length > 0) {
|
||||||
|
int length = 8;
|
||||||
|
if (offset > data.length) {
|
||||||
|
throw new IllegalArgumentException("Offset: " + offset + " is out of bounds for array with length: " + data.length + "!");
|
||||||
|
}
|
||||||
|
if ((offset + length) > data.length) {
|
||||||
|
throw new IllegalArgumentException("Default length is always 4 bytes. Offset: " + offset + " and Length: " + length + " is out of bounds for array with length: " + data.length + "!");
|
||||||
|
}
|
||||||
|
|
||||||
|
byte[] dataBytesArray = Arrays.copyOfRange(data, offset, (offset+length));
|
||||||
|
if (!bigEndian) {
|
||||||
|
ArrayUtils.reverse(dataBytesArray);
|
||||||
|
}
|
||||||
|
return ByteBuffer.wrap(dataBytesArray).getDouble();
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Array is null or array length is 0!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double parseBytesToDouble(List data, int offset) {
|
||||||
|
return parseBytesToDouble(data, offset, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double parseBytesToDouble(List data, int offset, boolean bigEndian) {
|
||||||
|
return parseBytesToDouble(Bytes.toArray(data), offset, bigEndian);
|
||||||
|
}
|
||||||
|
|
||||||
public static String bytesToHex(ExecutionArrayList<?> bytesList) {
|
public static String bytesToHex(ExecutionArrayList<?> bytesList) {
|
||||||
byte[] bytes = new byte[bytesList.size()];
|
byte[] bytes = new byte[bytesList.size()];
|
||||||
for (int i = 0; i < bytesList.size(); i++) {
|
for (int i = 0; i < bytesList.size(); i++) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user