Search in sources :

Example 51 with DecimalType

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());
}
Also used : DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Test(org.junit.Test)

Example 52 with DecimalType

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());
}
Also used : DateMidnight(org.joda.time.DateMidnight) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Test(org.junit.Test)

Example 53 with DecimalType

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;
}
Also used : State(org.eclipse.smarthome.core.types.State) ArrayList(java.util.ArrayList) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) Date(java.util.Date)

Example 54 with DecimalType

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;
}
Also used : DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem)

Example 55 with DecimalType

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);
}
Also used : State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem) BigDecimal(java.math.BigDecimal)

Aggregations

DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)68 Test (org.junit.Test)33 State (org.eclipse.smarthome.core.types.State)17 PercentType (org.eclipse.smarthome.core.library.types.PercentType)16 HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)11 StringType (org.eclipse.smarthome.core.library.types.StringType)9 HSBType (org.eclipse.smarthome.core.library.types.HSBType)8 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)7 Type (org.eclipse.smarthome.core.types.Type)7 Item (org.eclipse.smarthome.core.items.Item)6 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 BigDecimal (java.math.BigDecimal)5 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)4 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)4 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)4 Response (javax.ws.rs.core.Response)3 ValueSet (org.eclipse.smarthome.binding.dmx.internal.ValueSet)3 FadeAction (org.eclipse.smarthome.binding.dmx.internal.action.FadeAction)3 DateTimeType (org.eclipse.smarthome.core.library.types.DateTimeType)3