use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.
the class DimmerThingHandler method handleCommand.
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("received command {} in channel {}", command, channelUID);
ValueSet targetValueSet = new ValueSet(fadeTime, -1);
switch(channelUID.getId()) {
case CHANNEL_BRIGHTNESS:
{
if (command instanceof PercentType || command instanceof DecimalType) {
PercentType brightness = (command instanceof PercentType) ? (PercentType) command : Util.toPercentValue(((DecimalType) command).intValue());
logger.trace("adding fade to channels in thing {}", this.thing.getUID());
targetValueSet.addValue(brightness);
} else if (command instanceof OnOffType) {
logger.trace("adding {} fade to channels in thing {}", command, this.thing.getUID());
targetValueSet = ((OnOffType) command).equals(OnOffType.ON) ? turnOnValue : turnOffValue;
} else if (command instanceof IncreaseDecreaseType) {
if (isDimming && ((IncreaseDecreaseType) command).equals(IncreaseDecreaseType.INCREASE)) {
logger.trace("stopping fade in thing {}", this.thing.getUID());
channels.forEach(DmxChannel::clearAction);
isDimming = false;
return;
} else {
logger.trace("starting {} fade in thing {}", command, this.thing.getUID());
targetValueSet = ((IncreaseDecreaseType) command).equals(IncreaseDecreaseType.INCREASE) ? turnOnValue : turnOffValue;
targetValueSet.setFadeTime(dimTime);
isDimming = true;
}
} else if (command instanceof RefreshType) {
logger.trace("sending update on refresh to channel {}:brightness", this.thing.getUID());
currentBrightness = Util.toPercentValue(channels.get(0).getValue());
updateState(channelUID, currentBrightness);
return;
} else {
logger.debug("command {} not supported in channel {}:brightness", command.getClass(), this.thing.getUID());
return;
}
break;
}
default:
logger.debug("channel {} not supported in thing {}", channelUID.getId(), this.thing.getUID());
return;
}
final ValueSet valueSet = targetValueSet;
IntStream.range(0, channels.size()).forEach(i -> {
channels.get(i).setChannelAction(new FadeAction(valueSet.getFadeTime(), channels.get(i).getValue(), valueSet.getValue(i), valueSet.getHoldTime()));
});
}
use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.
the class TunableWhiteThingHandler method handleCommand.
@Override
public void handleCommand(ChannelUID channelUID, Command command) {
logger.trace("received command {} in channel {}", command, channelUID);
ValueSet targetValueSet = new ValueSet(fadeTime, -1);
switch(channelUID.getId()) {
case CHANNEL_BRIGHTNESS:
{
if (command instanceof PercentType || command instanceof DecimalType) {
PercentType brightness = (command instanceof PercentType) ? (PercentType) command : Util.toPercentValue(((DecimalType) command).intValue());
logger.trace("adding fade to channels in thing {}", this.thing.getUID());
targetValueSet.addValue(Util.toDmxValue(Util.toDmxValue(brightness) * (100 - currentColorTemperature.intValue()) / 100));
targetValueSet.addValue(Util.toDmxValue(Util.toDmxValue(brightness) * currentColorTemperature.intValue() / 100));
} else if (command instanceof OnOffType) {
logger.trace("adding {} fade to channels in thing {}", command, this.thing.getUID());
targetValueSet = ((OnOffType) command).equals(OnOffType.ON) ? turnOnValue : turnOffValue;
} else if (command instanceof IncreaseDecreaseType) {
if (isDimming && ((IncreaseDecreaseType) command).equals(IncreaseDecreaseType.INCREASE)) {
logger.trace("stopping fade in thing {}", this.thing.getUID());
channels.forEach(DmxChannel::clearAction);
isDimming = false;
return;
} else {
logger.trace("starting {} fade in thing {}", command, this.thing.getUID());
targetValueSet = ((IncreaseDecreaseType) command).equals(IncreaseDecreaseType.INCREASE) ? turnOnValue : turnOffValue;
targetValueSet.setFadeTime(dimTime);
isDimming = true;
}
} else if (command instanceof RefreshType) {
logger.trace("sending update on refresh to channel {}:brightness", this.thing.getUID());
currentValues.set(0, channels.get(0).getValue());
currentValues.set(1, channels.get(1).getValue());
updateCurrentBrightnessAndTemperature();
updateState(channelUID, currentBrightness);
return;
} else {
logger.debug("command {} not supported in channel {}:brightness", command.getClass(), this.thing.getUID());
return;
}
break;
}
case CHANNEL_BRIGHTNESS_CW:
if (command instanceof RefreshType) {
logger.trace("sending update on refresh to channel {}:brightness_cw", this.thing.getUID());
currentValues.set(0, channels.get(0).getValue());
updateState(channelUID, Util.toPercentValue(currentValues.get(0)));
return;
} else {
logger.debug("command {} not supported in channel {}:brightness_cw", command.getClass(), this.thing.getUID());
return;
}
case CHANNEL_BRIGHTNESS_WW:
if (command instanceof RefreshType) {
logger.trace("sending update on refresh to channel {}:brightness_ww", this.thing.getUID());
currentValues.set(1, channels.get(1).getValue());
updateState(channelUID, Util.toPercentValue(currentValues.get(1)));
return;
} else {
logger.debug("command {} not supported in channel {}:brightness_ww", command.getClass(), this.thing.getUID());
return;
}
case CHANNEL_COLOR_TEMPERATURE:
{
if (command instanceof PercentType) {
PercentType colorTemperature = (PercentType) command;
targetValueSet.addValue(Util.toDmxValue(Util.toDmxValue(currentBrightness) * (100 - colorTemperature.intValue()) / 100));
targetValueSet.addValue(Util.toDmxValue(Util.toDmxValue(currentBrightness) * colorTemperature.intValue() / 100));
} else if (command instanceof RefreshType) {
logger.trace("sending update on refresh to channel {}:color_temperature", this.thing.getUID());
currentValues.set(0, channels.get(0).getValue());
currentValues.set(1, channels.get(1).getValue());
updateCurrentBrightnessAndTemperature();
updateState(channelUID, currentColorTemperature);
return;
} else {
logger.debug("command {} not supported in channel {}:color_temperature", command.getClass(), this.thing.getUID());
return;
}
break;
}
default:
logger.debug("channel {} not supported in thing {}", channelUID.getId(), this.thing.getUID());
return;
}
final ValueSet valueSet = targetValueSet;
IntStream.range(0, channels.size()).forEach(i -> {
channels.get(i).setChannelAction(new FadeAction(valueSet.getFadeTime(), channels.get(i).getValue(), valueSet.getValue(i), valueSet.getHoldTime()));
});
}
use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.
the class ItemStateConditionHandler method isSatisfied.
@Override
public boolean isSatisfied(Map<String, Object> inputs) {
String itemName = (String) module.getConfiguration().get(ITEM_NAME);
String state = (String) module.getConfiguration().get(STATE);
String operator = (String) module.getConfiguration().get(OPERATOR);
if (operator == null || state == null || itemName == null) {
logger.error("Module is not well configured: itemName={} operator={} state = {}", itemName, operator, state);
return false;
}
if (itemRegistry == null) {
logger.error("The ItemRegistry is not available to evaluate the condition.");
return false;
}
try {
Item item = itemRegistry.getItem(itemName);
State compareState = TypeParser.parseState(item.getAcceptedDataTypes(), state);
State itemState = item.getState();
logger.debug("ItemStateCondition '{}'checking if {} (State={}) {} {}", module.getId(), itemName, itemState, operator, compareState);
switch(operator) {
case "=":
logger.debug("ConditionSatisfied --> {}", itemState.equals(compareState));
return itemState.equals(compareState);
case "!=":
return !itemState.equals(compareState);
case "<":
if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
return ((DecimalType) itemState).compareTo((DecimalType) compareState) < 0;
}
break;
case "<=":
case "=<":
if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
return ((DecimalType) itemState).compareTo((DecimalType) compareState) <= 0;
}
break;
case ">":
if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
return ((DecimalType) itemState).compareTo((DecimalType) compareState) > 0;
}
break;
case ">=":
case "=>":
if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
return ((DecimalType) itemState).compareTo((DecimalType) compareState) >= 0;
}
break;
default:
break;
}
} catch (ItemNotFoundException e) {
logger.error("Item with Name {} not found in itemRegistry", itemName);
return false;
}
return false;
}
use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.
the class ColorItem method setState.
@Override
public void setState(State state) {
if (isAcceptedState(acceptedDataTypes, state)) {
State currentState = this.state;
if (currentState instanceof HSBType) {
DecimalType hue = ((HSBType) currentState).getHue();
PercentType saturation = ((HSBType) currentState).getSaturation();
// we map ON/OFF values to dark/bright, so that the hue and saturation values are not changed
if (state == OnOffType.OFF) {
applyState(new HSBType(hue, saturation, PercentType.ZERO));
} else if (state == OnOffType.ON) {
applyState(new HSBType(hue, saturation, PercentType.HUNDRED));
} else if (state instanceof PercentType && !(state instanceof HSBType)) {
applyState(new HSBType(hue, saturation, (PercentType) state));
} else if (state instanceof DecimalType && !(state instanceof HSBType)) {
applyState(new HSBType(hue, saturation, new PercentType(((DecimalType) state).toBigDecimal().multiply(BigDecimal.valueOf(100)))));
} else {
applyState(state);
}
} else {
// try conversion
State convertedState = state.as(HSBType.class);
if (convertedState != null) {
applyState(convertedState);
} else {
applyState(state);
}
}
} else {
logSetTypeError(state);
}
}
use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.
the class EnrichedItemDTOMapperWithTransformOSGiTest method shouldConsiderTraformationWhenPresent.
@Test
public void shouldConsiderTraformationWhenPresent() {
NumberItem item1 = new NumberItem("Item1");
item1.setState(new DecimalType("12.34"));
item1.setStateDescriptionService(stateDescriptionService);
EnrichedItemDTO enrichedDTO = EnrichedItemDTOMapper.map(item1, false, null, null, null);
assertThat(enrichedDTO, is(notNullValue()));
assertThat(enrichedDTO.name, is("Item1"));
assertThat(enrichedDTO.state, is("12.34"));
StateDescription sd = enrichedDTO.stateDescription;
assertThat(sd.getMinimum(), is(BigDecimal.valueOf(0)));
assertThat(sd.getMaximum(), is(BigDecimal.valueOf(100)));
assertThat(sd.getStep(), is(BigDecimal.valueOf(10)));
assertThat(sd.getPattern(), is("%d °C"));
assertThat(sd.getOptions().get(0).getValue(), is("SOUND"));
assertThat(sd.getOptions().get(0).getLabel(), is("My great sound."));
}
Aggregations