use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.NumberValue in project smarthome by eclipse.
the class ChannelStateTests method receiveDecimalFractionalTest.
@Test
public void receiveDecimalFractionalTest() throws InterruptedException, ExecutionException, TimeoutException {
NumberValue value = new NumberValue(null, null, new BigDecimal(10.5));
ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
c.start(connection, mock(ScheduledExecutorService.class), 100);
c.processMessage("state", "5.5".getBytes());
assertThat(value.getChannelState().toString(), is("5.5"));
c.processMessage("state", "INCREASE".getBytes());
assertThat(value.getChannelState().toString(), is("16.0"));
}
use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.NumberValue in project smarthome by eclipse.
the class ChannelStateTests method receiveDecimalTest.
@Test
public void receiveDecimalTest() throws InterruptedException, ExecutionException, TimeoutException {
NumberValue value = new NumberValue(null, null, new BigDecimal(10));
ChannelState c = spy(new ChannelState(config, channelUID, value, channelStateUpdateListener));
c.start(connection, mock(ScheduledExecutorService.class), 100);
c.processMessage("state", "15".getBytes());
assertThat(value.getChannelState().toString(), is("15"));
c.processMessage("state", "INCREASE".getBytes());
assertThat(value.getChannelState().toString(), is("25"));
c.processMessage("state", "DECREASE".getBytes());
assertThat(value.getChannelState().toString(), is("15"));
verify(channelStateUpdateListener, times(3)).updateChannelState(eq(channelUID), any());
}
use of org.eclipse.smarthome.binding.mqtt.generic.internal.values.NumberValue in project smarthome by eclipse.
the class Property method createChannelFromAttribute.
public void createChannelFromAttribute() {
final String commandTopic = topic + "/set";
final String stateTopic = topic;
Value value;
Boolean isDecimal = null;
switch(attributes.datatype) {
case boolean_:
value = new OnOffValue("true", "false");
break;
case color_:
value = new ColorValue(attributes.format.contains("rgb"), null, null, 100);
break;
case enum_:
String[] enumValues = attributes.format.split(",");
value = new TextValue(enumValues);
break;
case float_:
case integer_:
isDecimal = attributes.datatype == DataTypeEnum.float_;
String[] s = attributes.format.split("\\:");
BigDecimal min = s.length == 2 ? convertFromString(s[0]) : null;
BigDecimal max = s.length == 2 ? convertFromString(s[1]) : null;
BigDecimal step = (min != null && max != null) ? max.subtract(min).divide(new BigDecimal(100.0), new MathContext(isDecimal ? 2 : 0)) : null;
if (step != null && !isDecimal && step.intValue() <= 0) {
step = new BigDecimal(1);
}
value = new NumberValue(min, max, step);
break;
case string_:
case unknown:
default:
value = new TextValue();
break;
}
ChannelConfigBuilder b = ChannelConfigBuilder.create().makeTrigger(!attributes.retained).withStateTopic(stateTopic);
if (isDecimal != null && !isDecimal) {
// Apply formatter to only publish integers
b = b.withFormatter("%d");
}
if (attributes.settable) {
b = b.withCommandTopic(commandTopic);
}
final ChannelState channelState = new ChannelState(b.build(), channelUID, value, callback);
this.channelState = channelState;
final ChannelType type = createChannelType(attributes, channelState);
this.type = type;
this.channel = ChannelBuilder.create(channelUID, type.getItemType()).withType(type.getUID()).withKind(type.getKind()).withLabel(attributes.name).withConfiguration(new Configuration(attributes.asMap())).build();
}
Aggregations