Search in sources :

Example 1 with QuantityType

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

the class ItemStateConverterImplTest method numberItemShouldNotConvertUnitsWhereMeasurmentSystemEquals.

@Test
public void numberItemShouldNotConvertUnitsWhereMeasurmentSystemEquals() {
    NumberItem item = mock(NumberItem.class);
    doReturn(Length.class).when(item).getDimension();
    UnitProvider unitProvider = mock(UnitProvider.class);
    when(unitProvider.getUnit(Length.class)).thenReturn(SIUnits.METRE);
    itemStateConverter.setUnitProvider(unitProvider);
    QuantityType<Length> originalState = new QuantityType<>("100 cm");
    @SuppressWarnings("unchecked") QuantityType<Length> convertedState = (QuantityType<Length>) itemStateConverter.convertToAcceptedState(originalState, item);
    assertThat(convertedState.getUnit(), is(originalState.getUnit()));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) UnitProvider(org.eclipse.smarthome.core.i18n.UnitProvider) Length(javax.measure.quantity.Length) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) Test(org.junit.Test)

Example 2 with QuantityType

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

the class ItemStateConverterImplTest method numberItemWitDimensionShouldConvertToItemStateDescriptionUnit.

@Test
public void numberItemWitDimensionShouldConvertToItemStateDescriptionUnit() {
    NumberItem item = mock(NumberItem.class);
    StateDescription stateDescription = mock(StateDescription.class);
    when(item.getStateDescription()).thenReturn(stateDescription);
    doReturn(Temperature.class).when(item).getDimension();
    when(stateDescription.getPattern()).thenReturn("%.1f K");
    State originalState = new QuantityType<>("12.34 °C");
    State convertedState = itemStateConverter.convertToAcceptedState(originalState, item);
    assertThat(convertedState, is(new QuantityType<>("285.49 K")));
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) StateDescription(org.eclipse.smarthome.core.types.StateDescription) Test(org.junit.Test)

Example 3 with QuantityType

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

the class SelectionRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    String snippet = getSnippet("selection");
    snippet = preprocessSnippet(snippet, w);
    snippet = StringUtils.replace(snippet, "%value_map%", getMappingsJSON((Selection) w));
    snippet = StringUtils.replace(snippet, "%label_header%", getLabel(w));
    State state = itemUIRegistry.getState(w);
    Selection selection = (Selection) w;
    String mappingLabel = null;
    Item item = null;
    try {
        item = itemUIRegistry.getItem(w.getItem());
    } catch (ItemNotFoundException e) {
        logger.debug("Failed to retrieve item during widget rendering: {}", e.getMessage());
    }
    StringBuilder rowSB = new StringBuilder();
    for (Mapping mapping : selection.getMappings()) {
        String rowSnippet = getSnippet("selection_row");
        String command = mapping.getCmd() != null ? mapping.getCmd() : "";
        String label = mapping.getLabel();
        if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) {
            String unit = getUnitForWidget(w);
            command = StringUtils.replace(command, UnitUtils.UNIT_PLACEHOLDER, unit);
            label = StringUtils.replace(label, UnitUtils.UNIT_PLACEHOLDER, unit);
            // Special treatment for °C since uom library uses a single character: ℃
            // This will ensure the current state matches the cmd and the buttonClass is set accordingly.
            command = StringUtils.replace(command, "°C", "℃");
        }
        rowSnippet = StringUtils.replace(rowSnippet, "%item%", w.getItem() != null ? w.getItem() : "");
        rowSnippet = StringUtils.replace(rowSnippet, "%cmd%", escapeHtml(command));
        rowSnippet = StringUtils.replace(rowSnippet, "%label%", label != null ? escapeHtml(label) : "");
        State compareMappingState = state;
        if (state instanceof QuantityType) {
            // convert the item state to the command value for proper
            // comparison and "checked" attribute calculation
            compareMappingState = convertStateToLabelUnit((QuantityType<?>) state, command);
        }
        if (compareMappingState.toString().equals(command)) {
            mappingLabel = label;
            rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "checked=\"true\"");
        } else {
            rowSnippet = StringUtils.replace(rowSnippet, "%checked%", "");
        }
        rowSB.append(rowSnippet);
    }
    snippet = StringUtils.replace(snippet, "%rows%", rowSB.toString());
    snippet = StringUtils.replace(snippet, "%value_header%", mappingLabel != null ? mappingLabel : "");
    // Process the color tags
    snippet = processColor(w, snippet);
    sb.append(snippet);
    return null;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) Selection(org.eclipse.smarthome.model.sitemap.Selection) Mapping(org.eclipse.smarthome.model.sitemap.Mapping) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 4 with QuantityType

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

the class SwitchRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Switch s = (Switch) w;
    String snippetName = null;
    Item item = null;
    try {
        item = itemUIRegistry.getItem(w.getItem());
        if (s.getMappings().size() == 0) {
            if (item instanceof RollershutterItem) {
                snippetName = "rollerblind";
            } else if (item instanceof GroupItem && ((GroupItem) item).getBaseItem() instanceof RollershutterItem) {
                snippetName = "rollerblind";
            } else {
                snippetName = "switch";
            }
        } else {
            snippetName = "buttons";
        }
    } catch (ItemNotFoundException e) {
        logger.warn("Cannot determine item type of '{}'", w.getItem(), e);
        snippetName = "switch";
    }
    String snippet = getSnippet(snippetName);
    State state = itemUIRegistry.getState(w);
    snippet = preprocessSnippet(snippet, w);
    snippet = StringUtils.replace(snippet, "%count%", Integer.toString(s.getMappings().size()));
    if (s.getMappings().size() == 0) {
        if (state.equals(OnOffType.ON)) {
            snippet = snippet.replaceAll("%checked%", "checked=true");
        } else {
            snippet = snippet.replaceAll("%checked%", "");
        }
    } else {
        StringBuilder buttons = new StringBuilder();
        for (Mapping mapping : s.getMappings()) {
            String button = getSnippet("button");
            String command = mapping.getCmd();
            String label = mapping.getLabel();
            if (item instanceof NumberItem && ((NumberItem) item).getDimension() != null) {
                String unit = getUnitForWidget(w);
                command = StringUtils.replace(command, UnitUtils.UNIT_PLACEHOLDER, unit);
                label = StringUtils.replace(label, UnitUtils.UNIT_PLACEHOLDER, unit);
                // Special treatment for °C since uom library uses a single character: ℃
                // This will ensure the current state matches the cmd and the buttonClass is set accordingly.
                command = StringUtils.replace(command, "°C", "℃");
            }
            button = StringUtils.replace(button, "%item%", w.getItem());
            button = StringUtils.replace(button, "%cmd%", escapeHtml(command));
            button = StringUtils.replace(button, "%label%", label != null ? escapeHtml(label) : "");
            String buttonClass;
            State compareMappingState = state;
            if (state instanceof QuantityType) {
                // convert the item state to the command value for proper
                // comparison and buttonClass calculation
                compareMappingState = convertStateToLabelUnit((QuantityType<?>) state, command);
            }
            if (s.getMappings().size() > 1 && compareMappingState.toString().equals(command)) {
                buttonClass = "mdl-button--accent";
            } else {
                buttonClass = "mdl-button";
            }
            button = StringUtils.replace(button, "%class%", buttonClass);
            buttons.append(button);
        }
        snippet = StringUtils.replace(snippet, "%buttons%", buttons.toString());
    }
    // Process the color tags
    snippet = processColor(w, snippet);
    sb.append(snippet);
    return null;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) Switch(org.eclipse.smarthome.model.sitemap.Switch) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) Mapping(org.eclipse.smarthome.model.sitemap.Mapping) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 5 with QuantityType

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

the class SetpointRenderer method renderWidget.

@Override
public EList<Widget> renderWidget(Widget w, StringBuilder sb) throws RenderException {
    Setpoint sp = (Setpoint) w;
    State state = itemUIRegistry.getState(w);
    String newLowerState = state.toString();
    String newHigherState = state.toString();
    // set defaults for min, max and step
    BigDecimal step = sp.getStep();
    if (step == null) {
        step = BigDecimal.ONE;
    }
    BigDecimal minValue = sp.getMinValue();
    if (minValue == null) {
        minValue = BigDecimal.ZERO;
    }
    BigDecimal maxValue = sp.getMaxValue();
    if (maxValue == null) {
        maxValue = BigDecimal.valueOf(100);
    }
    // if the current state is a valid value, we calculate the up and down step values
    if (state instanceof DecimalType || state instanceof QuantityType) {
        BigDecimal currentState;
        if (state instanceof DecimalType) {
            currentState = ((DecimalType) state).toBigDecimal();
        } else {
            currentState = ((QuantityType<?>) state).toBigDecimal();
        }
        BigDecimal newLower = currentState.subtract(step);
        BigDecimal newHigher = currentState.add(step);
        if (newLower.compareTo(minValue) < 0) {
            newLower = minValue;
        }
        if (newHigher.compareTo(maxValue) > 0) {
            newHigher = maxValue;
        }
        newLowerState = newLower.toString();
        newHigherState = newHigher.toString();
        if (state instanceof QuantityType) {
            newLowerState = newLowerState + " " + ((QuantityType<?>) state).getUnit().toString();
            newHigherState = newHigherState + " " + ((QuantityType<?>) state).getUnit().toString();
        }
    }
    String snippetName = "setpoint";
    String snippet = getSnippet(snippetName);
    snippet = StringUtils.replace(snippet, "%id%", itemUIRegistry.getWidgetId(w));
    snippet = StringUtils.replace(snippet, "%category%", getCategory(w));
    snippet = StringUtils.replace(snippet, "%item%", w.getItem());
    snippet = StringUtils.replace(snippet, "%state%", getState(w));
    snippet = StringUtils.replace(snippet, "%newlowerstate%", newLowerState);
    snippet = StringUtils.replace(snippet, "%newhigherstate%", newHigherState);
    snippet = StringUtils.replace(snippet, "%label%", getLabel(w));
    snippet = StringUtils.replace(snippet, "%format%", getFormat());
    snippet = StringUtils.replace(snippet, "%servletname%", WebAppServlet.SERVLET_NAME);
    snippet = StringUtils.replace(snippet, "%minValue%", minValue.toString());
    snippet = StringUtils.replace(snippet, "%maxValue%", maxValue.toString());
    snippet = StringUtils.replace(snippet, "%step%", step.toString());
    // Process the color tags
    snippet = processColor(w, snippet);
    sb.append(snippet);
    return null;
}
Also used : QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) State(org.eclipse.smarthome.core.types.State) Setpoint(org.eclipse.smarthome.model.sitemap.Setpoint) DecimalType(org.eclipse.smarthome.core.library.types.DecimalType) BigDecimal(java.math.BigDecimal)

Aggregations

QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)15 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)12 State (org.eclipse.smarthome.core.types.State)12 Item (org.eclipse.smarthome.core.items.Item)6 Test (org.junit.Test)6 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)5 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)4 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)4 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)4 Temperature (javax.measure.quantity.Temperature)3 GroupItem (org.eclipse.smarthome.core.items.GroupItem)3 PercentType (org.eclipse.smarthome.core.library.types.PercentType)3 Switch (org.eclipse.smarthome.model.sitemap.Switch)3 UnitProvider (org.eclipse.smarthome.core.i18n.UnitProvider)2 GroupFunctionDTO (org.eclipse.smarthome.core.items.dto.GroupFunctionDTO)2 ArithmeticGroupFunction (org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction)2 DateTimeType (org.eclipse.smarthome.core.library.types.DateTimeType)2 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)2 StringType (org.eclipse.smarthome.core.library.types.StringType)2 StateDescription (org.eclipse.smarthome.core.types.StateDescription)2