Search in sources :

Example 16 with HistoricItem

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

the class JdbcBaseDAO method doGetHistItemFilterQuery.

public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount, String table, String name) {
    String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name);
    logger.debug("JDBC::doGetHistItemFilterQuery sql={}", sql);
    List<Object[]> m = Yank.queryObjectArrays(sql, null);
    List<HistoricItem> items = new ArrayList<HistoricItem>();
    for (int i = 0; i < m.size(); i++) {
        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 17 with HistoricItem

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

the class DynamoDBPersistenceService method query.

/**
     * {@inheritDoc}
     */
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    logger.debug("got a query");
    if (!isProperlyConfigured) {
        logger.warn("Configuration for dynamodb not yet loaded or broken. Not storing item.");
        return Collections.emptyList();
    }
    if (!maybeConnectAndCheckConnection()) {
        logger.warn("DynamoDB not connected. Not storing item.");
        return Collections.emptyList();
    }
    String itemName = filter.getItemName();
    Item item = getItemFromRegistry(itemName);
    if (item == null) {
        logger.warn("Could not get item {} from registry!", itemName);
        return Collections.emptyList();
    }
    Class<DynamoDBItem<?>> dtoClass = AbstractDynamoDBItem.getDynamoItemClass(item.getClass());
    String tableName = tableNameResolver.fromClass(dtoClass);
    DynamoDBMapper mapper = getDBMapper(tableName);
    logger.debug("item {} (class {}) will be tried to query using dto class {} from table {}", itemName, item.getClass(), dtoClass, tableName);
    List<HistoricItem> historicItems = new ArrayList<HistoricItem>();
    DynamoDBQueryExpression<DynamoDBItem<?>> queryExpression = createQueryExpression(dtoClass, filter);
    @SuppressWarnings("rawtypes") final PaginatedQueryList<? extends DynamoDBItem> paginatedList;
    try {
        paginatedList = mapper.query(dtoClass, queryExpression);
    } catch (AmazonServiceException e) {
        logger.error("DynamoDB query raised unexpected exception: {}. Returning empty collection. " + "Status code 400 (resource not found) might occur if table was just created.", e.getMessage());
        return Collections.emptyList();
    }
    for (int itemIndexOnPage = 0; itemIndexOnPage < filter.getPageSize(); itemIndexOnPage++) {
        int itemIndex = filter.getPageNumber() * filter.getPageSize() + itemIndexOnPage;
        DynamoDBItem<?> dynamoItem;
        try {
            dynamoItem = paginatedList.get(itemIndex);
        } catch (IndexOutOfBoundsException e) {
            logger.debug("Index {} is out-of-bounds", itemIndex);
            break;
        }
        if (dynamoItem != null) {
            HistoricItem historicItem = dynamoItem.asHistoricItem(item);
            logger.trace("Dynamo item {} converted to historic item: {}", item, historicItem);
            historicItems.add(historicItem);
        }
    }
    return historicItems;
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) ArrayList(java.util.ArrayList) HistoricItem(org.openhab.core.persistence.HistoricItem) Item(org.openhab.core.items.Item) AmazonServiceException(com.amazonaws.AmazonServiceException) HistoricItem(org.openhab.core.persistence.HistoricItem)

Example 18 with HistoricItem

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

the class AbstractTwoItemIntegrationTest method testQueryUsingNameAndStartAndEndWithGTOperator.

@Test
public void testQueryUsingNameAndStartAndEndWithGTOperator() {
    // Skip for subclasses which have null "state between"
    Assume.assumeTrue(getQueryItemStateBetween() != null);
    FilterCriteria criteria = new FilterCriteria();
    criteria.setOperator(Operator.GT);
    criteria.setState(getQueryItemStateBetween());
    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)

Example 19 with HistoricItem

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

the class AbstractTwoItemIntegrationTest method testQueryUsingNameAndStartAndEnd.

@Test
public void testQueryUsingNameAndStartAndEnd() {
    FilterCriteria criteria = new FilterCriteria();
    criteria.setOrdering(Ordering.ASCENDING);
    criteria.setItemName(getItemName());
    criteria.setBeginDate(beforeStore);
    criteria.setEndDate(afterStore2);
    Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
    assertIterableContainsItems(iterable, true);
}
Also used : FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) Test(org.junit.Test)

Example 20 with HistoricItem

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

the class AbstractTwoItemIntegrationTest method testQueryUsingName.

@Test
public void testQueryUsingName() {
    FilterCriteria criteria = new FilterCriteria();
    criteria.setOrdering(Ordering.ASCENDING);
    criteria.setItemName(getItemName());
    Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
    assertIterableContainsItems(iterable, true);
}
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