Search in sources :

Example 46 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class ItemStateConverterImpl method convertToAcceptedState.

@Override
@NonNull
public State convertToAcceptedState(@Nullable State state, @Nullable Item item) {
    if (state == null) {
        logger.error("A conversion of null was requested:", new IllegalArgumentException("State must not be null."));
        return UnDefType.NULL;
    }
    if (item != null && !isAccepted(item, state)) {
        for (Class<? extends State> acceptedType : item.getAcceptedDataTypes()) {
            State convertedState = state.as(acceptedType);
            if (convertedState != null) {
                logger.debug("Converting {} '{}' to {} '{}' for item '{}'", state.getClass().getSimpleName(), state, convertedState.getClass().getSimpleName(), convertedState, item.getName());
                return convertedState;
            }
        }
    }
    if (item instanceof NumberItem && state instanceof QuantityType) {
        NumberItem numberItem = (NumberItem) item;
        if (numberItem.getDimension() != null) {
            QuantityType<?> quantityState = (QuantityType<?>) state;
            // in case the item does define a unit it takes precedence over all other conversions:
            Unit<?> itemUnit = parseItemUnit(numberItem);
            if (itemUnit != null) {
                if (!itemUnit.equals(quantityState.getUnit())) {
                    return convertOrUndef(quantityState, itemUnit);
                }
                return quantityState;
            }
            Class<? extends Quantity<?>> dimension = numberItem.getDimension();
            @SuppressWarnings({ "unchecked", "rawtypes" }) Unit<? extends Quantity<?>> conversionUnit = unitProvider.getUnit((Class<Quantity>) dimension);
            if (conversionUnit != null && UnitUtils.isDifferentMeasurementSystem(conversionUnit, quantityState.getUnit())) {
                return convertOrUndef(quantityState, conversionUnit);
            }
            return state;
        } else {
            return state.as(DecimalType.class);
        }
    }
    return state;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) Quantity(javax.measure.Quantity) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 47 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class GenericItem method applyState.

/**
 * Sets new state, notifies listeners and sends events.
 *
 * Classes overriding the {@link #setState(State)} method should call this method in order to actually set the
 * state, inform listeners and send the event.
 *
 * @param state new state of this item
 */
protected final void applyState(State state) {
    State oldState = this.state;
    this.state = state;
    notifyListeners(oldState, state);
    if (!oldState.equals(state)) {
        sendStateChangedEvent(state, oldState);
    }
}
Also used : State(org.eclipse.smarthome.core.types.State)

Example 48 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class GroupItem method stateUpdated.

@Override
public void stateUpdated(Item item, State state) {
    State oldState = this.state;
    if (function != null && baseItem != null) {
        State calculatedState = function.calculate(getStateMembers(getMembers()));
        calculatedState = itemStateConverter.convertToAcceptedState(calculatedState, baseItem);
        setState(calculatedState);
    }
    if (!oldState.equals(this.state)) {
        sendGroupStateChangedEvent(item.getName(), this.state, oldState);
    }
}
Also used : State(org.eclipse.smarthome.core.types.State)

Example 49 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class GroupItem method setState.

@Override
public void setState(State state) {
    State oldState = this.state;
    if (baseItem != null) {
        baseItem.setState(state);
        this.state = baseItem.getState();
    } else {
        this.state = state;
    }
    notifyListeners(oldState, state);
}
Also used : State(org.eclipse.smarthome.core.types.State)

Example 50 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class StringItem method getStateAs.

@Override
@Nullable
public <T extends State> T getStateAs(Class<T> typeClass) {
    ArrayList<Class<? extends State>> list = new ArrayList<Class<? extends State>>();
    list.add(typeClass);
    State convertedState = TypeParser.parseState(list, state.toString());
    if (typeClass.isInstance(convertedState)) {
        return typeClass.cast(convertedState);
    } else {
        return super.getStateAs(typeClass);
    }
}
Also used : State(org.eclipse.smarthome.core.types.State) ArrayList(java.util.ArrayList) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

State (org.eclipse.smarthome.core.types.State)130 Test (org.junit.Test)59 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)23 PercentType (org.eclipse.smarthome.core.library.types.PercentType)22 Item (org.eclipse.smarthome.core.items.Item)21 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)19 Temperature (javax.measure.quantity.Temperature)18 StringType (org.eclipse.smarthome.core.library.types.StringType)18 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)17 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)15 HSBType (org.eclipse.smarthome.core.library.types.HSBType)15 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)15 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)13 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)12 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)11 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)10 RawType (org.eclipse.smarthome.core.library.types.RawType)10 Pressure (javax.measure.quantity.Pressure)9 GroupItem (org.eclipse.smarthome.core.items.GroupItem)8