Search in sources :

Example 6 with HistoricItem

use of org.openhab.core.persistence.HistoricItem in project openhab1-addons by openhab.

the class Db4oPersistenceService method query.

@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    Query query = queryWithReconnect();
    if (query != null) {
        query.constrain(Db4oItem.class);
        if (filter == null) {
            filter = new FilterCriteria();
        }
        if (filter.getBeginDate() != null) {
            query.descend("timestamp").constrain(filter.getBeginDate()).greater().equal();
        }
        if (filter.getEndDate() != null) {
            query.descend("timestamp").constrain(filter.getEndDate()).smaller().equal();
        }
        if (filter.getItemName() != null) {
            query.descend("name").constrain(filter.getItemName()).equal();
        }
        if (filter.getState() != null && filter.getOperator() != null) {
            switch(filter.getOperator()) {
                case EQ:
                    query.descend("state").constrain(filter.getState()).equal();
                    break;
                case GT:
                    query.descend("state").constrain(filter.getState()).greater();
                    break;
                case LT:
                    query.descend("state").constrain(filter.getState()).smaller();
                    break;
                case NEQ:
                    query.descend("state").constrain(filter.getState()).equal().not();
                    break;
                case GTE:
                    query.descend("state").constrain(filter.getState()).greater().equal();
                    break;
                case LTE:
                    query.descend("state").constrain(filter.getState()).smaller().equal();
                    break;
            }
        }
        if (filter.getOrdering() == Ordering.ASCENDING) {
            query.descend("timestamp").orderAscending();
        } else {
            query.descend("timestamp").orderDescending();
        }
        ObjectSet<HistoricItem> results = query.execute();
        int startIndex = filter.getPageNumber() * filter.getPageSize();
        if (startIndex < results.size()) {
            int endIndex = startIndex + filter.getPageSize();
            if (endIndex > results.size()) {
                endIndex = results.size();
            }
            return results.subList(startIndex, endIndex);
        }
    }
    return Collections.emptyList();
}
Also used : Query(com.db4o.query.Query) FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem)

Example 7 with HistoricItem

use of org.openhab.core.persistence.HistoricItem in project openhab1-addons by openhab.

the class JdbcDerbyDAO method doGetHistItemFilterQuery.

@Override
public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount, String table, String name) {
    String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name);
    List<Object[]> m = Yank.queryObjectArrays(sql, null);
    logger.debug("JDBC::doGetHistItemFilterQuery got Array length={}", m.size());
    List<HistoricItem> items = new ArrayList<HistoricItem>();
    for (int i = 0; i < m.size(); i++) {
        logger.debug("JDBC::doGetHistItemFilterQuery 0='{}' 1='{}'", m.get(i)[0], m.get(i)[1]);
        items.add(new JdbcItem(item.getName(), getState(item, m.get(i)[1]), objectAsDate(m.get(i)[0])));
    }
    return items;
}
Also used : ArrayList(java.util.ArrayList) JdbcItem(org.openhab.persistence.jdbc.model.JdbcItem) HistoricItem(org.openhab.core.persistence.HistoricItem)

Example 8 with HistoricItem

use of org.openhab.core.persistence.HistoricItem in project openhab1-addons by openhab.

the class AbstractDynamoDBItemSerializationTest method testAsHistoricGeneric.

/**
     * Test state deserialization, that is DynamoDBItem conversion to HistoricItem
     *
     * @param dbItem dynamo db item
     * @param item parameter for DynamoDBItem.asHistoricItem
     * @param expectedState Expected state of the historic item. DecimalTypes are compared with reduced accuracy
     * @return
     * @throws IOException
     */
public HistoricItem testAsHistoricGeneric(DynamoDBItem<?> dbItem, Item item, Object expectedState) throws IOException {
    HistoricItem historicItem = dbItem.asHistoricItem(item);
    assertEquals("item1", historicItem.getName());
    assertEquals(date, historicItem.getTimestamp());
    assertEquals(expectedState.getClass(), historicItem.getState().getClass());
    if (expectedState instanceof DecimalType) {
        // serialization loses accuracy, take this into consideration
        assertTrue(DynamoDBBigDecimalItem.loseDigits(((DecimalType) expectedState).toBigDecimal()).compareTo(((DecimalType) historicItem.getState()).toBigDecimal()) == 0);
    } else if (expectedState instanceof CallType) {
        // CallType has buggy equals, let's compare strings instead
        assertEquals(expectedState.toString(), historicItem.getState().toString());
    } else {
        assertEquals(expectedState, historicItem.getState());
    }
    return historicItem;
}
Also used : CallType(org.openhab.library.tel.types.CallType) DecimalType(org.openhab.core.library.types.DecimalType) HistoricItem(org.openhab.core.persistence.HistoricItem)

Example 9 with HistoricItem

use of org.openhab.core.persistence.HistoricItem in project openhab1-addons by openhab.

the class AbstractTwoItemIntegrationTest method testQueryUsingNameAndStartAndEndWithNEQOperator.

@Test
public void testQueryUsingNameAndStartAndEndWithNEQOperator() {
    FilterCriteria criteria = new FilterCriteria();
    criteria.setOperator(Operator.NEQ);
    criteria.setState(getSecondItemState());
    criteria.setItemName(getItemName());
    criteria.setBeginDate(beforeStore);
    criteria.setEndDate(afterStore2);
    Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
    Iterator<HistoricItem> iterator = iterable.iterator();
    HistoricItem actual1 = iterator.next();
    assertFalse(iterator.hasNext());
    assertStateEquals(getFirstItemState(), actual1.getState());
    assertTrue(actual1.getTimestamp().before(afterStore1));
    assertTrue(actual1.getTimestamp().after(beforeStore));
}
Also used : FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) Test(org.junit.Test)

Example 10 with HistoricItem

use of org.openhab.core.persistence.HistoricItem in project openhab1-addons by openhab.

the class AbstractTwoItemIntegrationTest method testQueryUsingNameAndStartAndEndWithGTEOperator.

@Test
public void testQueryUsingNameAndStartAndEndWithGTEOperator() {
    FilterCriteria criteria = new FilterCriteria();
    criteria.setOperator(Operator.GTE);
    criteria.setState(getSecondItemState());
    criteria.setItemName(getItemName());
    criteria.setBeginDate(beforeStore);
    criteria.setEndDate(afterStore2);
    Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
    Iterator<HistoricItem> iterator = iterable.iterator();
    HistoricItem actual1 = iterator.next();
    assertFalse(iterator.hasNext());
    assertStateEquals(getSecondItemState(), actual1.getState());
    assertTrue(actual1.getTimestamp().before(afterStore2));
    assertTrue(actual1.getTimestamp().after(afterStore1));
}
Also used : FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) Test(org.junit.Test)

Aggregations

HistoricItem (org.openhab.core.persistence.HistoricItem)34 FilterCriteria (org.openhab.core.persistence.FilterCriteria)19 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)12 Item (org.openhab.core.items.Item)6 State (org.openhab.core.types.State)5 Date (java.util.Date)4 ItemNotFoundException (org.openhab.core.items.ItemNotFoundException)4 DecimalType (org.openhab.core.library.types.DecimalType)4 JdbcItem (org.openhab.persistence.jdbc.model.JdbcItem)3 Calendar (java.util.Calendar)2 GroupItem (org.openhab.core.items.GroupItem)2 ColorItem (org.openhab.core.library.items.ColorItem)2 ContactItem (org.openhab.core.library.items.ContactItem)2 DateTimeItem (org.openhab.core.library.items.DateTimeItem)2 DimmerItem (org.openhab.core.library.items.DimmerItem)2 NumberItem (org.openhab.core.library.items.NumberItem)2 RollershutterItem (org.openhab.core.library.items.RollershutterItem)2 SwitchItem (org.openhab.core.library.items.SwitchItem)2 DateTimeType (org.openhab.core.library.types.DateTimeType)2