Search in sources :

Example 11 with HistoricItem

use of org.eclipse.smarthome.core.persistence.HistoricItem in project smarthome by eclipse.

the class PersistenceExtensionsTest method testPreviousStateNoSkip.

@Test
public void testPreviousStateNoSkip() {
    item.setState(new DecimalType(4321));
    HistoricItem prevStateItem = PersistenceExtensions.previousState(item, false, "test");
    assertNotNull(prevStateItem);
    assertEquals("2012", prevStateItem.getState().toString());
    item.setState(new DecimalType(2012));
    prevStateItem = PersistenceExtensions.previousState(item, false, "test");
    assertNotNull(prevStateItem);
    assertEquals("2012", prevStateItem.getState().toString());
}
Also used : DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Test(org.junit.Test)

Example 12 with HistoricItem

use of org.eclipse.smarthome.core.persistence.HistoricItem in project smarthome by eclipse.

the class PersistenceExtensionsTest method testHistoricState.

@Test
public void testHistoricState() {
    HistoricItem historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2012, 1, 1), "test");
    assertEquals("2012", historicItem.getState().toString());
    historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2011, 12, 31), "test");
    assertEquals("2011", historicItem.getState().toString());
    historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2011, 1, 1), "test");
    assertEquals("2011", historicItem.getState().toString());
    historicItem = PersistenceExtensions.historicState(item, new DateMidnight(2000, 1, 1), "test");
    assertEquals("2000", historicItem.getState().toString());
}
Also used : DateMidnight(org.joda.time.DateMidnight) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Test(org.junit.Test)

Example 13 with HistoricItem

use of org.eclipse.smarthome.core.persistence.HistoricItem in project smarthome by eclipse.

the class PersistenceExtensionsTest method testMinimumSince.

@Test
public void testMinimumSince() {
    item.setState(new DecimalType(5000));
    HistoricItem historicItem = PersistenceExtensions.minimumSince(item, new DateMidnight(1940, 1, 1), "test");
    assertNotNull(historicItem);
    assertEquals("5000", historicItem.getState().toString());
    historicItem = PersistenceExtensions.minimumSince(item, new DateMidnight(2005, 1, 1), "test");
    assertEquals("2005", historicItem.getState().toString());
    assertEquals(new DateMidnight(2005, 1, 1).toDate(), historicItem.getTimestamp());
}
Also used : DateMidnight(org.joda.time.DateMidnight) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Test(org.junit.Test)

Example 14 with HistoricItem

use of org.eclipse.smarthome.core.persistence.HistoricItem 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 15 with HistoricItem

use of org.eclipse.smarthome.core.persistence.HistoricItem in project smarthome by eclipse.

the class PersistenceExtensions method previousState.

/**
 * Returns the previous state of a given <code>item</code>.
 * The {@link PersistenceService} identified by the <code>serviceId</code> is used.
 *
 * @param item the item to get the previous state value for
 * @param skipEqual if <code>true</code>, skips equal state values and searches the first state not equal the
 *            current state
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return the previous state or <code>null</code> if no previous state could be found, or if the given
 *         <code>serviceId</code> is not available or does not refer to a {@link QueryablePersistenceService}
 */
public static HistoricItem previousState(Item item, boolean skipEqual, String serviceId) {
    PersistenceService service = getService(serviceId);
    if (service instanceof QueryablePersistenceService) {
        QueryablePersistenceService qService = (QueryablePersistenceService) service;
        FilterCriteria filter = new FilterCriteria();
        filter.setItemName(item.getName());
        filter.setOrdering(Ordering.DESCENDING);
        filter.setPageSize(skipEqual ? 1000 : 1);
        int startPage = 0;
        filter.setPageNumber(startPage);
        Iterable<HistoricItem> items = qService.query(filter);
        while (items != null) {
            Iterator<HistoricItem> itemIterator = items.iterator();
            int itemCount = 0;
            while (itemIterator.hasNext()) {
                HistoricItem historicItem = itemIterator.next();
                itemCount++;
                if (!skipEqual || (skipEqual && !historicItem.getState().equals(item.getState()))) {
                    return historicItem;
                }
            }
            if (itemCount == filter.getPageSize()) {
                filter.setPageNumber(++startPage);
                items = qService.query(filter);
            } else {
                items = null;
            }
        }
        return null;
    } else {
        LoggerFactory.getLogger(PersistenceExtensions.class).warn("There is no queryable persistence service registered with the id '{}'", serviceId);
        return null;
    }
}
Also used : QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) PersistenceService(org.eclipse.smarthome.core.persistence.PersistenceService) QueryablePersistenceService(org.eclipse.smarthome.core.persistence.QueryablePersistenceService) FilterCriteria(org.eclipse.smarthome.core.persistence.FilterCriteria) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem)

Aggregations

HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)20 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)11 State (org.eclipse.smarthome.core.types.State)9 FilterCriteria (org.eclipse.smarthome.core.persistence.FilterCriteria)6 PersistenceService (org.eclipse.smarthome.core.persistence.PersistenceService)5 QueryablePersistenceService (org.eclipse.smarthome.core.persistence.QueryablePersistenceService)5 Test (org.junit.Test)5 ZonedDateTime (java.time.ZonedDateTime)3 Date (java.util.Date)3 DateMidnight (org.joda.time.DateMidnight)3 BigDecimal (java.math.BigDecimal)2 ArrayList (java.util.ArrayList)2 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)2 OpenClosedType (org.eclipse.smarthome.core.library.types.OpenClosedType)2 DateTime (org.joda.time.DateTime)2 BasicStroke (java.awt.BasicStroke)1 Color (java.awt.Color)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Calendar (java.util.Calendar)1