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

The following examples show how to use org.eclipse.smarthome.core.types.State#equals() . 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: RuleTriggerManager.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private void internalGetUpdateRules(String name, Boolean isGroup, List<Class<? extends State>> acceptedDataTypes,
        State state, List<Rule> result) {
    final String mapName = (isGroup) ? GROUP_NAME_PREFIX + name : name;
    for (Rule rule : getAllRules(UPDATE, mapName)) {
        for (EventTrigger t : rule.getEventtrigger()) {
            String triggerStateString = null;
            if ((!isGroup) && (t instanceof UpdateEventTrigger)) {
                final UpdateEventTrigger ut = (UpdateEventTrigger) t;
                if (ut.getItem().equals(name)) {
                    triggerStateString = ut.getState() != null ? ut.getState().getValue() : null;
                } else {
                    continue;
                }
            } else if ((isGroup) && (t instanceof GroupMemberUpdateEventTrigger)) {
                final GroupMemberUpdateEventTrigger gmut = (GroupMemberUpdateEventTrigger) t;
                if (gmut.getGroup().equals(name)) {
                    triggerStateString = gmut.getState() != null ? gmut.getState().getValue() : null;
                } else {
                    continue;
                }
            } else {
                continue;
            }
            if (triggerStateString != null) {
                final State triggerState = TypeParser.parseState(acceptedDataTypes, triggerStateString);
                if (!state.equals(triggerState)) {
                    continue;
                }
            }
            result.add(rule);
        }
    }
}
 
Example 2
Source File: StateUtil.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
public static void testAcceptedStates(GenericItem item) {
    HashSet<Class<? extends State>> successfullStates = new HashSet<>();

    for (State s : getAllStates()) {
        item.setState(s);
        if (item.isAcceptedState(item.getAcceptedDataTypes(), s)) {
            if (s.equals(UnDefType.NULL)) {
                // if we set null, the item should stay null
                assertEquals(UnDefType.NULL, item.getState());
            } else {
                // the state should be set on the item now
                assertNotEquals(UnDefType.NULL, item.getState());
                successfullStates.add(s.getClass());
            }
            // reset item
            item.setState(UnDefType.NULL);
        } else {
            assertEquals(UnDefType.NULL, item.getState());
        }
    }

    // test if the item accepts a state that is not in our test state list
    for (Class<? extends State> acceptedState : item.getAcceptedDataTypes()) {
        if (!successfullStates.contains(acceptedState)) {
            fail("Item '" + item.getType() + "' accepts untested state: " + acceptedState);
        }
    }
}
 
Example 3
Source File: AbstractRuleBasedInterpreter.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Executes a command on one item that's to be found in the item registry by given name fragments.
 * Fails, if there is more than on item.
 *
 * @param language resource bundle used for producing localized response texts
 * @param labelFragments label fragments that are used to match an item's label.
 *            For a positive match, the item's label has to contain every fragment - independently of their order.
 *            They are treated case insensitive.
 * @param command command that should be executed
 * @return response text
 * @throws InterpretationException in case that there is no or more than on item matching the fragments
 */
protected String executeSingle(ResourceBundle language, String[] labelFragments, Command command)
        throws InterpretationException {
    ArrayList<Item> items = getMatchingItems(language, labelFragments, command.getClass());
    if (items.size() < 1) {
        if (getMatchingItems(language, labelFragments, null).size() >= 1) {
            throw new InterpretationException(
                    language.getString(COMMAND_NOT_ACCEPTED).replace("<cmd>", command.toString()));
        } else {
            throw new InterpretationException(language.getString(NO_OBJECTS));
        }
    } else if (items.size() > 1) {
        throw new InterpretationException(language.getString(MULTIPLE_OBJECTS));
    } else {
        Item item = items.get(0);
        if (command instanceof State) {
            try {
                State newState = (State) command;
                State oldState = item.getStateAs(newState.getClass());
                if (oldState.equals(newState)) {
                    String template = language.getString(STATE_ALREADY_SINGULAR);
                    String cmdName = "state_" + command.toString().toLowerCase();
                    String stateText = null;
                    try {
                        stateText = language.getString(cmdName);
                    } catch (Exception e) {
                        stateText = language.getString(STATE_CURRENT);
                    }
                    return template.replace("<state>", stateText);
                }
            } catch (Exception ex) {
                logger.debug("Failed constructing response: {}", ex.getMessage());
                return language.getString(ERROR);
            }
        }
        eventPublisher.post(ItemEventFactory.createCommandEvent(item.getName(), command));
        return language.getString(OK);
    }
}
 
Example 4
Source File: GroupFunction.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public State calculate(Set<Item> items) {
    if (items.size() > 0) {
        Iterator<Item> it = items.iterator();
        State state = it.next().getState();
        while (it.hasNext()) {
            if (!state.equals(it.next().getState())) {
                return UnDefType.UNDEF;
            }
        }
        return state;
    } else {
        return UnDefType.UNDEF;
    }
}
 
Example 5
Source File: GroupItem.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 6
Source File: GenericItem.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 7
Source File: LifxLightHandler.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private void updateStateIfChanged(String channel, State newState) {
    State oldState = channelStates.get(channel);
    if (oldState == null || !oldState.equals(newState)) {
        updateState(channel, newState);
        channelStates.put(channel, newState);
    }
}
 
Example 8
Source File: RuleTriggerManager.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
private void internalGetChangeRules(String name, Boolean isGroup, List<Class<? extends State>> acceptedDataTypes,
        State newState, State oldState, List<Rule> result) {
    final String mapName = (isGroup) ? GROUP_NAME_PREFIX + name : name;
    for (Rule rule : getAllRules(CHANGE, mapName)) {
        for (EventTrigger t : rule.getEventtrigger()) {
            String triggerOldStateString = null;
            String triggerNewStateString = null;
            if ((!isGroup) && (t instanceof ChangedEventTrigger)) {
                final ChangedEventTrigger ct = (ChangedEventTrigger) t;
                if (ct.getItem().equals(name)) {
                    triggerOldStateString = ct.getOldState() != null ? ct.getOldState().getValue() : null;
                    triggerNewStateString = ct.getNewState() != null ? ct.getNewState().getValue() : null;
                } else {
                    continue;
                }
            } else if ((isGroup) && (t instanceof GroupMemberChangedEventTrigger)) {
                final GroupMemberChangedEventTrigger gmct = (GroupMemberChangedEventTrigger) t;
                if (gmct.getGroup().equals(name)) {
                    triggerOldStateString = gmct.getOldState() != null ? gmct.getOldState().getValue() : null;
                    triggerNewStateString = gmct.getNewState() != null ? gmct.getNewState().getValue() : null;
                } else {
                    continue;
                }
            } else {
                continue;
            }
            if (triggerOldStateString != null) {
                final State triggerOldState = TypeParser.parseState(acceptedDataTypes, triggerOldStateString);
                if (!oldState.equals(triggerOldState)) {
                    continue;
                }
            }
            if (triggerNewStateString != null) {
                final State triggerNewState = TypeParser.parseState(acceptedDataTypes, triggerNewStateString);
                if (!newState.equals(triggerNewState)) {
                    continue;
                }
            }
            result.add(rule);
        }
    }
}
 
Example 9
Source File: ItemStateConditionHandler.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean isSatisfied(Map<String, Object> inputs) {
    String itemName = (String) module.getConfiguration().get(ITEM_NAME);
    String state = (String) module.getConfiguration().get(STATE);
    String operator = (String) module.getConfiguration().get(OPERATOR);
    if (operator == null || state == null || itemName == null) {
        logger.error("Module is not well configured: itemName={}  operator={}  state = {}", itemName, operator,
                state);
        return false;
    }
    if (itemRegistry == null) {
        logger.error("The ItemRegistry is not available to evaluate the condition.");
        return false;
    }
    try {
        Item item = itemRegistry.getItem(itemName);
        State compareState = TypeParser.parseState(item.getAcceptedDataTypes(), state);
        State itemState = item.getState();
        logger.debug("ItemStateCondition '{}'checking if {} (State={}) {} {}", module.getId(), itemName, itemState,
                operator, compareState);
        switch (operator) {
            case "=":
                logger.debug("ConditionSatisfied --> {}", itemState.equals(compareState));
                return itemState.equals(compareState);
            case "!=":
                return !itemState.equals(compareState);
            case "<":
                if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
                    return ((DecimalType) itemState).compareTo((DecimalType) compareState) < 0;
                }
                break;
            case "<=":
            case "=<":
                if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
                    return ((DecimalType) itemState).compareTo((DecimalType) compareState) <= 0;
                }
                break;
            case ">":
                if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
                    return ((DecimalType) itemState).compareTo((DecimalType) compareState) > 0;
                }
                break;
            case ">=":
            case "=>":
                if (itemState instanceof DecimalType && compareState instanceof DecimalType) {
                    return ((DecimalType) itemState).compareTo((DecimalType) compareState) >= 0;
                }
                break;
            default:
                break;
        }
    } catch (ItemNotFoundException e) {
        logger.error("Item with Name {} not found in itemRegistry", itemName);
        return false;
    }
    return false;
}
 
Example 10
Source File: ArithmeticGroupFunction.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public State calculate(Set<Item> items) {
    State result = super.calculate(items);
    State notResult = result.equals(activeState) ? passiveState : activeState;
    return notResult;
}
 
Example 11
Source File: ArithmeticGroupFunction.java    From smarthome with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public State calculate(Set<Item> items) {
    State result = super.calculate(items);
    State notResult = result.equals(activeState) ? passiveState : activeState;
    return notResult;
}