Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(asset.provider): Added integer and float as possible Scale and Offset type #5584

Merged
merged 14 commits into from
Dec 3, 2024
Merged
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 @@ -80,37 +80,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 Down Expand Up @@ -215,20 +184,6 @@ 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
Expand All @@ -241,20 +196,6 @@ public 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
Expand Down Expand Up @@ -345,7 +286,9 @@ public void setEnabled(boolean isEnabled) {
* @param scale
* a double value that specifies the scale to be applied to the channel value
* @since 2.3
* @deprecated since version 3.0
*/
@Deprecated
public void setScale(double scale) {
this.valueScale = scale;
}
Expand All @@ -367,7 +310,9 @@ public void setScale(Number scale) {
* @param offset
* a double value that specifies the offset to be applied to the channel value
* @since 2.3
* @deprecated since version 3.0
*/
@Deprecated
public void setOffset(double 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,15 +48,23 @@ 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;
}

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

}
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 @@ -462,12 +462,12 @@ private TypedValue<? extends Number> calculateScaleAndOffsetByTypedValue(TypedVa
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();
break;
case DOUBLE:
result = (double) typedValue.getValue() * scale.doubleValue() + offset.doubleValue();
break;
case INTEGER:
result = (int) typedValue.getValue() * scale.intValue() + offset.intValue();
break;
Expand All @@ -488,9 +488,15 @@ private TypedValue<? extends Number> calculateScaleAndOffset(DataType outputValu
Number result = null;

switch (scaleOffsetType) {
case FLOAT:
result = scale.floatValue() * typedValue.getValue().floatValue() + offset.floatValue();
break;
case DOUBLE:
result = scale.doubleValue() * typedValue.getValue().doubleValue() + offset.doubleValue();
break;
case INTEGER:
result = scale.intValue() * typedValue.getValue().intValue() + offset.intValue();
break;
case LONG:
result = scale.longValue() * typedValue.getValue().longValue() + offset.longValue();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ private static Number getValueScale(final Map<String, Object> properties) {
if (valueScale == null || valueScale.isEmpty()) {
return 1.0d;
}
return parseScaleOffsetTypedValue(getScaleOffsetType(properties), valueScale);
return parseScaleOffsetTypedValue(getScaleOffsetType(properties), getDataType(properties), valueScale);
}

private static Number getValueOffset(final Map<String, Object> properties) {
Expand All @@ -354,7 +354,7 @@ private static Number getValueOffset(final Map<String, Object> properties) {
if (valueOffset == null || valueOffset.isEmpty()) {
return 0.0d;
}
return parseScaleOffsetTypedValue(getScaleOffsetType(properties), valueOffset);
return parseScaleOffsetTypedValue(getScaleOffsetType(properties), getDataType(properties), valueOffset);
}

private static String getUnit(final Map<String, Object> properties) {
Expand Down Expand Up @@ -402,18 +402,42 @@ private static Channel extractChannel(final String channelName, final Map<String
return channel;
}

private static Number parseScaleOffsetTypedValue(ScaleOffsetType type, String value) {
Objects.requireNonNull(type, "type cannot be null");
private static Number parseScaleOffsetTypedValue(ScaleOffsetType scaleOffsetType, DataType valueType,
String value) {
Objects.requireNonNull(scaleOffsetType, "scaleOffsetType cannot be null");
Objects.requireNonNull(valueType, "valueType cannot be null");
Objects.requireNonNull(value, "value cannot be null");

switch (type) {
case DEFINED_BY_VALUE_TYPE:
DataType actualDataType = scaleOffsetType == ScaleOffsetType.DEFINED_BY_VALUE_TYPE ? valueType
: toDataType(scaleOffsetType);

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

private static DataType toDataType(ScaleOffsetType scaleOffsetType) {
switch (scaleOffsetType) {
case FLOAT:
return DataType.FLOAT;
case DOUBLE:
return DataType.DOUBLE;
case INTEGER:
return DataType.INTEGER;
case LONG:
return DataType.LONG;
default:
throw new IllegalArgumentException("Unsupported scale offset type: " + scaleOffsetType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ public static ChannelRecord createModifiedChannelRecord(Channel channel) {

private static DataType toDataType(ScaleOffsetType scaleOffsetType) {
switch (scaleOffsetType) {
case FLOAT:
return DataType.FLOAT;
case DOUBLE:
return DataType.DOUBLE;
case INTEGER:
return DataType.INTEGER;
case LONG:
return DataType.LONG;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Bundle-Category: Asset-Driver Management
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.kura;version="[1.3,2.0)",
org.eclipse.kura.bluetooth.le;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,1.1)",
org.eclipse.kura.configuration;version="[1.1,2.0)",
org.eclipse.kura.configuration.metatype;version="[1.0,2.0)",
Expand Down
2 changes: 1 addition & 1 deletion kura/org.eclipse.kura.driver.ble.xdk/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Bundle-Category: Asset-Driver Management
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.kura;version="[1.3,2.0)",
org.eclipse.kura.bluetooth.le;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,1.1)",
org.eclipse.kura.configuration;version="[1.1,2.0)",
org.eclipse.kura.configuration.metatype;version="[1.0,2.0)",
Expand Down
2 changes: 1 addition & 1 deletion kura/org.eclipse.kura.driver.block/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bundle-SymbolicName: org.eclipse.kura.driver.block
Bundle-Version: 2.0.0.qualifier
Bundle-Vendor: Eclipse Kura
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, 2.0)",
org.eclipse.kura.driver;version="[1.0, 2.0)",
org.eclipse.kura.type;version="[1.0, 2.0)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Import-Package: org.eclipse.kura;version="[1.3,2.0)",
org.eclipse.kura.bluetooth.le;version="[1.0,2.0)",
org.eclipse.kura.bluetooth.le.beacon;version="[1.0,2.0)",
org.eclipse.kura.bluetooth.le.beacon.listener;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,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 @@ -11,7 +11,7 @@ Import-Package: javax.security.cert,
javax.xml,
javax.xml.stream,
org.eclipse.kura;version="[1.3,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 @@ -12,7 +12,7 @@ Import-Package: org.eclipse.kura;version="[1.3,2.0)",
org.eclipse.kura.bluetooth.le;version="[1.0,2.0)",
org.eclipse.kura.bluetooth.le.beacon;version="[1.0,2.0)",
org.eclipse.kura.bluetooth.le.beacon.listener;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,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 @@ -15,7 +15,7 @@ Import-Package: javax.crypto,
javax.xml.bind,
javax.xml.stream,
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 @@ -7,7 +7,7 @@ Bundle-Vendor: Eclipse Kura
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"
Import-Package: Moka7;version="[1.0,1.1)",
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,2.0)",
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 @@ -14,7 +14,7 @@ Import-Package: com.google.gson;version="2.7.0",
javax.ws.rs.core;version="2.0.1",
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.rest.utils;version="[1.0,1.1)",
org.eclipse.kura.type;version="[1.1,2.0]",
org.osgi.framework;version="1.8.0",
Expand Down
2 changes: 1 addition & 1 deletion kura/org.eclipse.kura.web2/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Import-Package: com.eclipsesource.json;version="0.9.5",
org.eclipse.kura.asset.provider;version="[2.1,2.2)",
org.eclipse.kura.audit;version="[1.0,2.0)",
org.eclipse.kura.certificate;version="[2.1,3.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.0,2.0)",
org.eclipse.kura.cloud.factory;version="[1.1,2.0)",
org.eclipse.kura.cloudconnection;version="[1.0,2.0)",
Expand Down
Loading
Loading