Search in sources :

Example 6 with GroupItem

use of org.eclipse.smarthome.core.items.GroupItem 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 7 with GroupItem

use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.

the class CmdServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    for (Object key : req.getParameterMap().keySet()) {
        String itemName = key.toString();
        if (!itemName.startsWith("__")) {
            // all additional webapp params start with "__" and should be ignored
            String commandName = req.getParameter(itemName);
            try {
                Item item = itemRegistry.getItem(itemName);
                // into real commands by the webapp.
                if ((item instanceof SwitchItem || item instanceof GroupItem) && commandName.equals("TOGGLE")) {
                    commandName = OnOffType.ON.equals(item.getStateAs(OnOffType.class)) ? "OFF" : "ON";
                }
                Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
                if (command != null) {
                    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
                } else {
                    logger.warn("Received unknown command '{}' for item '{}'", commandName, itemName);
                }
            } catch (ItemNotFoundException e) {
                logger.warn("Received command '{}' for item '{}', but the item does not exist in the registry", commandName, itemName);
            }
        }
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) Command(org.eclipse.smarthome.core.types.Command) GroupItem(org.eclipse.smarthome.core.items.GroupItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 8 with GroupItem

use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.

the class CmdServlet method service.

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    for (Object key : req.getParameterMap().keySet()) {
        String itemName = key.toString();
        if (!itemName.startsWith("__")) {
            // all additional webapp params start with "__" and should be ignored
            String commandName = req.getParameter(itemName);
            try {
                Item item = itemRegistry.getItem(itemName);
                // into real commands by the webapp.
                if ((item instanceof SwitchItem || item instanceof GroupItem) && commandName.equals("TOGGLE")) {
                    commandName = OnOffType.ON.equals(item.getStateAs(OnOffType.class)) ? "OFF" : "ON";
                }
                Command command = TypeParser.parseCommand(item.getAcceptedCommandTypes(), commandName);
                if (command != null) {
                    eventPublisher.post(ItemEventFactory.createCommandEvent(itemName, command));
                } else {
                    logger.warn("Received unknown command '{}' for item '{}'", commandName, itemName);
                }
            } catch (ItemNotFoundException e) {
                logger.warn("Received command '{}' for item '{}', but the item does not exist in the registry", commandName, itemName);
            }
        }
    }
}
Also used : SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) Item(org.eclipse.smarthome.core.items.Item) GroupItem(org.eclipse.smarthome.core.items.GroupItem) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) Command(org.eclipse.smarthome.core.types.Command) GroupItem(org.eclipse.smarthome.core.items.GroupItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 9 with GroupItem

use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.

the class ItemDTOMapper method map.

/**
 * Maps item DTO into item object.
 *
 * @param itemDTO the DTO
 * @param itemFactories the item factories in order to create the items
 * @return the item object
 */
@Nullable
public static ActiveItem map(ItemDTO itemDTO, Set<ItemFactory> itemFactories) {
    if (itemDTO == null) {
        throw new IllegalArgumentException("The argument 'itemDTO' must no be null.");
    }
    if (itemFactories == null) {
        throw new IllegalArgumentException("The argument 'itemFactories' must no be null.");
    }
    GenericItem newItem = null;
    if (itemDTO.type != null) {
        if (itemDTO instanceof GroupItemDTO && itemDTO.type.equals(GroupItem.TYPE)) {
            GroupItemDTO groupItemDTO = (GroupItemDTO) itemDTO;
            GenericItem baseItem = null;
            if (!StringUtils.isEmpty(groupItemDTO.groupType)) {
                baseItem = createItem(groupItemDTO.groupType, itemDTO.name, itemFactories);
            }
            GroupFunction function = new GroupFunction.Equality();
            if (groupItemDTO.function != null) {
                function = mapFunction(baseItem, groupItemDTO.function);
            }
            newItem = new GroupItem(itemDTO.name, baseItem, function);
        } else {
            String itemType = itemDTO.type;
            newItem = createItem(itemType, itemDTO.name, itemFactories);
        }
        if (newItem != null) {
            if (itemDTO.label != null) {
                newItem.setLabel(itemDTO.label);
            }
            if (itemDTO.category != null) {
                newItem.setCategory(itemDTO.category);
            }
            if (itemDTO.groupNames != null) {
                newItem.addGroupNames(itemDTO.groupNames);
            }
            if (itemDTO.tags != null) {
                newItem.addTags(itemDTO.tags);
            }
        }
    }
    return newItem;
}
Also used : GroupFunction(org.eclipse.smarthome.core.items.GroupFunction) GenericItem(org.eclipse.smarthome.core.items.GenericItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 10 with GroupItem

use of org.eclipse.smarthome.core.items.GroupItem in project smarthome by eclipse.

the class GenericItemProvider2Test method testGroupAssignmentsAreConsidered.

@Test
public void testGroupAssignmentsAreConsidered() {
    assertThat(itemRegistry.getAll().size(), is(0));
    String model = // 
    "Group testGroup " + // 
    "Number number1 (testGroup) " + "Number number2 ";
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
    model = // 
    "Group testGroup " + // 
    "Number number1 (testGroup) " + "Number number2 (testGroup)";
    modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
    GenericItem item = (GenericItem) itemRegistry.get("number2");
    assertTrue(item.getGroupNames().contains("testGroup"));
    GroupItem groupItem = (GroupItem) itemRegistry.get("testGroup");
    assertTrue(groupItem.getAllMembers().contains(item));
}
Also used : GenericItem(org.eclipse.smarthome.core.items.GenericItem) ByteArrayInputStream(java.io.ByteArrayInputStream) GroupItem(org.eclipse.smarthome.core.items.GroupItem) JavaOSGiTest(org.eclipse.smarthome.test.java.JavaOSGiTest) Test(org.junit.Test)

Aggregations

GroupItem (org.eclipse.smarthome.core.items.GroupItem)29 Item (org.eclipse.smarthome.core.items.Item)18 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)13 GenericItem (org.eclipse.smarthome.core.items.GenericItem)12 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)11 Test (org.junit.Test)8 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)7 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)7 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)5 ArithmeticGroupFunction (org.eclipse.smarthome.core.library.types.ArithmeticGroupFunction)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Date (java.util.Date)3 GroupFunction (org.eclipse.smarthome.core.items.GroupFunction)3 HistoricItem (org.eclipse.smarthome.core.persistence.HistoricItem)3 ModelGroupFunction (org.eclipse.smarthome.model.items.ModelGroupFunction)3 ModelGroupItem (org.eclipse.smarthome.model.items.ModelGroupItem)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2