use of org.eclipse.smarthome.core.library.types.PercentType in project smarthome by eclipse.
the class ColorThingHandler 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_R:
if (command instanceof RefreshType) {
logger.trace("sending update on refresh to channel {}:brightness_r", this.thing.getUID());
currentValues.set(0, channels.get(0).getValue());
updateCurrentColor();
updateState(channelUID, Util.toPercentValue(currentValues.get(0)));
return;
} else {
logger.debug("command {} not supported in channel {}:brightness_r", command.getClass(), this.thing.getUID());
return;
}
case CHANNEL_BRIGHTNESS_G:
if (command instanceof RefreshType) {
logger.trace("sending update on refresh to channel {}:brightness_g", this.thing.getUID());
currentValues.set(1, channels.get(1).getValue());
updateCurrentColor();
updateState(channelUID, Util.toPercentValue(currentValues.get(1)));
return;
} else {
logger.debug("command {} not supported in channel {}:brightness_g", command.getClass(), this.thing.getUID());
return;
}
case CHANNEL_BRIGHTNESS_B:
if (command instanceof RefreshType) {
logger.trace("sending update on refresh to channel {}:brightness_b", this.thing.getUID());
currentValues.set(2, channels.get(2).getValue());
updateCurrentColor();
updateState(channelUID, Util.toPercentValue(currentValues.get(2)));
return;
} else {
logger.debug("command {} not supported in channel {}:brightness_b", command.getClass(), this.thing.getUID());
return;
}
case CHANNEL_COLOR:
{
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 HSBType) {
logger.trace("adding color fade to channels in thing {}", this.thing.getUID());
targetValueSet.addValue(((HSBType) command).getRed());
targetValueSet.addValue(((HSBType) command).getGreen());
targetValueSet.addValue(((HSBType) command).getBlue());
} else if ((command instanceof PercentType) || (command instanceof DecimalType)) {
logger.trace("adding brightness fade to channels in thing {}", this.thing.getUID());
PercentType brightness = (command instanceof PercentType) ? (PercentType) command : Util.toPercentValue(((DecimalType) command).intValue());
HSBType targetColor = new HSBType(currentColor.getHue(), currentColor.getSaturation(), brightness);
targetValueSet.addValue(targetColor.getRed());
targetValueSet.addValue(targetColor.getGreen());
targetValueSet.addValue(targetColor.getBlue());
} 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());
HSBType targetColor;
if (((IncreaseDecreaseType) command).equals(IncreaseDecreaseType.INCREASE)) {
targetColor = new HSBType(currentColor.getHue(), currentColor.getSaturation(), PercentType.HUNDRED);
} else {
targetColor = new HSBType(currentColor.getHue(), currentColor.getSaturation(), PercentType.ZERO);
}
targetValueSet.addValue(targetColor.getRed());
targetValueSet.addValue(targetColor.getGreen());
targetValueSet.addValue(targetColor.getBlue());
targetValueSet.setFadeTime(dimTime);
isDimming = true;
}
} else if (command instanceof RefreshType) {
logger.trace("sending update on refresh to channel {}:color", this.thing.getUID());
currentValues.set(0, channels.get(0).getValue());
currentValues.set(1, channels.get(1).getValue());
currentValues.set(2, channels.get(2).getValue());
updateCurrentColor();
updateState(channelUID, currentColor);
return;
} else {
logger.debug("command {} not supported in channel {}:color", 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.PercentType 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.PercentType 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.PercentType in project smarthome by eclipse.
the class ColorThingHandlerTest method testColorCommand.
@Test
public void testColorCommand() {
// setting of color
long currentTime = System.currentTimeMillis();
dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, TEST_COLOR);
currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
waitForAssert(() -> {
assertChannelStateUpdate(CHANNEL_UID_COLOR, state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 1))));
assertChannelStateUpdate(CHANNEL_UID_COLOR, state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 0.5))));
assertChannelStateUpdate(CHANNEL_UID_COLOR, state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(100.0, 0.5))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(66.5, 0.5))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertEquals(PercentType.HUNDRED, state));
});
// color dimming
dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR, new PercentType(30));
currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
waitForAssert(() -> {
assertChannelStateUpdate(CHANNEL_UID_COLOR, state -> assertThat(((HSBType) state).getHue().doubleValue(), is(closeTo(280, 2))));
assertChannelStateUpdate(CHANNEL_UID_COLOR, state -> assertThat(((HSBType) state).getSaturation().doubleValue(), is(closeTo(100.0, 1))));
assertChannelStateUpdate(CHANNEL_UID_COLOR, state -> assertThat(((HSBType) state).getBrightness().doubleValue(), is(closeTo(30.0, 1))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_R, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(19.2, 0.5))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_G, state -> assertEquals(PercentType.ZERO, state));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_B, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(29.8, 0.5))));
});
}
use of org.eclipse.smarthome.core.library.types.PercentType in project smarthome by eclipse.
the class TunableWhiteThingHandlerTest method testColorTemperature.
@Test
public void testColorTemperature() {
long currentTime = System.currentTimeMillis();
dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
waitForAssert(() -> {
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50.0, 0.5))));
});
// cool white
dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.ZERO);
currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
waitForAssert(() -> {
assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.1))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.5))));
});
// warm white
dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.HUNDRED);
currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
waitForAssert(() -> {
assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.1))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.5))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
});
// intermediate white
dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, new PercentType(75));
currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
waitForAssert(() -> {
assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(75.0, 0.1))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 1.0))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(25.0, 0.5))));
assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW, state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(75.0, 0.5))));
});
}
Aggregations