use of org.eclipse.smarthome.core.library.types.DecimalType 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.library.types.DecimalType 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.library.types.DecimalType 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.library.types.DecimalType in project smarthome by eclipse.
the class PersistenceExtensions method deltaSince.
/**
* Gets the difference value of the state of a given <code>item</code> since a certain point in time.
* The {@link PersistenceService} identified by the <code>serviceId</code> is used.
*
* @param item the item to get the average state value for
* @param timestamp the point in time from which to compute the delta
* @param serviceId the name of the {@link PersistenceService} to use
* @return the difference between now and then, or <code>null</code> if the given serviceId does not refer to an
* available {@link QueryablePersistenceService}, or if there is no persisted state for the given
* <code>item</code> at the given <code>timestamp</code> using the persistence service named
* <code>serviceId</code>
*/
public static DecimalType deltaSince(Item item, AbstractInstant timestamp, String serviceId) {
HistoricItem itemThen = historicState(item, timestamp, serviceId);
if (itemThen != null) {
DecimalType valueThen = (DecimalType) itemThen.getState();
DecimalType valueNow = (DecimalType) item.getStateAs(DecimalType.class);
if ((valueThen != null) && (valueNow != null)) {
return new DecimalType(valueNow.toBigDecimal().subtract(valueThen.toBigDecimal()));
}
}
return null;
}
use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.
the class PersistenceExtensions method sumSince.
/**
* Gets the sum of the state of a given <code>item</code> since a certain point in time.
* The {@link PersistenceService} identified by the <code>serviceId</code> is used.
*
* @param item the item for which we will sum its persisted state values since <code>timestamp</code>
* @param timestamp the point in time from which to start the summation
* @param serviceId the name of the {@link PersistenceService} to use
* @return the sum of the state values since the given point in time, or {@link DecimalType.ZERO} if no historic
* states could be found for the <code>item</code> or if <code>serviceId</code> does no refer to a
* {@link QueryablePersistenceService}
*/
public static DecimalType sumSince(Item item, AbstractInstant timestamp, String serviceId) {
Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
Iterator<HistoricItem> it = result.iterator();
BigDecimal sum = BigDecimal.ZERO;
while (it.hasNext()) {
State state = it.next().getState();
if (state instanceof DecimalType) {
sum = sum.add(((DecimalType) state).toBigDecimal());
}
}
return new DecimalType(sum);
}
Aggregations