Skip to content

Commit

Permalink
Set default scale and offset value to Optional.empty()
Browse files Browse the repository at this point in the history
  • Loading branch information
salvatore-coppola committed Nov 25, 2024
1 parent af10940 commit 04d7a6f
Show file tree
Hide file tree
Showing 24 changed files with 142 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Bundle-License: Eclipse Public License v2.0
Bundle-Category: Asset-Driver Management
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.kura;version="[1.2,2.0)",
org.eclipse.kura.channel;version="[1.0,2.0)",
org.eclipse.kura.channel;version="[2.0,3.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.0,2.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bundle-SymbolicName: org.eclipse.kura.wire.devel.component.provider
Bundle-Version: 2.0.0.qualifier
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.kura;version="[1.0,2.0)",
org.eclipse.kura.channel;version="[1.0,2.0)",
org.eclipse.kura.channel;version="[2.0,3.0)",
org.eclipse.kura.channel.listener;version="[1.0,2.0)",
org.eclipse.kura.configuration;version="[1.0,2.0)",
org.eclipse.kura.configuration.metatype;version="[1.0,2.0)",
Expand Down
2 changes: 1 addition & 1 deletion 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.3.0",
org.eclipse.kura.channel;version="2.0.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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.kura.annotation.NotThreadSafe;
import org.eclipse.kura.type.DataType;
Expand Down Expand Up @@ -66,12 +67,12 @@ public class Channel {
/*
* The value used to scale the value
*/
private Number valueScale = 1.0d;
private Optional<Number> valueScale = Optional.empty();

/**
* The value used as offset of the value
*/
private Number valueOffset = 0.0d;
private Optional<Number> valueOffset = Optional.empty();

private String unit = "";

Expand All @@ -80,37 +81,6 @@ public class Channel {
*/
private boolean isEnabled = true;

/**
* Instantiates a new channel.
*
* @param name
* the name for this channel
* @param type
* the type
* @param valueType
* the value type
* @param config
* the configuration
* @throws NullPointerException
* if any of the arguments is null
* @deprecated Use {@link #Channel(String, ChannelType, DataType, ScaleOffsetType, Number, Number, Map)}
*/

@Deprecated
public Channel(final String name, final ChannelType type, final DataType valueType,
final Map<String, Object> config) {

requireNonNull(name, MESSAGE_CHANNEL_NAME_CANNOT_BE_NULL);
requireNonNull(type, MESSAGE_CHANNEL_TYPE_CANNOT_BE_NULL);
requireNonNull(valueType, MESSAGE_CHANNEL_VALUE_TYPE_CANNOT_BE_NULL);
requireNonNull(config, MESSAGE_CHANNEL_CONFIGURATION_CANNOT_BE_NULL);

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

/**
* Instantiates a new channel.
*
Expand All @@ -121,30 +91,28 @@ public Channel(final String name, final ChannelType type, final DataType valueTy
* @param valueType
* the value type
* @param valueScale
* the value used to scale the value, must have the same {@link DataType} as valueOffset
* an optional 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
* an optional 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 2.8
* @since 3.0
*/
public Channel(final String name, final ChannelType type, final DataType valueType,
final ScaleOffsetType scaleOffsetType, final Number valueScale, final Number valueOffset,
final Map<String, Object> config) {
final ScaleOffsetType scaleOffsetType, final Optional<Number> valueScale,
final Optional<Number> valueOffset, final Map<String, Object> config) {

requireNonNull(name, MESSAGE_CHANNEL_NAME_CANNOT_BE_NULL);
requireNonNull(type, MESSAGE_CHANNEL_TYPE_CANNOT_BE_NULL);
requireNonNull(valueType, MESSAGE_CHANNEL_VALUE_TYPE_CANNOT_BE_NULL);
requireNonNull(config, MESSAGE_CHANNEL_CONFIGURATION_CANNOT_BE_NULL);

requireNonNull(scaleOffsetType, "Scale/Offset type cannot be null");
requireNonNull(valueScale, "Channel value scale cannot be null");
requireNonNull(valueOffset, "Channel value offset cannot be null");

this.configuration = Collections.unmodifiableMap(config);
this.name = name;
Expand Down Expand Up @@ -215,55 +183,27 @@ public boolean isEnabled() {
return this.isEnabled;
}

/**
* Returns a double that represents the scale factor to be applied to the read value
*
* @return a double that represents the scale factor to be applied to the read value
*
* @since 2.3
*
* @deprecated Use {@link #getValueScaleAsNumber()}
*/
@Deprecated
public double getValueScale() {
return this.valueScale.doubleValue();
}

/**
* Returns a {@link Number} that represents the scale factor to be applied to the read
* value
*
* @return a {@link Number} that represents the scale factor to be applied to the read value
*
* @since 2.8
* @since 3.0
*/
public Number getValueScaleAsNumber() {
public Optional<Number> getValueScaleAsNumber() {
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
*
* @since 2.3
*
* @deprecated Use {@link #getValueOffsetAsNumber()}
*/
@Deprecated
public double getValueOffset() {
return this.valueOffset.doubleValue();
}

/**
* 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 2.8
* @since 3.0
*/
public Number getValueOffsetAsNumber() {
public Optional<Number> getValueOffsetAsNumber() {
return this.valueOffset;
}

Expand Down Expand Up @@ -343,43 +283,21 @@ public void setEnabled(boolean isEnabled) {
* Specifies the scale to be applied to the channel value
*
* @param scale
* a double value that specifies the scale to be applied to the channel value
* @since 2.3
* an optional {@link Number} value that specifies the scale to be applied to the channel value
* @since 3.0
*/
public void setScale(double scale) {
public void setScale(Optional<Number> scale) {
this.valueScale = scale;
}

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

/**
* Specifies the offset to be applied to the channel value
*
* @param offset
* a double value that specifies the offset to be applied to the channel value
* @since 2.3
*/
public void setOffset(double offset) {
this.valueOffset = offset;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ public enum ScaleOffsetType {

DEFINED_BY_VALUE_TYPE,

/**
* @since 3.0
*/
FLOAT,
DOUBLE,

/**
* @since 3.0
*/
INTEGER,
LONG;

/**
Expand All @@ -41,10 +48,18 @@ public static ScaleOffsetType getScaleOffsetType(String stringScaleOffsetType) {
return DEFINED_BY_VALUE_TYPE;
}

if (FLOAT.name().equalsIgnoreCase(stringScaleOffsetType)) {
return FLOAT;
}

if (DOUBLE.name().equalsIgnoreCase(stringScaleOffsetType)) {
return DOUBLE;
}

if (INTEGER.name().equalsIgnoreCase(stringScaleOffsetType)) {
return INTEGER;
}

if (LONG.name().equalsIgnoreCase(stringScaleOffsetType)) {
return LONG;
}
Expand Down
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: com.eclipsesource.json;version="0.9.5",
org.eclipse.kura;version="[1.3,2.0)",
org.eclipse.kura.asset;version="[1.0,2.0)",
org.eclipse.kura.channel;version="[1.0,2.0)",
org.eclipse.kura.channel;version="[2.0,3.0)",
org.eclipse.kura.cloud;version="[1.1,1.2)",
org.eclipse.kura.cloudconnection.message;version="[1.0,2.0)",
org.eclipse.kura.cloudconnection.request;version="[1.0,1.1)",
Expand Down
2 changes: 1 addition & 1 deletion 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.2,2.0)",
org.eclipse.kura.channel;version="[2.0,3.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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -424,8 +425,8 @@ private boolean shouldApplyScaleAndOffset(final ChannelRecord channelRecord, fin

@SuppressWarnings("unchecked")
private void applyScaleAndOffset(final ChannelRecord channelRecord, final Channel channel) {
final Number channelScale = channel.getValueScaleAsNumber();
final Number channelOffset = channel.getValueOffsetAsNumber();
final Optional<Number> channelScale = channel.getValueScaleAsNumber();
final Optional<Number> channelOffset = channel.getValueOffsetAsNumber();

switch (channel.getValueType()) {
case DOUBLE:
Expand Down Expand Up @@ -457,22 +458,26 @@ private void applyScaleAndOffset(final ChannelRecord channelRecord, final Channe

// legacy method
private TypedValue<? extends Number> calculateScaleAndOffsetByTypedValue(TypedValue<? extends Number> typedValue,
Number scale, Number offset) {
Optional<Number> scale, Optional<Number> offset) {

Number result;

switch (typedValue.getType()) {
case DOUBLE:
result = (double) typedValue.getValue() * scale.doubleValue() + offset.doubleValue();
break;
case FLOAT:
result = (float) typedValue.getValue() * scale.floatValue() + offset.floatValue();
result = (float) typedValue.getValue() * scale.map(Number::floatValue).orElse(1f)
+ offset.map(Number::floatValue).orElse(0f);
break;
case DOUBLE:
result = (double) typedValue.getValue() * scale.map(Number::doubleValue).orElse(1d)
+ offset.map(Number::doubleValue).orElse(0d);
break;
case INTEGER:
result = (int) typedValue.getValue() * scale.intValue() + offset.intValue();
result = (int) typedValue.getValue() * scale.map(Number::intValue).orElse(1)
+ offset.map(Number::intValue).orElse(0);
break;
case LONG:
result = (long) typedValue.getValue() * scale.longValue() + offset.longValue();
result = (long) typedValue.getValue() * scale.map(Number::longValue).orElse(1L)
+ offset.map(Number::longValue).orElse(0L);
break;
default:
throw new IllegalArgumentException("Unknown value type" + typedValue.getType());
Expand All @@ -483,16 +488,27 @@ private TypedValue<? extends Number> calculateScaleAndOffsetByTypedValue(TypedVa
}

private TypedValue<? extends Number> calculateScaleAndOffset(DataType outputValueType,
TypedValue<? extends Number> typedValue, ScaleOffsetType scaleOffsetType, Number scale, Number offset) {
TypedValue<? extends Number> typedValue, ScaleOffsetType scaleOffsetType, Optional<Number> scale,
Optional<Number> offset) {

Number result = null;

switch (scaleOffsetType) {
case FLOAT:
result = scale.map(Number::floatValue).orElse(1f) * typedValue.getValue().floatValue()
+ offset.map(Number::floatValue).orElse(0f);
break;
case DOUBLE:
result = scale.doubleValue() * typedValue.getValue().doubleValue() + offset.doubleValue();
result = scale.map(Number::doubleValue).orElse(1d) * typedValue.getValue().doubleValue()
+ offset.map(Number::doubleValue).orElse(0d);
break;
case INTEGER:
result = scale.map(Number::intValue).orElse(1) * typedValue.getValue().intValue()
+ offset.map(Number::intValue).orElse(0);
break;
case LONG:
result = scale.longValue() * typedValue.getValue().longValue() + offset.longValue();
result = scale.map(Number::longValue).orElse(1L) * typedValue.getValue().longValue()
+ offset.map(Number::longValue).orElse(0L);
break;
default:
throw new IllegalArgumentException("Invalid scale/offset type");
Expand Down
Loading

0 comments on commit 04d7a6f

Please sign in to comment.