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();
}
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;
}
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;
}
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));
}
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));
}
Aggregations