Skip to content

Commit

Permalink
Merge pull request #202 from wes-johnson/develop
Browse files Browse the repository at this point in the history
Fixes #131 for the Java client library
  • Loading branch information
wes-johnson authored Jun 9, 2022
2 parents 5fd59a7 + ab4b900 commit fa3f07b
Show file tree
Hide file tree
Showing 3 changed files with 189 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@

package org.eclipse.tahu;

import static org.eclipse.tahu.message.model.MetricDataType.*;
import static org.eclipse.tahu.message.model.MetricDataType.Boolean;
import static org.eclipse.tahu.message.model.MetricDataType.DataSet;
import static org.eclipse.tahu.message.model.MetricDataType.DateTime;
import static org.eclipse.tahu.message.model.MetricDataType.Double;
import static org.eclipse.tahu.message.model.MetricDataType.Float;
import static org.eclipse.tahu.message.model.MetricDataType.Int16;
import static org.eclipse.tahu.message.model.MetricDataType.Int32;
import static org.eclipse.tahu.message.model.MetricDataType.Int64;
import static org.eclipse.tahu.message.model.MetricDataType.Int8;
import static org.eclipse.tahu.message.model.MetricDataType.String;
import static org.eclipse.tahu.message.model.MetricDataType.Template;
import static org.eclipse.tahu.message.model.MetricDataType.Text;
import static org.eclipse.tahu.message.model.MetricDataType.UInt16;
import static org.eclipse.tahu.message.model.MetricDataType.UInt32;
import static org.eclipse.tahu.message.model.MetricDataType.UInt64;
import static org.eclipse.tahu.message.model.MetricDataType.UInt8;
import static org.eclipse.tahu.message.model.MetricDataType.UUID;

import java.math.BigInteger;
import java.util.ArrayList;
Expand All @@ -24,6 +40,7 @@
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocketFactory;
Expand All @@ -35,17 +52,25 @@
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.tahu.SparkplugException;
import org.eclipse.tahu.SparkplugInvalidTypeException;
import org.eclipse.tahu.message.SparkplugBPayloadDecoder;
import org.eclipse.tahu.message.SparkplugBPayloadEncoder;
import org.eclipse.tahu.message.model.*;
import org.eclipse.tahu.message.model.DataSet;
import org.eclipse.tahu.message.model.DataSet.DataSetBuilder;
import org.eclipse.tahu.message.model.DataSetDataType;
import org.eclipse.tahu.message.model.Metric;
import org.eclipse.tahu.message.model.Metric.MetricBuilder;
import org.eclipse.tahu.message.model.Parameter;
import org.eclipse.tahu.message.model.ParameterDataType;
import org.eclipse.tahu.message.model.PropertyDataType;
import org.eclipse.tahu.message.model.PropertySet;
import org.eclipse.tahu.message.model.PropertySet.PropertySetBuilder;
import org.eclipse.tahu.message.model.PropertyValue;
import org.eclipse.tahu.message.model.Row.RowBuilder;
import org.eclipse.tahu.message.model.SparkplugBPayload;
import org.eclipse.tahu.message.model.SparkplugBPayload.SparkplugBPayloadBuilder;
import org.eclipse.tahu.message.model.Template;
import org.eclipse.tahu.message.model.Template.TemplateBuilder;
import org.eclipse.tahu.message.model.Value;
import org.eclipse.tahu.util.CompressionAlgorithm;
import org.eclipse.tahu.util.PayloadUtil;

Expand All @@ -68,8 +93,7 @@ public class SparkplugExample implements MqttCallbackExtended {
private String groupId = "Sparkplug B Devices";
private String edgeNode = "Java Sparkplug B Example";
private String deviceId = "SparkplugBExample";
private String clientId = null;
// private String clientId = "SparkplugBExampleEdgeNode";
private String clientId = "SparkplugBExampleEdgeNode";
private String username = "admin";
private String password = "changeme";
private long PUBLISH_PERIOD = 60000; // Publish period in milliseconds
Expand Down Expand Up @@ -213,16 +237,23 @@ private void publishDeviceBirth() {

payload.addMetric(new MetricBuilder("Device Control/Rebirth", Boolean, false).createMetric());

// Only do this once to set up the inputs and outputs
payload.addMetric(new MetricBuilder("Inputs/0", Boolean, true).createMetric());
payload.addMetric(new MetricBuilder("Inputs/1", Int32, 0).createMetric());
payload.addMetric(new MetricBuilder("Inputs/2", Double, 1.23d).createMetric());
payload.addMetric(new MetricBuilder("Outputs/0", Boolean, true).createMetric());
payload.addMetric(new MetricBuilder("Outputs/1", Int32, 0).createMetric());
payload.addMetric(new MetricBuilder("Outputs/2", Double, 1.23d).createMetric());

// payload.addMetric(new MetricBuilder("New_1", Int32, 0).createMetric());
// payload.addMetric(new MetricBuilder("New_2", Double, 1.23d).createMetric());
payload.addMetric(new MetricBuilder("Int8_Min", Int8, (byte) -128).createMetric());
payload.addMetric(new MetricBuilder("Int8_Max", Int8, (byte) 127).createMetric());
payload.addMetric(new MetricBuilder("Int16_Min", Int16, (short) -32768).createMetric());
payload.addMetric(new MetricBuilder("Int16_Max", Int16, (short) 32767).createMetric());
payload.addMetric(new MetricBuilder("Int32_Min", Int32, -2147483648).createMetric());
payload.addMetric(new MetricBuilder("Int32_Max", Int32, 2147483647).createMetric());
payload.addMetric(new MetricBuilder("Int64_Min", Int64, -9223372036854775808L).createMetric());
payload.addMetric(new MetricBuilder("Int64_Max", Int64, 9223372036854775807L).createMetric());
payload.addMetric(new MetricBuilder("UInt8_Min", UInt8, (short) 0).createMetric());
payload.addMetric(new MetricBuilder("UInt8_Max", UInt8, (short) 255).createMetric());
payload.addMetric(new MetricBuilder("UInt16_Min", UInt16, 0).createMetric());
payload.addMetric(new MetricBuilder("UInt16_Max", UInt16, 64535).createMetric());
payload.addMetric(new MetricBuilder("UInt32_Min", UInt32, 0L).createMetric());
payload.addMetric(new MetricBuilder("UInt32_Max", UInt32, 4294967295L).createMetric());
payload.addMetric(new MetricBuilder("UInt64_Min", UInt64, BigInteger.valueOf(0L)).createMetric());
payload.addMetric(
new MetricBuilder("UInt64_Max", UInt64, new BigInteger("18446744073709551615")).createMetric());

// Add some properties
payload.addMetric(new MetricBuilder("Properties/hw_version", String, HW_VERSION).createMetric());
Expand Down Expand Up @@ -359,10 +390,10 @@ private List<Metric> newMetrics(boolean isBirth) throws SparkplugException {
metrics.add(new MetricBuilder("Int16", Int16, (short) random.nextInt()).createMetric());
metrics.add(new MetricBuilder("Int32", Int32, random.nextInt()).createMetric());
metrics.add(new MetricBuilder("Int64", Int64, random.nextLong()).createMetric());
metrics.add(new MetricBuilder("UInt8", UInt8, (short) random.nextInt()).createMetric());
metrics.add(new MetricBuilder("UInt16", UInt16, random.nextInt()).createMetric());
metrics.add(new MetricBuilder("UInt32", UInt32, random.nextLong()).createMetric());
metrics.add(new MetricBuilder("UInt64", UInt64, BigInteger.valueOf(random.nextLong())).createMetric());
metrics.add(new MetricBuilder("UInt8", UInt8, getRandomUInt8()).createMetric());
metrics.add(new MetricBuilder("UInt16", UInt16, getRandomUInt16()).createMetric());
metrics.add(new MetricBuilder("UInt32", UInt32, getRandomUInt32()).createMetric());
metrics.add(new MetricBuilder("UInt64", UInt64, getRandomUInt64()).createMetric());
metrics.add(new MetricBuilder("Float", Float, random.nextFloat()).createMetric());
metrics.add(new MetricBuilder("Double", Double, random.nextDouble()).createMetric());
metrics.add(new MetricBuilder("Boolean", Boolean, random.nextBoolean()).createMetric());
Expand Down Expand Up @@ -417,10 +448,10 @@ private Map<String, PropertyValue> newProps(boolean withPropTypes) throws Sparkp
propMap.put("PropInt16", new PropertyValue(PropertyDataType.Int16, (short) random.nextInt()));
propMap.put("PropInt32", new PropertyValue(PropertyDataType.Int32, random.nextInt()));
propMap.put("PropInt64", new PropertyValue(PropertyDataType.Int64, random.nextLong()));
propMap.put("PropUInt8", new PropertyValue(PropertyDataType.UInt8, (short) random.nextInt()));
propMap.put("PropUInt16", new PropertyValue(PropertyDataType.UInt16, random.nextInt()));
propMap.put("PropUInt32", new PropertyValue(PropertyDataType.UInt32, random.nextLong()));
propMap.put("PropUInt64", new PropertyValue(PropertyDataType.UInt64, BigInteger.valueOf(random.nextLong())));
propMap.put("PropUInt8", new PropertyValue(PropertyDataType.UInt8, getRandomUInt8()));
propMap.put("PropUInt16", new PropertyValue(PropertyDataType.UInt16, getRandomUInt16()));
propMap.put("PropUInt32", new PropertyValue(PropertyDataType.UInt32, getRandomUInt32()));
propMap.put("PropUInt64", new PropertyValue(PropertyDataType.UInt64, getRandomUInt64()));
propMap.put("PropFloat", new PropertyValue(PropertyDataType.Float, random.nextFloat()));
propMap.put("PropDouble", new PropertyValue(PropertyDataType.Double, random.nextDouble()));
propMap.put("PropBoolean", new PropertyValue(PropertyDataType.Boolean, random.nextBoolean()));
Expand Down Expand Up @@ -491,10 +522,10 @@ private Template newTemplate(boolean isDef, String templatRef) throws SparkplugE
metrics.add(new MetricBuilder("MyInt16", Int16, (short) random.nextInt()).createMetric());
metrics.add(new MetricBuilder("MyInt32", Int32, random.nextInt()).createMetric());
metrics.add(new MetricBuilder("MyInt64", Int64, random.nextLong()).createMetric());
metrics.add(new MetricBuilder("MyUInt8", UInt8, (short) random.nextInt()).createMetric());
metrics.add(new MetricBuilder("MyUInt16", UInt16, random.nextInt()).createMetric());
metrics.add(new MetricBuilder("MyUInt32", UInt32, random.nextLong()).createMetric());
metrics.add(new MetricBuilder("MyUInt64", UInt64, BigInteger.valueOf(random.nextLong())).createMetric());
metrics.add(new MetricBuilder("MyUInt8", UInt8, getRandomUInt8()).createMetric());
metrics.add(new MetricBuilder("MyUInt16", UInt16, getRandomUInt16()).createMetric());
metrics.add(new MetricBuilder("MyUInt32", UInt32, getRandomUInt32()).createMetric());
metrics.add(new MetricBuilder("MyUInt64", UInt64, getRandomUInt64()).createMetric());
metrics.add(new MetricBuilder("MyFloat", Float, random.nextFloat()).createMetric());
metrics.add(new MetricBuilder("MyDouble", Double, random.nextDouble()).createMetric());
metrics.add(new MetricBuilder("MyBoolean", Boolean, random.nextBoolean()).createMetric());
Expand Down Expand Up @@ -522,10 +553,10 @@ private DataSet newDataSet() throws SparkplugException {
.addValue(new Value<Short>(DataSetDataType.Int16, (short) random.nextInt()))
.addValue(new Value<Integer>(DataSetDataType.Int32, random.nextInt()))
.addValue(new Value<Long>(DataSetDataType.Int64, random.nextLong()))
.addValue(new Value<Short>(DataSetDataType.UInt8, (short) random.nextInt()))
.addValue(new Value<Integer>(DataSetDataType.UInt16, random.nextInt()))
.addValue(new Value<Long>(DataSetDataType.UInt32, random.nextLong()))
.addValue(new Value<BigInteger>(DataSetDataType.UInt64, BigInteger.valueOf(random.nextLong())))
.addValue(new Value<Short>(DataSetDataType.UInt8, getRandomUInt8()))
.addValue(new Value<Integer>(DataSetDataType.UInt16, getRandomUInt16()))
.addValue(new Value<Long>(DataSetDataType.UInt32, getRandomUInt32()))
.addValue(new Value<BigInteger>(DataSetDataType.UInt64, getRandomUInt64()))
.addValue(new Value<Float>(DataSetDataType.Float, random.nextFloat()))
.addValue(new Value<Double>(DataSetDataType.Double, random.nextDouble()))
.addValue(new Value<Boolean>(DataSetDataType.Boolean, random.nextBoolean()))
Expand All @@ -536,10 +567,10 @@ private DataSet newDataSet() throws SparkplugException {
.addValue(new Value<Short>(DataSetDataType.Int16, (short) random.nextInt()))
.addValue(new Value<Integer>(DataSetDataType.Int32, random.nextInt()))
.addValue(new Value<Long>(DataSetDataType.Int64, random.nextLong()))
.addValue(new Value<Short>(DataSetDataType.UInt8, (short) random.nextInt()))
.addValue(new Value<Integer>(DataSetDataType.UInt16, random.nextInt()))
.addValue(new Value<Long>(DataSetDataType.UInt32, random.nextLong()))
.addValue(new Value<BigInteger>(DataSetDataType.UInt64, BigInteger.valueOf(random.nextLong())))
.addValue(new Value<Short>(DataSetDataType.UInt8, getRandomUInt8()))
.addValue(new Value<Integer>(DataSetDataType.UInt16, getRandomUInt16()))
.addValue(new Value<Long>(DataSetDataType.UInt32, getRandomUInt32()))
.addValue(new Value<BigInteger>(DataSetDataType.UInt64, getRandomUInt64()))
.addValue(new Value<Float>(DataSetDataType.Float, random.nextFloat()))
.addValue(new Value<Double>(DataSetDataType.Double, random.nextDouble()))
.addValue(new Value<Boolean>(DataSetDataType.Boolean, random.nextBoolean()))
Expand All @@ -549,6 +580,33 @@ private DataSet newDataSet() throws SparkplugException {
.createDataSet();
}

private short getRandomUInt8() {
Random random = new Random();
return (short) random.nextInt(256);
}

private int getRandomUInt16() {
Random random = new Random();
return random.nextInt(65536);

}

private long getRandomUInt32() {
return ThreadLocalRandom.current().nextLong(4294967296L);
}

private BigInteger getRandomUInt64() {
Random random = new Random();
BigInteger minSize = new BigInteger("0");
BigInteger maxSize = new BigInteger("18446744073709551616");
BigInteger randomResult = new BigInteger(64, random);
while (randomResult.compareTo(minSize) <= 0 || randomResult.compareTo(maxSize) >= 0) {
randomResult = new BigInteger(64, random);
System.out.println("New randomResult: " + randomResult);
}
return randomResult;
}

private class Publisher implements Runnable {

private String topic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,17 @@ private Object getPropertyValue(SparkplugBProto.Payload.PropertyValue value) thr
case UInt16:
return value.getIntValue();
case UInt32:
if (value.hasIntValue()) {
return Integer.toUnsignedLong(value.getIntValue());
} else if (value.hasLongValue()) {
return value.getLongValue();
} else {
logger.error("Invalid value for UInt32 datatype");
}
case Int64:
return value.getLongValue();
case UInt64:
return BigInteger.valueOf(value.getLongValue());
return new BigInteger(Long.toUnsignedString(value.getLongValue()));
case String:
case Text:
return value.getStringValue();
Expand Down Expand Up @@ -210,10 +217,17 @@ private Object getMetricValue(SparkplugBProto.Payload.Metric protoMetric) throws
case UInt16:
return protoMetric.getIntValue();
case UInt32:
if (protoMetric.hasIntValue()) {
return Integer.toUnsignedLong(protoMetric.getIntValue());
} else if (protoMetric.hasLongValue()) {
return protoMetric.getLongValue();
} else {
logger.error("Invalid value for UInt32 datatype");
}
case Int64:
return protoMetric.getLongValue();
case UInt64:
return BigInteger.valueOf(protoMetric.getLongValue());
return new BigInteger(Long.toUnsignedString(protoMetric.getLongValue()));
case String:
case Text:
case UUID:
Expand Down Expand Up @@ -396,10 +410,17 @@ private Object getParameterValue(SparkplugBProto.Payload.Template.Parameter prot
case UInt16:
return protoParameter.getIntValue();
case UInt32:
if (protoParameter.hasIntValue()) {
return Integer.toUnsignedLong(protoParameter.getIntValue());
} else if (protoParameter.hasLongValue()) {
return protoParameter.getLongValue();
} else {
logger.error("Invalid value for UInt32 datatype");
}
case Int64:
return protoParameter.getLongValue();
case UInt64:
return BigInteger.valueOf(protoParameter.getLongValue());
return new BigInteger(Long.toUnsignedString(protoParameter.getLongValue()));
case String:
case Text:
return protoParameter.getStringValue();
Expand Down Expand Up @@ -463,6 +484,13 @@ private Value<?> convertDataSetValue(int protoType, SparkplugBProto.Payload.Data
return new Value<Integer>(type, null);
}
case UInt32:
if (protoValue.hasIntValue()) {
return new Value<Long>(type, Integer.toUnsignedLong(protoValue.getIntValue()));
} else if (protoValue.hasLongValue()) {
return new Value<Long>(type, protoValue.getLongValue());
} else {
return new Value<Long>(type, null);
}
case Int64:
if (protoValue.hasLongValue()) {
return new Value<Long>(type, protoValue.getLongValue());
Expand All @@ -471,7 +499,8 @@ private Value<?> convertDataSetValue(int protoType, SparkplugBProto.Payload.Data
}
case UInt64:
if (protoValue.hasLongValue()) {
return new Value<BigInteger>(type, BigInteger.valueOf(protoValue.getLongValue()));
return new Value<BigInteger>(type,
new BigInteger(Long.toUnsignedString(protoValue.getLongValue())));
} else {
return new Value<BigInteger>(type, null);
}
Expand Down
Loading

0 comments on commit fa3f07b

Please sign in to comment.