Search in sources :

Example 96 with State

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

the class RuleTriggerManager method internalGetChangeRules.

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();
                    triggerNewStateString = ct.getNewState();
                } else {
                    continue;
                }
            } else if ((isGroup) && (t instanceof GroupMemberChangedEventTrigger)) {
                final GroupMemberChangedEventTrigger gmct = (GroupMemberChangedEventTrigger) t;
                if (gmct.getGroup().equals(name)) {
                    triggerOldStateString = gmct.getOldState();
                    triggerNewStateString = gmct.getNewState();
                } 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);
        }
    }
}
Also used : State(org.eclipse.smarthome.core.types.State) Rule(org.eclipse.smarthome.model.rule.rules.Rule) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger) ChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) UpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.UpdateEventTrigger) ThingStateChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateChangedEventTrigger) ThingStateUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.ThingStateUpdateEventTrigger) EventTrigger(org.eclipse.smarthome.model.rule.rules.EventTrigger) ChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.ChangedEventTrigger) GroupMemberUpdateEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberUpdateEventTrigger) GroupMemberChangedEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberChangedEventTrigger) GroupMemberCommandEventTrigger(org.eclipse.smarthome.model.rule.rules.GroupMemberCommandEventTrigger) CommandEventTrigger(org.eclipse.smarthome.model.rule.rules.CommandEventTrigger)

Example 97 with State

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

the class TestPersistenceService method query.

@SuppressWarnings("deprecation")
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    int startValue = 1950;
    int endValue = 2012;
    if (filter.getBeginDateZoned() != null) {
        startValue = filter.getBeginDateZoned().getYear();
    }
    if (filter.getEndDateZoned() != null) {
        endValue = filter.getEndDateZoned().getYear();
    }
    if (endValue <= startValue || startValue < 1950) {
        return Collections.emptyList();
    }
    ArrayList<HistoricItem> results = new ArrayList<HistoricItem>(endValue - startValue);
    for (int i = startValue; i <= endValue; i++) {
        final int year = i;
        results.add(new HistoricItem() {

            @Override
            public Date getTimestamp() {
                return new Date(year - 1900, 0, 1);
            }

            @Override
            public State getState() {
                return new DecimalType(year);
            }

            @Override
            public String getName() {
                return "Test";
            }
        });
    }
    if (filter.getOrdering() == Ordering.DESCENDING) {
        Collections.reverse(results);
    }
    return results;
}
Also used : State(org.eclipse.smarthome.core.types.State) ArrayList(java.util.ArrayList) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Date(java.util.Date)

Example 98 with State

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

the class PersistenceExtensions method sumSince.

/**
 * Gets the sum of the state of a given <code>item</code> since a certain point in time.
 * The {@link PersistenceService} identified by the <code>serviceId</code> is used.
 *
 * @param item the item for which we will sum its persisted state values since <code>timestamp</code>
 * @param timestamp the point in time from which to start the summation
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return the sum of the state values since the given point in time, or {@link DecimalType.ZERO} if no historic
 *         states could be found for the <code>item</code> or if <code>serviceId</code> does no refer to a
 *         {@link QueryablePersistenceService}
 */
public static DecimalType sumSince(Item item, AbstractInstant timestamp, String serviceId) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
    Iterator<HistoricItem> it = result.iterator();
    BigDecimal sum = BigDecimal.ZERO;
    while (it.hasNext()) {
        State state = it.next().getState();
        if (state instanceof DecimalType) {
            sum = sum.add(((DecimalType) state).toBigDecimal());
        }
    }
    return new DecimalType(sum);
}
Also used : State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) BigDecimal(java.math.BigDecimal)

Example 99 with State

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

the class PersistenceExtensions method maximumSince.

/**
 * Gets the historic item with the maximum value of the state of a given <code>item</code> since
 * a certain point in time. The {@link PersistenceService} identified by the <code>serviceId</code> is used.
 *
 * @param item the item to get the maximum state value for
 * @param timestamp the point in time to start the check
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return a {@link HistoricItem} with the maximum state value since the given point in time, or a
 *         {@link HistoricItem} constructed from the <code>item</code>'s state if <code>item</code>'s state is the
 *         maximum value or if the given <code>serviceId</code> does not refer to an available
 *         {@link QueryablePersistenceService}
 */
public static HistoricItem maximumSince(final Item item, AbstractInstant timestamp, String serviceId) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
    Iterator<HistoricItem> it = result.iterator();
    HistoricItem maximumHistoricItem = null;
    DecimalType maximum = (DecimalType) item.getStateAs(DecimalType.class);
    while (it.hasNext()) {
        HistoricItem historicItem = it.next();
        State state = historicItem.getState();
        if (state instanceof DecimalType) {
            DecimalType value = (DecimalType) state;
            if (maximum == null || value.compareTo(maximum) > 0) {
                maximum = value;
                maximumHistoricItem = historicItem;
            }
        }
    }
    if (maximumHistoricItem == null && maximum != null) {
        // the maximum state is the current one, so construct a historic item on the fly
        final DecimalType state = maximum;
        return new HistoricItem() {

            @Override
            public Date getTimestamp() {
                return Calendar.getInstance().getTime();
            }

            @Override
            public State getState() {
                return state;
            }

            @Override
            public String getName() {
                return item.getName();
            }
        };
    } else {
        return maximumHistoricItem;
    }
}
Also used : State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem)

Example 100 with State

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

the class PersistenceExtensions method averageSince.

/**
 * Gets the average value of the state of a given <code>item</code> since a certain point in time.
 * The {@link PersistenceService} identified by the <code>serviceId</code> is used.
 *
 * @param item the item to get the average state value for
 * @param timestamp the point in time from which to search for the average state value
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return the average state values since <code>timestamp</code>, or the state of the given <code>item</code> if no
 *         previous states could be found or if the persistence service given by <code>serviceId</code> does not
 *         refer to an available {@link QueryablePersistenceService}
 */
public static DecimalType averageSince(Item item, AbstractInstant timestamp, String serviceId) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
    Iterator<HistoricItem> it = result.iterator();
    BigDecimal total = BigDecimal.ZERO;
    BigDecimal avgValue, timeSpan;
    DecimalType lastState = null, thisState = null;
    BigDecimal lastTimestamp = null, thisTimestamp = null;
    BigDecimal firstTimestamp = null;
    while (it.hasNext()) {
        HistoricItem thisItem = it.next();
        State state = thisItem.getState();
        if (state instanceof DecimalType) {
            thisState = (DecimalType) state;
            thisTimestamp = BigDecimal.valueOf(thisItem.getTimestamp().getTime());
            if (firstTimestamp == null) {
                firstTimestamp = thisTimestamp;
            } else {
                avgValue = (thisState.toBigDecimal().add(lastState.toBigDecimal())).divide(BigDecimal.valueOf(2), MathContext.DECIMAL64);
                timeSpan = thisTimestamp.subtract(lastTimestamp);
                total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64));
            }
            lastTimestamp = thisTimestamp;
            lastState = thisState;
        }
    }
    if (lastState != null) {
        thisState = (DecimalType) item.getStateAs(DecimalType.class);
        thisTimestamp = BigDecimal.valueOf((new DateTime()).getMillis());
        avgValue = (thisState.toBigDecimal().add(lastState.toBigDecimal())).divide(BigDecimal.valueOf(2), MathContext.DECIMAL64);
        timeSpan = thisTimestamp.subtract(lastTimestamp);
        total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64));
    }
    if (thisTimestamp != null) {
        timeSpan = thisTimestamp.subtract(firstTimestamp, MathContext.DECIMAL64);
        BigDecimal average = total.divide(timeSpan, MathContext.DECIMAL64);
        return new DecimalType(average);
    } else {
        return null;
    }
}
Also used : State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) BigDecimal(java.math.BigDecimal) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime)

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