Search in sources :

Example 31 with DecimalType

use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.

the class PersistenceExtensionsTest method testPreviousStateSkip.

@Test
public void testPreviousStateSkip() {
    item.setState(new DecimalType(2012));
    HistoricItem prevStateItem = PersistenceExtensions.previousState(item, true, "test");
    assertNotNull(prevStateItem);
    assertEquals("2011", 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 32 with DecimalType

use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.

the class PersistenceExtensionsTest method testMaximumSince.

@Test
public void testMaximumSince() {
    item.setState(new DecimalType(1));
    HistoricItem historicItem = PersistenceExtensions.maximumSince(item, new DateMidnight(2012, 1, 1), "test");
    assertNotNull(historicItem);
    assertEquals("1", historicItem.getState().toString());
    historicItem = PersistenceExtensions.maximumSince(item, new DateMidnight(2005, 1, 1), "test");
    assertEquals("2012", historicItem.getState().toString());
    assertEquals(new DateMidnight(2012, 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 33 with DecimalType

use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.

the class PersistenceExtensions method minimumSince.

/**
 * Gets the historic item with the minimum 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 minimum state value for
 * @param timestamp the point in time from which to search for the minimum state value
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return the historic item with the minimum state value since the given point in time, or a {@link HistoricItem}
 *         constructed from the <code>item</code>'s state if <code>item</code>'s state is the minimum value or if
 *         the given <code>serviceId</code> does not refer to an available {@link QueryablePersistenceService}
 */
public static HistoricItem minimumSince(final Item item, AbstractInstant timestamp, String serviceId) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
    Iterator<HistoricItem> it = result.iterator();
    HistoricItem minimumHistoricItem = null;
    DecimalType minimum = (DecimalType) item.getStateAs(DecimalType.class);
    while (it.hasNext()) {
        HistoricItem historicItem = it.next();
        State state = historicItem.getState();
        if (state instanceof DecimalType) {
            DecimalType value = (DecimalType) state;
            if (minimum == null || value.compareTo(minimum) < 0) {
                minimum = value;
                minimumHistoricItem = historicItem;
            }
        }
    }
    if (minimumHistoricItem == null && minimum != null) {
        // the minimal state is the current one, so construct a historic item on the fly
        final DecimalType state = minimum;
        return new HistoricItem() {

            @Override
            public Date getTimestamp() {
                return Calendar.getInstance().getTime();
            }

            @Override
            public State getState() {
                return state;
            }

            @Override
            public String getName() {
                return item.getName();
            }
        };
    } else {
        return minimumHistoricItem;
    }
}
Also used : State(org.eclipse.smarthome.core.types.State) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem)

Example 34 with DecimalType

use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.

the class PersistenceExtensions method evolutionRate.

/**
 * Gets the evolution rate 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 evolution rate value for
 * @param timestamp the point in time from which to compute the evolution rate
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return the evolution rate in percent (positive and negative) between now and then, or <code>null</code> if
 *         the persistence service given by serviceId is not available or is not a
 *         {@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 given by
 *         <code>serviceId</code>, or if there is a state but it is zero (which would cause a divide-by-zero
 *         error)
 */
public static DecimalType evolutionRate(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) && (valueThen.toBigDecimal().compareTo(BigDecimal.ZERO) != 0) && (valueNow != null)) {
            // ((now - then) / then) * 100
            return new DecimalType(valueNow.toBigDecimal().subtract(valueThen.toBigDecimal()).divide(valueThen.toBigDecimal(), MathContext.DECIMAL64).movePointRight(2));
        }
    }
    return null;
}
Also used : DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) HistoricItem(org.eclipse.smarthome.core.persistence.HistoricItem)

Example 35 with DecimalType

use of org.eclipse.smarthome.core.library.types.DecimalType in project smarthome by eclipse.

the class LightStateConverter method toHSBType.

/**
 * Transforms Hue Light {@link State} into {@link HSBType} representing the
 * color.
 *
 * @param lightState light state
 * @return HSB type representing the color
 */
public static HSBType toHSBType(State lightState) {
    int hue = lightState.getHue();
    int saturationInPercent = (int) (lightState.getSaturation() / SATURATION_FACTOR);
    int brightnessInPercent = (int) (lightState.getBrightness() / BRIGHTNESS_FACTOR);
    saturationInPercent = restrictToBounds(saturationInPercent);
    brightnessInPercent = restrictToBounds(brightnessInPercent);
    return new HSBType(new DecimalType(hue / HUE_FACTOR), new PercentType(saturationInPercent), new PercentType(brightnessInPercent));
}
Also used : DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) HSBType(org.eclipse.smarthome.core.library.types.HSBType)

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