Java Code Examples for org.eclipse.smarthome.core.types.State#as()

The following examples show how to use org.eclipse.smarthome.core.types.State#as() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ItemUIRegistryImpl.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Converts an item state to the type the widget supports (if possible)
 *
 * @param w Widget in sitemap that shows the state
 * @param i item
 * @param state
 * @return the converted state or the original if conversion was not possible
 */
@Override
public State convertState(Widget w, Item i, State state) {
    State returnState = null;

    State itemState = i.getState();
    if (itemState instanceof QuantityType) {
        itemState = convertStateToWidgetUnit((QuantityType<?>) itemState, w);
    }

    if (w instanceof Switch && i instanceof RollershutterItem) {
        // RollerShutter are represented as Switch in a Sitemap but need a PercentType state
        returnState = itemState.as(PercentType.class);
    } else if (w instanceof Slider) {
        if (i.getAcceptedDataTypes().contains(PercentType.class)) {
            returnState = itemState.as(PercentType.class);
        } else {
            returnState = itemState.as(DecimalType.class);
        }
    } else if (w instanceof Switch) {
        Switch sw = (Switch) w;
        if (sw.getMappings().size() == 0) {
            returnState = itemState.as(OnOffType.class);
        }
    }

    // if returnState is null, a conversion was not possible
    if (returnState == null) {
        // we return the original state to not break anything
        returnState = itemState;
    }
    return returnState;
}
 
Example 2
Source File: DimmerItem.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setState(State state) {
    if (isAcceptedState(acceptedDataTypes, state)) {
        // try conversion
        State convertedState = state.as(PercentType.class);
        if (convertedState != null) {
            applyState(convertedState);
        } else {
            applyState(state);
        }
    } else {
        logSetTypeError(state);
    }
}
 
Example 3
Source File: ColorItem.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 4
Source File: RollershutterItem.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setState(State state) {
    if (isAcceptedState(acceptedDataTypes, state)) {
        // try conversion
        State convertedState = state.as(PercentType.class);
        if (convertedState != null) {
            applyState(convertedState);
        } else {
            applyState(state);
        }
    } else {
        logSetTypeError(state);
    }
}
 
Example 5
Source File: RawButtonToggleSwitchProfile.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void onStateUpdateFromItem(State state) {
    previousState = state.as(OnOffType.class);
}