Search in sources :

Example 6 with ItemNotFoundException

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

the class ItemUIRegistryImpl method processColorDefinition.

private String processColorDefinition(State state, List<ColorArray> colorList) {
    // Sanity check
    if (colorList == null) {
        return null;
    }
    if (colorList.size() == 0) {
        return null;
    }
    String colorString = null;
    // static colour
    if (colorList.size() == 1 && colorList.get(0).getState() == null) {
        colorString = colorList.get(0).getArg();
    } else {
        // with the supplied value
        for (ColorArray color : colorList) {
            // Use a local state variable in case it gets overridden below
            State cmpState = state;
            if (color.getState() == null) {
                logger.error("Error parsing color");
                continue;
            }
            // If there's an item defined here, get its state
            String itemName = color.getItem();
            if (itemName != null) {
                // Try and find the item to test.
                // If it's not found, return visible
                Item item;
                try {
                    item = itemRegistry.getItem(itemName);
                    // Get the item state
                    cmpState = item.getState();
                } catch (ItemNotFoundException e) {
                    logger.warn("Cannot retrieve color item {} for widget", color.getItem());
                }
            }
            // Handle the sign
            String value;
            if (color.getSign() != null) {
                value = color.getSign() + color.getState();
            } else {
                value = color.getState();
            }
            if (matchStateToValue(cmpState, value, color.getCondition()) == true) {
                // We have the color for this value - break!
                colorString = color.getArg();
                break;
            }
        }
    }
    // Remove quotes off the colour - if they exist
    if (colorString == null) {
        return null;
    }
    if (colorString.startsWith("\"") && colorString.endsWith("\"")) {
        colorString = colorString.substring(1, colorString.length() - 1);
    }
    return colorString;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) State(org.eclipse.smarthome.core.types.State) ColorArray(org.eclipse.smarthome.model.sitemap.ColorArray) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 7 with ItemNotFoundException

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

the class ItemUIRegistryImpl method getDynamicGroupChildren.

/**
 * This method creates a list of children for a group dynamically.
 * If there are no explicit children defined in a sitemap, the children
 * can thus be created on the fly by iterating over the members of the group item.
 *
 * @param group The group widget to get children for
 * @return a list of default widgets provided for the member items
 */
private EList<Widget> getDynamicGroupChildren(Group group) {
    EList<Widget> children = new BasicEList<Widget>();
    String itemName = group.getItem();
    try {
        if (itemName != null) {
            Item item = getItem(itemName);
            if (item instanceof GroupItem) {
                GroupItem groupItem = (GroupItem) item;
                for (Item member : groupItem.getMembers()) {
                    Widget widget = getDefaultWidget(member.getClass(), member.getName());
                    if (widget != null) {
                        widget.setItem(member.getName());
                        children.add(widget);
                    }
                }
            } else {
                logger.warn("Item '{}' is not a group.", item.getName());
            }
        } else {
            logger.warn("Group does not specify an associated item - ignoring it.");
        }
    } catch (ItemNotFoundException e) {
        logger.warn("Dynamic group with label '{}' will be ignored, because its item '{}' does not exist.", group.getLabel(), itemName);
    }
    return children;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) BasicEList(org.eclipse.emf.common.util.BasicEList) Widget(org.eclipse.smarthome.model.sitemap.Widget) LinkableWidget(org.eclipse.smarthome.model.sitemap.LinkableWidget) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 8 with ItemNotFoundException

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

the class ItemUIRegistryImpl method getVisiblity.

@Override
public boolean getVisiblity(Widget w) {
    // Default to visible if parameters not set
    List<VisibilityRule> ruleList = w.getVisibility();
    if (ruleList == null) {
        return true;
    }
    if (ruleList.size() == 0) {
        return true;
    }
    logger.debug("Checking visiblity for widget '{}'.", w.getLabel());
    for (VisibilityRule rule : w.getVisibility()) {
        String itemName = rule.getItem();
        if (itemName == null) {
            continue;
        }
        if (rule.getState() == null) {
            continue;
        }
        // Try and find the item to test.
        // If it's not found, return visible
        Item item;
        try {
            item = itemRegistry.getItem(itemName);
        } catch (ItemNotFoundException e) {
            logger.error("Cannot retrieve visibility item {} for widget {}", rule.getItem(), w.eClass().getInstanceTypeName());
            // Default to visible!
            return true;
        }
        // Get the item state
        State state = item.getState();
        // Handle the sign
        String value;
        if (rule.getSign() != null) {
            value = rule.getSign() + rule.getState();
        } else {
            value = rule.getState();
        }
        if (matchStateToValue(state, value, rule.getCondition()) == true) {
            // We have the name for this value!
            return true;
        }
    }
    logger.debug("Widget {} is not visible.", w.getLabel());
    // The state wasn't in the list, so we don't display it
    return false;
}
Also used : NumberItem(org.eclipse.smarthome.core.library.items.NumberItem) CallItem(org.eclipse.smarthome.core.library.items.CallItem) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) DateTimeItem(org.eclipse.smarthome.core.library.items.DateTimeItem) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) LocationItem(org.eclipse.smarthome.core.library.items.LocationItem) ContactItem(org.eclipse.smarthome.core.library.items.ContactItem) GenericItem(org.eclipse.smarthome.core.items.GenericItem) Item(org.eclipse.smarthome.core.items.Item) StringItem(org.eclipse.smarthome.core.library.items.StringItem) PlayerItem(org.eclipse.smarthome.core.library.items.PlayerItem) ImageItem(org.eclipse.smarthome.core.library.items.ImageItem) DimmerItem(org.eclipse.smarthome.core.library.items.DimmerItem) VisibilityRule(org.eclipse.smarthome.model.sitemap.VisibilityRule) State(org.eclipse.smarthome.core.types.State) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Example 9 with ItemNotFoundException

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

the class ItemUIRegistryImplTest method getLabel_itemNotFound.

@Test
public void getLabel_itemNotFound() throws ItemNotFoundException {
    String testLabel = "Label [%s]";
    when(widget.getLabel()).thenReturn(testLabel);
    when(widget.eClass()).thenReturn(SitemapFactory.eINSTANCE.createText().eClass());
    when(registry.getItem("Item")).thenThrow(new ItemNotFoundException("Item"));
    when(item.getState()).thenReturn(new StringType("State"));
    String label = uiRegistry.getLabel(widget);
    assertEquals("Label [-]", label);
}
Also used : StringType(org.eclipse.smarthome.core.library.types.StringType) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Test(org.junit.Test)

Example 10 with ItemNotFoundException

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

the class ChartServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    logger.debug("Received incoming chart request: {}", req);
    int width = defaultWidth;
    try {
        width = Integer.parseInt(req.getParameter("w"));
    } catch (Exception e) {
    }
    int height = defaultHeight;
    try {
        String h = req.getParameter("h");
        if (h != null) {
            Double d = Double.parseDouble(h) * scale;
            height = d.intValue();
        }
    } catch (Exception e) {
    }
    // To avoid ambiguity you are not allowed to specify period, begin and end time at the same time.
    if (req.getParameter("period") != null && req.getParameter("begin") != null && req.getParameter("end") != null) {
        throw new ServletException("Do not specify the three parameters period, begin and end at the same time.");
    }
    // Read out the parameter period, begin and end and save them.
    Date timeBegin = null;
    Date timeEnd = null;
    Long period = PERIODS.get(req.getParameter("period"));
    if (period == null) {
        // use a day as the default period
        period = PERIODS.get("D");
    }
    if (req.getParameter("begin") != null) {
        try {
            timeBegin = new SimpleDateFormat(DATE_FORMAT).parse(req.getParameter("begin"));
        } catch (ParseException e) {
            throw new ServletException("Begin and end must have this format: " + DATE_FORMAT + ".");
        }
    }
    if (req.getParameter("end") != null) {
        try {
            timeEnd = new SimpleDateFormat(DATE_FORMAT).parse(req.getParameter("end"));
        } catch (ParseException e) {
            throw new ServletException("Begin and end must have this format: " + DATE_FORMAT + ".");
        }
    }
    // Set begin and end time and check legality.
    if (timeBegin == null && timeEnd == null) {
        timeEnd = new Date();
        timeBegin = new Date(timeEnd.getTime() - period);
        logger.debug("No begin or end is specified, use now as end and now-period as begin.");
    } else if (timeEnd == null) {
        timeEnd = new Date(timeBegin.getTime() + period);
        logger.debug("No end is specified, use begin + period as end.");
    } else if (timeBegin == null) {
        timeBegin = new Date(timeEnd.getTime() - period);
        logger.debug("No begin is specified, use end-period as begin");
    } else if (timeEnd.before(timeBegin)) {
        throw new ServletException("The end is before the begin.");
    }
    // If a persistence service is specified, find the provider
    String serviceName = req.getParameter("service");
    ChartProvider provider = getChartProviders().get(providerName);
    if (provider == null) {
        throw new ServletException("Could not get chart provider.");
    }
    // Read out the parameter 'dpi'
    Integer dpi = null;
    if (req.getParameter("dpi") != null) {
        try {
            dpi = Integer.valueOf(req.getParameter("dpi"));
        } catch (NumberFormatException e) {
            throw new ServletException("dpi parameter is invalid");
        }
        if (dpi <= 0) {
            throw new ServletException("dpi parameter is <= 0");
        }
    }
    // Read out parameter 'legend'
    Boolean legend = null;
    if (req.getParameter("legend") != null) {
        legend = BooleanUtils.toBoolean(req.getParameter("legend"));
    }
    if (maxWidth > 0 && width > maxWidth) {
        height = Math.round((float) height / (float) width * maxWidth);
        if (dpi != null) {
            dpi = Math.round((float) dpi / (float) width * maxWidth);
        }
        width = maxWidth;
    }
    // Set the content type to that provided by the chart provider
    res.setContentType("image/" + provider.getChartType());
    logger.debug("chart building with width {} height {} dpi {}", width, height, dpi);
    try (ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(res.getOutputStream())) {
        BufferedImage chart = provider.createChart(serviceName, req.getParameter("theme"), timeBegin, timeEnd, height, width, req.getParameter("items"), req.getParameter("groups"), dpi, legend);
        ImageIO.write(chart, provider.getChartType().toString(), imageOutputStream);
        logger.debug("Chart successfully generated and written to the response.");
    } catch (ItemNotFoundException e) {
        logger.debug("{}", e.getMessage());
        res.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
    } catch (IllegalArgumentException e) {
        logger.warn("Illegal argument in chart: {}", e.getMessage());
        res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Illegal argument in chart: " + e.getMessage());
    } catch (RuntimeException e) {
        if (logger.isDebugEnabled()) {
            // we also attach the stack trace
            logger.warn("Chart generation failed: {}", e.getMessage(), e);
        } else {
            logger.warn("Chart generation failed: {}", e.getMessage());
        }
        res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : ServletException(javax.servlet.ServletException) NamespaceException(org.osgi.service.http.NamespaceException) ParseException(java.text.ParseException) IOException(java.io.IOException) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Date(java.util.Date) BufferedImage(java.awt.image.BufferedImage) ServletException(javax.servlet.ServletException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) ChartProvider(org.eclipse.smarthome.ui.chart.ChartProvider) ImageOutputStream(javax.imageio.stream.ImageOutputStream) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException)

Aggregations

ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)41 Item (org.eclipse.smarthome.core.items.Item)36 GroupItem (org.eclipse.smarthome.core.items.GroupItem)22 GenericItem (org.eclipse.smarthome.core.items.GenericItem)17 State (org.eclipse.smarthome.core.types.State)15 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)8 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)8 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)8 Command (org.eclipse.smarthome.core.types.Command)7 ItemNotUniqueException (org.eclipse.smarthome.core.items.ItemNotUniqueException)5 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)5 Mapping (org.eclipse.smarthome.model.sitemap.Mapping)5 Date (java.util.Date)4 CallItem (org.eclipse.smarthome.core.library.items.CallItem)4 DateTimeItem (org.eclipse.smarthome.core.library.items.DateTimeItem)4 StringItem (org.eclipse.smarthome.core.library.items.StringItem)4 Widget (org.eclipse.smarthome.model.sitemap.Widget)4 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)3 ContactItem (org.eclipse.smarthome.core.library.items.ContactItem)3 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)3