Skip to content

Commit

Permalink
Added new field for Channel, scaleoffset.type which assign a type (Do…
Browse files Browse the repository at this point in the history
…uble, Float, Integer or Long) to scale and offset value used in the channel.
  • Loading branch information
salvatore-coppola committed Sep 26, 2024
1 parent 9bac3e8 commit 4f43831
Show file tree
Hide file tree
Showing 14 changed files with 439 additions and 143 deletions.
4 changes: 2 additions & 2 deletions kura/org.eclipse.kura.api/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Export-Package: org.eclipse.kura;version="1.7.0",
org.eclipse.kura.bluetooth.le.beacon.listener;version="1.0.0",
org.eclipse.kura.certificate;version="2.1.0",
org.eclipse.kura.certificate.enrollment;version="1.0.0",
org.eclipse.kura.channel;version="1.2.0",
org.eclipse.kura.channel;version="1.3.0",
org.eclipse.kura.channel.listener;version="1.0.0",
org.eclipse.kura.clock;version="1.0.1",
org.eclipse.kura.cloud;version="1.1.0",
Expand Down Expand Up @@ -77,7 +77,7 @@ Export-Package: org.eclipse.kura;version="1.7.0",
org.eclipse.kura.ssl;version="2.1.0",
org.eclipse.kura.status;version="1.0.2",
org.eclipse.kura.system;version="1.7.0",
org.eclipse.kura.type;version="1.1.0",
org.eclipse.kura.type;version="1.2.0",
org.eclipse.kura.usb;version="1.3.0",
org.eclipse.kura.watchdog;version="1.0.2",
org.eclipse.kura.wire;version="2.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2021 Eurotech and/or its affiliates and others
* Copyright (c) 2017, 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -21,6 +21,7 @@
import org.eclipse.kura.annotation.NotThreadSafe;
import org.eclipse.kura.type.DataType;
import org.eclipse.kura.type.TypedValue;
import org.eclipse.kura.type.TypedValues;
import org.osgi.annotation.versioning.ProviderType;

/**
Expand Down Expand Up @@ -49,9 +50,15 @@ public class Channel {
*/
private DataType valueType;

private double valueScale;
/*
* The value used to scale the value
*/
private TypedValue<? extends Number> valueScale;

private double valueOffset;
/**
* The value used as offset of the value
*/
private TypedValue<? extends Number> valueOffset;

private String unit;

Expand Down Expand Up @@ -85,8 +92,59 @@ public Channel(final String name, final ChannelType type, final DataType valueTy
this.name = name;
this.type = type;
this.valueType = valueType;
this.valueScale = 1.0d;
this.valueOffset = 0d;

this.valueScale = TypedValues.newDoubleValue(1.0d);
this.valueOffset = TypedValues.newDoubleValue(0.0d);

this.unit = "";
}

/**
* Instantiates a new channel.
*
* @param name
* the name for this channel
* @param type
* the type
* @param valueType
* the value type
* @param valueScale
* the value used to scale the value, must have the same {@link DataType} as valueOffset
* @param valueOffset
* the value used as offset of the value, must have the same {@link DataType} as valueScale
* @param config
* the configuration
* @throws NullPointerException
* if any of the arguments is null
* @throws IllegalArgumentException
* if any of the valueScale and valueOffset have different types
*
* @since 3.0
*/
public Channel(final String name, final ChannelType type, final DataType valueType,
final TypedValue<? extends Number> valueScale, final TypedValue<? extends Number> valueOffset,
final Map<String, Object> config) {
requireNonNull(name, "Channel name cannot be null");
requireNonNull(type, "Channel type cannot be null");
requireNonNull(valueType, "Channel value type cannot be null");

requireNonNull(valueScale, "Channel value scale cannot be null");
requireNonNull(valueOffset, "Channel value offset cannot be null");

requireNonNull(config, "Channel configuration cannot be null");

this.configuration = Collections.unmodifiableMap(config);
this.name = name;
this.type = type;
this.valueType = valueType;

if (valueScale.getType() != valueOffset.getType()) {
throw new IllegalArgumentException("Channel value scale and offset must have the same type");
}

this.valueScale = valueScale;
this.valueOffset = valueOffset;

this.unit = "";
}

Expand Down Expand Up @@ -142,20 +200,62 @@ public boolean isEnabled() {
*
* @return a double that represents the scale factor to be applied to the read value
*
* @throws IllegalArgumentException
*
* @since 2.3
*
* @deprecated Use {@link #getValueScaleAsTypedValue()}
*/
@Deprecated
public double getValueScale() {
if (this.valueScale.getType() != DataType.DOUBLE) {
throw new IllegalStateException(
"the type of the scale is " + this.valueScale.getType().toString() + ".Expected a double");
}
return (Double) this.valueScale.getValue();
}

/**
* Returns a {@link TypedValue} that represents the scale factor to be applied to the read
* value
*
* @return a {@link TypedValue} that represents the scale factor to be applied to the read value
*
* @since 3.0
*/
public TypedValue<? extends Number> getValueScaleAsTypedValue() {
return this.valueScale;
}

/**
* Returns a double that represents the offset to be applied to the read value
*
* @return a double that represents the offset to be applied to the read value
*
* @throws IllegalArgumentException
*
* @since 2.3
*
* @deprecated Use {@link #getValueOffsetAsTypedValue()}
*/
@Deprecated
public double getValueOffset() {
if (this.valueOffset.getType() != DataType.DOUBLE) {
throw new IllegalStateException(
"the type of the offset is " + this.valueOffset.getType().toString() + ".Expected a double");
}
return (Double) this.valueOffset.getValue();
}

/**
* Returns a {@link TypedValue} that represents the offset factor to be applied to the read
* value
*
* @return a {@link TypedValue} that represents the offset factor to be applied to the read value
*
* @since 3.0
*/
public TypedValue<? extends Number> getValueOffsetAsTypedValue() {
return this.valueOffset;
}

Expand Down Expand Up @@ -224,6 +324,17 @@ public void setEnabled(boolean isEnabled) {
* @since 2.3
*/
public void setScale(double scale) {
this.valueScale = TypedValues.newDoubleValue(scale);
}

/**
* Specifies the scale to be applied to the channel value
*
* @param scale
* a {@link TypedValue} value that specifies the scale to be applied to the channel value
* @since 3.0
*/
public void setScale(TypedValue<? extends Number> scale) {
this.valueScale = scale;
}

Expand All @@ -235,6 +346,17 @@ public void setScale(double scale) {
* @since 2.3
*/
public void setOffset(double offset) {
this.valueOffset = TypedValues.newDoubleValue(offset);
}

/**
* Specifies the offset to be applied to the channel value
*
* @param offset
* a {@link TypedValue} value that specifies the offset to be applied to the channel value
* @since 3.0
*/
public void setOffset(TypedValue<? extends Number> offset) {
this.valueOffset = offset;
}

Expand Down Expand Up @@ -301,9 +423,9 @@ public int hashCode() {
result = prime * result + (this.type == null ? 0 : this.type.hashCode());
result = prime * result + (this.unit == null ? 0 : this.unit.hashCode());
long temp;
temp = Double.doubleToLongBits(this.valueOffset);
temp = Double.doubleToLongBits((Double) this.valueOffset.getValue());
result = prime * result + (int) (temp ^ temp >>> 32);
temp = Double.doubleToLongBits(this.valueScale);
temp = Double.doubleToLongBits((Double) this.valueScale.getValue());
result = prime * result + (int) (temp ^ temp >>> 32);
result = prime * result + (this.valueType == null ? 0 : this.valueType.hashCode());
return result;
Expand Down Expand Up @@ -338,10 +460,10 @@ public boolean equals(Object obj) {
} else if (!this.unit.equals(other.unit)) {
return false;
}
if (Double.doubleToLongBits(this.valueOffset) != Double.doubleToLongBits(other.valueOffset)) {
if (!this.valueOffset.equals(other.valueOffset)) {
return false;
}
if (Double.doubleToLongBits(this.valueScale) != Double.doubleToLongBits(other.valueScale)) {
if (!this.valueScale.equals(other.valueScale)) {
return false;
}
if (this.valueType != other.valueType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2020 Eurotech and/or its affiliates and others
* Copyright (c) 2016, 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -69,4 +69,32 @@ public static DataType getDataType(String stringDataType) {

throw new IllegalArgumentException("Cannot convert to DataType");
}

/**
* Converts {@code stringDataType}, if possible, to the related numeric {@link DataType}.
*
* @param stringDataType
* String that we want to use to get the respective numeric {@link DataType}.
* @return a numeric DataType that corresponds to the String passed as argument.
* @throws IllegalArgumentException
* if the passed string does not correspond to an existing numeric {@link DataType}.
*
* @since 3.0
*/
public static DataType getNumericDataType(String stringDataType) {
if (INTEGER.name().equalsIgnoreCase(stringDataType)) {
return INTEGER;
}
if (FLOAT.name().equalsIgnoreCase(stringDataType)) {
return FLOAT;
}
if (DOUBLE.name().equalsIgnoreCase(stringDataType)) {
return DOUBLE;
}
if (LONG.name().equalsIgnoreCase(stringDataType)) {
return LONG;
}

throw new IllegalArgumentException("Cannot convert to Numeric DataType");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2020 Eurotech and/or its affiliates and others
* Copyright (c) 2016, 2024 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -144,12 +144,42 @@ public static TypedValue<?> newTypedValue(final Object value) {
}

/**
* Parses a TypedValue of given type from a String.
*
* Creates new numeric {@link TypedValue} of given type
*
* @param type
* the {@link DataType} of the returned {@link TypedValue}
* @param value
* the String to be parsed into a {@link TypedValue}
* a {@link Number} that needs to be represented as {@link TypedValue}
* @return a {@link TypedValue} that represents the conversion of {@code value}
* @throws IllegalArgumentException
* if {@code value} cannot be represented as a numeric {@link TypedValue}
*/
public static TypedValue<? extends Number> newNumericTypedValue(final DataType type, final Number value) {
Objects.requireNonNull(type, "type cannot be null");
Objects.requireNonNull(value, "value cannot be null");

switch (type) {
case DOUBLE:
return newDoubleValue(value.doubleValue());
case FLOAT:
return newFloatValue(value.floatValue());
case INTEGER:
return newIntegerValue(value.intValue());
case LONG:
return newLongValue(value.longValue());
default:
throw new IllegalArgumentException(value + " cannot be converted into a TypedValue of type " + type);
}

}

/**
* Parses a {@link TypedValue} of given type from a String.
*
* @param type
* the {@link DataType} of the returned {@link TypedValue}
* @param value
* the String to be parsed into a {@link TypedValue}
* @return a {@link TypedValue} that represents the conversion of {@code value}
* @throws IllegalArgumentException
* if {@code value} cannot be represented as {@link TypedValue}
Expand Down Expand Up @@ -178,4 +208,38 @@ public static TypedValue<?> parseTypedValue(final DataType type, final String va
}
throw new IllegalArgumentException(value + " cannot be converted into a TypedValue of type " + type);
}

/**
* Parses a Numeric {@link TypedValue} of given type from a String.
*
* @param type
* the {@link DataType} of the returned {@link TypedValue}
* @param value
* the String to be parsed into a {@link TypedValue} *
* @return a Numeric {@link TypedValue} that represents the conversion of {@code value}
* @throws IllegalArgumentException
* if {@code value} cannot be represented as a Numeric {@link TypedValue}
*
* @since 3.0
*/

public static TypedValue<? extends Number> parseNumericTypedValue(DataType type, String value) {
Objects.requireNonNull(type, "type cannot be null");
Objects.requireNonNull(value, "value cannot be null");

switch (type) {
case DOUBLE:
return newDoubleValue(Double.parseDouble(value));
case FLOAT:
return newFloatValue(Float.parseFloat(value));
case INTEGER:
return newIntegerValue(Integer.parseInt(value));
case LONG:
return newLongValue(Long.parseLong(value));
default:
throw new IllegalArgumentException(value + " cannot be converted into a TypedValue of type " + type);
}

}

}
4 changes: 2 additions & 2 deletions kura/org.eclipse.kura.asset.provider/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Import-Package: org.eclipse.kura;version="[1.2,2.0)",
org.eclipse.kura.annotation;version="[1.0,2.0)",
org.eclipse.kura.asset;version="[1.0,1.1)",
org.eclipse.kura.channel;version="[1.1,2.0)",
org.eclipse.kura.channel;version="[1.2,2.0)",
org.eclipse.kura.channel.listener;version="[1.0,1.1)",
org.eclipse.kura.configuration;version="[1.1,2.0)",
org.eclipse.kura.configuration.metatype;version="[1.1,2.0)",
Expand All @@ -26,6 +26,6 @@ Import-Package: org.eclipse.kura;version="[1.2,2.0)",
org.osgi.service.component;version="1.2.0",
org.osgi.util.tracker;version="1.5.0",
org.slf4j;version="1.6.4"
Export-Package: org.eclipse.kura.asset.provider;version="2.0.0"
Export-Package: org.eclipse.kura.asset.provider;version="2.1.0"
Service-Component: OSGI-INF/*.xml
Bundle-ActivationPolicy: lazy
Loading

0 comments on commit 4f43831

Please sign in to comment.