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