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