Search in sources :

Example 1 with Switch

use of org.eclipse.smarthome.model.sitemap.Switch in project smarthome by eclipse.

the class ItemUIRegistryImplTest method testStateConversionForSwitchWidgetThroughGetState.

@Test
public void testStateConversionForSwitchWidgetThroughGetState() throws ItemNotFoundException {
    State colorState = new HSBType("23,42,50");
    ColorItem colorItem = new ColorItem("myItem");
    colorItem.setLabel("myItem");
    colorItem.setState(colorState);
    when(registry.getItem("myItem")).thenReturn(colorItem);
    Switch switchWidget = mock(Switch.class);
    when(switchWidget.getItem()).thenReturn("myItem");
    when(switchWidget.getMappings()).thenReturn(new BasicEList<Mapping>());
    State stateForSwitch = uiRegistry.getState(switchWidget);
    assertEquals(OnOffType.ON, stateForSwitch);
}
Also used : Switch(org.eclipse.smarthome.model.sitemap.Switch) State(org.eclipse.smarthome.core.types.State) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Mapping(org.eclipse.smarthome.model.sitemap.Mapping) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Test(org.junit.Test)

Example 2 with Switch

use of org.eclipse.smarthome.model.sitemap.Switch in project smarthome by eclipse.

the class ItemUIRegistryImplTest method testStateConversionForSwitchWidgetWithMappingThroughGetState.

@Test
public void testStateConversionForSwitchWidgetWithMappingThroughGetState() throws ItemNotFoundException {
    State colorState = new HSBType("23,42,50");
    ColorItem colorItem = new ColorItem("myItem");
    colorItem.setLabel("myItem");
    colorItem.setState(colorState);
    when(registry.getItem("myItem")).thenReturn(colorItem);
    Switch switchWidget = mock(Switch.class);
    when(switchWidget.getItem()).thenReturn("myItem");
    Mapping mapping = mock(Mapping.class);
    BasicEList<Mapping> mappings = new BasicEList<Mapping>();
    mappings.add(mapping);
    when(switchWidget.getMappings()).thenReturn(mappings);
    State stateForSwitch = uiRegistry.getState(switchWidget);
    assertEquals(colorState, stateForSwitch);
}
Also used : Switch(org.eclipse.smarthome.model.sitemap.Switch) State(org.eclipse.smarthome.core.types.State) BasicEList(org.eclipse.emf.common.util.BasicEList) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) Mapping(org.eclipse.smarthome.model.sitemap.Mapping) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Test(org.junit.Test)

Example 3 with Switch

use of org.eclipse.smarthome.model.sitemap.Switch 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 4 with Switch

use of org.eclipse.smarthome.model.sitemap.Switch in project smarthome by eclipse.

the class ItemUIRegistryImpl method createPlayerButtons.

private Switch createPlayerButtons() {
    Switch playerItemSwitch = SitemapFactory.eINSTANCE.createSwitch();
    List<Mapping> mappings = playerItemSwitch.getMappings();
    Mapping commandMapping = null;
    mappings.add(commandMapping = SitemapFactory.eINSTANCE.createMapping());
    commandMapping.setCmd(NextPreviousType.PREVIOUS.name());
    commandMapping.setLabel("<<");
    mappings.add(commandMapping = SitemapFactory.eINSTANCE.createMapping());
    commandMapping.setCmd(PlayPauseType.PAUSE.name());
    commandMapping.setLabel("||");
    mappings.add(commandMapping = SitemapFactory.eINSTANCE.createMapping());
    commandMapping.setCmd(PlayPauseType.PLAY.name());
    commandMapping.setLabel(">");
    mappings.add(commandMapping = SitemapFactory.eINSTANCE.createMapping());
    commandMapping.setCmd(NextPreviousType.NEXT.name());
    commandMapping.setLabel(">>");
    return playerItemSwitch;
}
Also used : Switch(org.eclipse.smarthome.model.sitemap.Switch) Mapping(org.eclipse.smarthome.model.sitemap.Mapping)

Example 5 with Switch

use of org.eclipse.smarthome.model.sitemap.Switch in project smarthome by eclipse.

the class ItemUIRegistryImpl method convertState.

/**
 * Converts an item state to the type the widget supports (if possible)
 *
 * @param w Widget in sitemap that shows the state
 * @param i item
 * @return the converted state or the original if conversion was not possible
 */
private State convertState(Widget w, Item i) {
    State returnState = null;
    State itemState = i.getState();
    if (itemState instanceof QuantityType) {
        itemState = convertStateToWidgetUnit((QuantityType<?>) itemState, w);
    }
    if (w instanceof Switch && i instanceof RollershutterItem) {
        // RollerShutter are represented as Switch in a Sitemap but need a PercentType state
        returnState = itemState.as(PercentType.class);
    } else if (w instanceof Slider) {
        if (i.getAcceptedDataTypes().contains(PercentType.class)) {
            returnState = itemState.as(PercentType.class);
        } else {
            returnState = itemState.as(DecimalType.class);
        }
    } else if (w instanceof Switch) {
        Switch sw = (Switch) w;
        if (sw.getMappings().size() == 0) {
            returnState = itemState.as(OnOffType.class);
        }
    }
    // if returnState is null, a conversion was not possible
    if (returnState == null) {
        // we return the original state to not break anything
        returnState = itemState;
    }
    return returnState;
}
Also used : Switch(org.eclipse.smarthome.model.sitemap.Switch) Slider(org.eclipse.smarthome.model.sitemap.Slider) QuantityType(org.eclipse.smarthome.core.library.types.QuantityType) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) State(org.eclipse.smarthome.core.types.State) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) PercentType(org.eclipse.smarthome.core.library.types.PercentType)

Aggregations

Switch (org.eclipse.smarthome.model.sitemap.Switch)7 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)6 State (org.eclipse.smarthome.core.types.State)5 Item (org.eclipse.smarthome.core.items.Item)3 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)3 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)3 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)3 GroupItem (org.eclipse.smarthome.core.items.GroupItem)2 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)2 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)2 HSBType (org.eclipse.smarthome.core.library.types.HSBType)2 Slider (org.eclipse.smarthome.model.sitemap.Slider)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 BasicEList (org.eclipse.emf.common.util.BasicEList)1 EList (org.eclipse.emf.common.util.EList)1 GenericItem (org.eclipse.smarthome.core.items.GenericItem)1 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)1 PercentType (org.eclipse.smarthome.core.library.types.PercentType)1