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;
}
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);
}
}
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);
}
}
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);
}
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);
}
}
Aggregations