Search in sources :

Example 31 with HSBType

use of org.eclipse.smarthome.core.library.types.HSBType in project smarthome by eclipse.

the class ColorpickerRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Colorpicker cp = (Colorpicker) w;
    String snippetName = "colorpicker";
    String snippet = getSnippet(snippetName);
    // set the default send-update frequency to 200ms
    String frequency = cp.getFrequency() == 0 ? "200" : Integer.toString(cp.getFrequency());
    // get RGB hex value
    State state = itemUIRegistry.getState(cp);
    String hexValue = "#ffffff";
    if (state instanceof HSBType) {
        HSBType hsbState = (HSBType) state;
        hexValue = "#" + Integer.toHexString(hsbState.getRGB()).substring(2);
    }
    String label = getLabel(cp);
    String purelabel = itemUIRegistry.getLabel(w);
    purelabel = purelabel.replaceAll("\\\"", "\\\\'");
    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(cp));
    snippet = StringUtils.replace(snippet, "%category%", getCategory(cp));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%label%", label);
    snippet = StringUtils.replace(snippet, "%format%", getFormat());
    snippet = StringUtils.replace(snippet, "%purelabel%", purelabel);
    snippet = StringUtils.replace(snippet, "%state%", hexValue);
    snippet = StringUtils.replace(snippet, "%frequency%", frequency);
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);
    String style = "";
    String color = itemUIRegistry.getLabelColor(w);
    if (color != null) {
        style = "color:" + color;
    }
    snippet = StringUtils.replace(snippet, "%labelstyle%", style);
    style = "";
    color = itemUIRegistry.getValueColor(w);
    if (color != null) {
        style = "color:" + color;
    }
    snippet = StringUtils.replace(snippet, "%valuestyle%", style);
    sb.append(snippet);
    return null;
}
Also used : State(org.eclipse.smarthome.core.types.State) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Colorpicker(org.eclipse.smarthome.model.sitemap.Colorpicker)

Example 32 with HSBType

use of org.eclipse.smarthome.core.library.types.HSBType 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);
    }
}
Also used : State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) HSBType(org.eclipse.smarthome.core.library.types.HSBType)

Example 33 with HSBType

use of org.eclipse.smarthome.core.library.types.HSBType in project smarthome by eclipse.

the class HueLightHandler method onLightStateChanged.

@Override
public void onLightStateChanged(@Nullable HueBridge bridge, FullLight fullLight) {
    logger.trace("onLightStateChanged() was called");
    if (!fullLight.getId().equals(lightId)) {
        logger.trace("Received state change for another handler's light ({}). Will be ignored.", fullLight.getId());
        return;
    }
    initializeProperties();
    lastSentColorTemp = null;
    lastSentBrightness = null;
    // update status (ONLINE, OFFLINE)
    if (fullLight.getState().isReachable()) {
        updateStatus(ThingStatus.ONLINE);
    } else {
        // we assume OFFLINE without any error (NONE), as this is an
        // expected state (when bulb powered off)
        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/offline.light-not-reachable");
    }
    HSBType hsbType = LightStateConverter.toHSBType(fullLight.getState());
    if (!fullLight.getState().isOn()) {
        hsbType = new HSBType(hsbType.getHue(), hsbType.getSaturation(), new PercentType(0));
    }
    updateState(CHANNEL_COLOR, hsbType);
    PercentType percentType = LightStateConverter.toColorTemperaturePercentType(fullLight.getState());
    updateState(CHANNEL_COLORTEMPERATURE, percentType);
    percentType = LightStateConverter.toBrightnessPercentType(fullLight.getState());
    if (!fullLight.getState().isOn()) {
        percentType = new PercentType(0);
    }
    updateState(CHANNEL_BRIGHTNESS, percentType);
    if (fullLight.getState().isOn()) {
        updateState(CHANNEL_SWITCH, OnOffType.ON);
    } else {
        updateState(CHANNEL_SWITCH, OnOffType.OFF);
    }
    StringType stringType = LightStateConverter.toAlertStringType(fullLight.getState());
    if (!stringType.toString().equals("NULL")) {
        updateState(CHANNEL_ALERT, stringType);
        scheduleAlertStateRestore(stringType);
    }
}
Also used : StringType(org.eclipse.smarthome.core.library.types.StringType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) HSBType(org.eclipse.smarthome.core.library.types.HSBType)

Example 34 with HSBType

use of org.eclipse.smarthome.core.library.types.HSBType in project smarthome by eclipse.

the class HueLightHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    HueBridgeHandler hueBridge = getHueBridgeHandler();
    if (hueBridge == null) {
        logger.warn("hue bridge handler not found. Cannot handle command without bridge.");
        return;
    }
    FullLight light = getLight();
    if (light == null) {
        logger.debug("hue light not known on bridge. Cannot handle command.");
        return;
    }
    StateUpdate lightState = null;
    switch(channelUID.getId()) {
        case CHANNEL_COLORTEMPERATURE:
            if (command instanceof PercentType) {
                lightState = LightStateConverter.toColorTemperatureLightState((PercentType) command);
            } else if (command instanceof OnOffType) {
                lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
                if (isOsramPar16) {
                    lightState = addOsramSpecificCommands(lightState, (OnOffType) command);
                }
            } else if (command instanceof IncreaseDecreaseType) {
                lightState = convertColorTempChangeToStateUpdate((IncreaseDecreaseType) command, light);
            }
            break;
        case CHANNEL_BRIGHTNESS:
            if (command instanceof PercentType) {
                lightState = LightStateConverter.toBrightnessLightState((PercentType) command);
            } else if (command instanceof OnOffType) {
                lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
                if (isOsramPar16) {
                    lightState = addOsramSpecificCommands(lightState, (OnOffType) command);
                }
            } else if (command instanceof IncreaseDecreaseType) {
                lightState = convertBrightnessChangeToStateUpdate((IncreaseDecreaseType) command, light);
            }
            break;
        case CHANNEL_SWITCH:
            logger.trace("CHANNEL_SWITCH handling command {}", command);
            if (command instanceof OnOffType) {
                lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
                if (isOsramPar16) {
                    lightState = addOsramSpecificCommands(lightState, (OnOffType) command);
                }
            }
            break;
        case CHANNEL_COLOR:
            if (command instanceof HSBType) {
                HSBType hsbCommand = (HSBType) command;
                if (hsbCommand.getBrightness().intValue() == 0) {
                    lightState = LightStateConverter.toOnOffLightState(OnOffType.OFF);
                } else {
                    lightState = LightStateConverter.toColorLightState(hsbCommand);
                }
            } else if (command instanceof PercentType) {
                lightState = LightStateConverter.toBrightnessLightState((PercentType) command);
            } else if (command instanceof OnOffType) {
                lightState = LightStateConverter.toOnOffLightState((OnOffType) command);
            } else if (command instanceof IncreaseDecreaseType) {
                lightState = convertBrightnessChangeToStateUpdate((IncreaseDecreaseType) command, light);
            }
            break;
        case CHANNEL_ALERT:
            if (command instanceof StringType) {
                lightState = LightStateConverter.toAlertState((StringType) command);
                if (lightState == null) {
                    // Unsupported StringType is passed. Log a warning
                    // message and return.
                    logger.warn("Unsupported String command: {}. Supported commands are: {}, {}, {} ", command, LightStateConverter.ALERT_MODE_NONE, LightStateConverter.ALERT_MODE_SELECT, LightStateConverter.ALERT_MODE_LONG_SELECT);
                    return;
                } else {
                    scheduleAlertStateRestore(command);
                }
            }
            break;
        case CHANNEL_EFFECT:
            if (command instanceof OnOffType) {
                lightState = LightStateConverter.toOnOffEffectState((OnOffType) command);
            }
            break;
    }
    if (lightState != null) {
        hueBridge.updateLightState(light, lightState);
    } else {
        logger.warn("Command sent to an unknown channel id: {}", channelUID);
    }
}
Also used : StateUpdate(org.eclipse.smarthome.binding.hue.internal.StateUpdate) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) StringType(org.eclipse.smarthome.core.library.types.StringType) IncreaseDecreaseType(org.eclipse.smarthome.core.library.types.IncreaseDecreaseType) FullLight(org.eclipse.smarthome.binding.hue.internal.FullLight) PercentType(org.eclipse.smarthome.core.library.types.PercentType) HSBType(org.eclipse.smarthome.core.library.types.HSBType)

Aggregations

HSBType (org.eclipse.smarthome.core.library.types.HSBType)34 Test (org.junit.Test)19 PercentType (org.eclipse.smarthome.core.library.types.PercentType)16 State (org.eclipse.smarthome.core.types.State)10 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)9 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)4 IncreaseDecreaseType (org.eclipse.smarthome.core.library.types.IncreaseDecreaseType)3 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)3 StringType (org.eclipse.smarthome.core.library.types.StringType)3 ChannelUID (org.eclipse.smarthome.core.thing.ChannelUID)2 RefreshType (org.eclipse.smarthome.core.types.RefreshType)2 Colorpicker (org.eclipse.smarthome.model.sitemap.Colorpicker)2 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)2 Switch (org.eclipse.smarthome.model.sitemap.Switch)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 BigDecimal (java.math.BigDecimal)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Locale (java.util.Locale)1 MissingResourceException (java.util.MissingResourceException)1