Search in sources :

Example 56 with DecimalType

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

the class PersistenceExtensions method maximumSince.

/**
 * Gets the historic item with the maximum 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 maximum state value for
 * @param timestamp the point in time to start the check
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return a {@link HistoricItem} with the maximum 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
 *         maximum value or if the given <code>serviceId</code> does not refer to an available
 *         {@link QueryablePersistenceService}
 */
public static HistoricItem maximumSince(final Item item, AbstractInstant timestamp, String serviceId) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
    Iterator<HistoricItem> it = result.iterator();
    HistoricItem maximumHistoricItem = null;
    DecimalType maximum = (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 (maximum == null || value.compareTo(maximum) > 0) {
                maximum = value;
                maximumHistoricItem = historicItem;
            }
        }
    }
    if (maximumHistoricItem == null && maximum != null) {
        // the maximum state is the current one, so construct a historic item on the fly
        final DecimalType state = maximum;
        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 maximumHistoricItem;
    }
}
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 57 with DecimalType

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

the class PersistenceExtensions method averageSince.

/**
 * Gets the average 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 search for the average state value
 * @param serviceId the name of the {@link PersistenceService} to use
 * @return the average state values since <code>timestamp</code>, or the state of the given <code>item</code> if no
 *         previous states could be found or if the persistence service given by <code>serviceId</code> does not
 *         refer to an available {@link QueryablePersistenceService}
 */
public static DecimalType averageSince(Item item, AbstractInstant timestamp, String serviceId) {
    Iterable<HistoricItem> result = getAllStatesSince(item, timestamp, serviceId);
    Iterator<HistoricItem> it = result.iterator();
    BigDecimal total = BigDecimal.ZERO;
    BigDecimal avgValue, timeSpan;
    DecimalType lastState = null, thisState = null;
    BigDecimal lastTimestamp = null, thisTimestamp = null;
    BigDecimal firstTimestamp = null;
    while (it.hasNext()) {
        HistoricItem thisItem = it.next();
        State state = thisItem.getState();
        if (state instanceof DecimalType) {
            thisState = (DecimalType) state;
            thisTimestamp = BigDecimal.valueOf(thisItem.getTimestamp().getTime());
            if (firstTimestamp == null) {
                firstTimestamp = thisTimestamp;
            } else {
                avgValue = (thisState.toBigDecimal().add(lastState.toBigDecimal())).divide(BigDecimal.valueOf(2), MathContext.DECIMAL64);
                timeSpan = thisTimestamp.subtract(lastTimestamp);
                total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64));
            }
            lastTimestamp = thisTimestamp;
            lastState = thisState;
        }
    }
    if (lastState != null) {
        thisState = (DecimalType) item.getStateAs(DecimalType.class);
        thisTimestamp = BigDecimal.valueOf((new DateTime()).getMillis());
        avgValue = (thisState.toBigDecimal().add(lastState.toBigDecimal())).divide(BigDecimal.valueOf(2), MathContext.DECIMAL64);
        timeSpan = thisTimestamp.subtract(lastTimestamp);
        total = total.add(avgValue.multiply(timeSpan, MathContext.DECIMAL64));
    }
    if (thisTimestamp != null) {
        timeSpan = thisTimestamp.subtract(firstTimestamp, MathContext.DECIMAL64);
        BigDecimal average = total.divide(timeSpan, MathContext.DECIMAL64);
        return new DecimalType(average);
    } else {
        return null;
    }
}
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) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime)

Example 58 with DecimalType

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

the class NumberExtensionsTest method testOperator_greaterThanTypeNumber.

/**
 * Test method for
 * {@link org.eclipse.smarthome.model.script.lib.NumberExtensions#operator_greaterThan(org.eclipse.smarthome.core.types.Type, java.lang.Number)}
 * .
 */
@Test
public void testOperator_greaterThanTypeNumber() {
    DecimalType type = new DecimalType(10);
    Number x = 123;
    boolean result = NumberExtensions.operator_greaterThan((Type) type, x);
    Assert.assertFalse(result);
    x = 2;
    result = NumberExtensions.operator_greaterThan((Type) type, x);
    Assert.assertTrue(result);
}
Also used : Type(org.eclipse.smarthome.core.types.Type) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) Test(org.junit.Test)

Example 59 with DecimalType

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

the class ScriptEngineOSGiTest method testGreaterThan_Number_Number.

@Test
public void testGreaterThan_Number_Number() throws ScriptParsingException, ScriptExecutionException {
    Item numberItem = itemRegistry.get(NUMBER_ITEM_DECIMAL);
    ((NumberItem) numberItem).setState(new DecimalType(20));
    assertTrue(runScript("NumberB.state > new DecimalType(19)"));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Example 60 with DecimalType

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

the class ColorThingHandler method handleCommand.

@Override
public void handleCommand(ChannelUID channelUID, Command command) {
    logger.trace("received command {} in channel {}", command, channelUID);
    ValueSet targetValueSet = new ValueSet(fadeTime, -1);
    switch(channelUID.getId()) {
        case CHANNEL_BRIGHTNESS_R:
            if (command instanceof RefreshType) {
                logger.trace("sending update on refresh to channel {}:brightness_r", this.thing.getUID());
                currentValues.set(0, channels.get(0).getValue());
                updateCurrentColor();
                updateState(channelUID, Util.toPercentValue(currentValues.get(0)));
                return;
            } else {
                logger.debug("command {} not supported in channel {}:brightness_r", command.getClass(), this.thing.getUID());
                return;
            }
        case CHANNEL_BRIGHTNESS_G:
            if (command instanceof RefreshType) {
                logger.trace("sending update on refresh to channel {}:brightness_g", this.thing.getUID());
                currentValues.set(1, channels.get(1).getValue());
                updateCurrentColor();
                updateState(channelUID, Util.toPercentValue(currentValues.get(1)));
                return;
            } else {
                logger.debug("command {} not supported in channel {}:brightness_g", command.getClass(), this.thing.getUID());
                return;
            }
        case CHANNEL_BRIGHTNESS_B:
            if (command instanceof RefreshType) {
                logger.trace("sending update on refresh to channel {}:brightness_b", this.thing.getUID());
                currentValues.set(2, channels.get(2).getValue());
                updateCurrentColor();
                updateState(channelUID, Util.toPercentValue(currentValues.get(2)));
                return;
            } else {
                logger.debug("command {} not supported in channel {}:brightness_b", command.getClass(), this.thing.getUID());
                return;
            }
        case CHANNEL_COLOR:
            {
                if (command instanceof OnOffType) {
                    logger.trace("adding {} fade to channels in thing {}", command, this.thing.getUID());
                    targetValueSet = ((OnOffType) command).equals(OnOffType.ON) ? turnOnValue : turnOffValue;
                } else if (command instanceof HSBType) {
                    logger.trace("adding color fade to channels in thing {}", this.thing.getUID());
                    targetValueSet.addValue(((HSBType) command).getRed());
                    targetValueSet.addValue(((HSBType) command).getGreen());
                    targetValueSet.addValue(((HSBType) command).getBlue());
                } else if ((command instanceof PercentType) || (command instanceof DecimalType)) {
                    logger.trace("adding brightness fade to channels in thing {}", this.thing.getUID());
                    PercentType brightness = (command instanceof PercentType) ? (PercentType) command : Util.toPercentValue(((DecimalType) command).intValue());
                    HSBType targetColor = new HSBType(currentColor.getHue(), currentColor.getSaturation(), brightness);
                    targetValueSet.addValue(targetColor.getRed());
                    targetValueSet.addValue(targetColor.getGreen());
                    targetValueSet.addValue(targetColor.getBlue());
                } else if (command instanceof IncreaseDecreaseType) {
                    if (isDimming && ((IncreaseDecreaseType) command).equals(IncreaseDecreaseType.INCREASE)) {
                        logger.trace("stopping fade in thing {}", this.thing.getUID());
                        channels.forEach(DmxChannel::clearAction);
                        isDimming = false;
                        return;
                    } else {
                        logger.trace("starting {} fade in thing {}", command, this.thing.getUID());
                        HSBType targetColor;
                        if (((IncreaseDecreaseType) command).equals(IncreaseDecreaseType.INCREASE)) {
                            targetColor = new HSBType(currentColor.getHue(), currentColor.getSaturation(), PercentType.HUNDRED);
                        } else {
                            targetColor = new HSBType(currentColor.getHue(), currentColor.getSaturation(), PercentType.ZERO);
                        }
                        targetValueSet.addValue(targetColor.getRed());
                        targetValueSet.addValue(targetColor.getGreen());
                        targetValueSet.addValue(targetColor.getBlue());
                        targetValueSet.setFadeTime(dimTime);
                        isDimming = true;
                    }
                } else if (command instanceof RefreshType) {
                    logger.trace("sending update on refresh to channel {}:color", this.thing.getUID());
                    currentValues.set(0, channels.get(0).getValue());
                    currentValues.set(1, channels.get(1).getValue());
                    currentValues.set(2, channels.get(2).getValue());
                    updateCurrentColor();
                    updateState(channelUID, currentColor);
                    return;
                } else {
                    logger.debug("command {} not supported in channel {}:color", command.getClass(), this.thing.getUID());
                    return;
                }
                break;
            }
        default:
            logger.debug("channel {} not supported in thing {}", channelUID.getId(), this.thing.getUID());
            return;
    }
    final ValueSet valueSet = targetValueSet;
    IntStream.range(0, channels.size()).forEach(i -> {
        channels.get(i).setChannelAction(new FadeAction(valueSet.getFadeTime(), channels.get(i).getValue(), valueSet.getValue(i), valueSet.getHoldTime()));
    });
}
Also used : FadeAction(org.eclipse.smarthome.binding.dmx.internal.action.FadeAction) BaseDmxChannel(org.eclipse.smarthome.binding.dmx.internal.multiverse.BaseDmxChannel) DmxChannel(org.eclipse.smarthome.binding.dmx.internal.multiverse.DmxChannel) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) IncreaseDecreaseType(org.eclipse.smarthome.core.library.types.IncreaseDecreaseType) PercentType(org.eclipse.smarthome.core.library.types.PercentType) RefreshType(org.eclipse.smarthome.core.types.RefreshType) ValueSet(org.eclipse.smarthome.binding.dmx.internal.ValueSet) 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