Search in sources :

Example 11 with State

use of org.eclipse.smarthome.core.types.State 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 12 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class ProxyServletService method uriFromRequest.

/**
 * Determine which URI to address based on the request contents.
 *
 * @param request the servlet request. New attributes may be added to the request in order to cache the result for
 *            future calls.
 * @return the URI indicated by the request, or <code>null</code> if not possible
 */
URI uriFromRequest(HttpServletRequest request) {
    try {
        // Return any URI we've already saved for this request
        URI uri = (URI) request.getAttribute(ATTR_URI);
        if (uri != null) {
            return uri;
        } else {
            ProxyServletException pse = (ProxyServletException) request.getAttribute(ATTR_SERVLET_EXCEPTION);
            if (pse != null) {
                // If we errored on this request before, there is no point continuing
                return null;
            }
        }
        String sitemapName = request.getParameter("sitemap");
        if (sitemapName == null) {
            throw new ProxyServletException(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'sitemap' must be provided!");
        }
        String widgetId = request.getParameter("widgetId");
        if (widgetId == null) {
            throw new ProxyServletException(HttpServletResponse.SC_BAD_REQUEST, "Parameter 'widgetId' must be provided!");
        }
        Sitemap sitemap = (Sitemap) modelRepository.getModel(sitemapName);
        if (sitemap == null) {
            throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND, String.format("Sitemap '%s' could not be found!", sitemapName));
        }
        Widget widget = itemUIRegistry.getWidget(sitemap, widgetId);
        if (widget == null) {
            throw new ProxyServletException(HttpServletResponse.SC_NOT_FOUND, String.format("Widget '%s' could not be found!", widgetId));
        }
        String uriString = null;
        if (widget instanceof Image) {
            uriString = ((Image) widget).getUrl();
        } else if (widget instanceof Video) {
            uriString = ((Video) widget).getUrl();
        } else {
            throw new ProxyServletException(HttpServletResponse.SC_FORBIDDEN, String.format("Widget type '%s' is not supported!", widget.getClass().getName()));
        }
        String itemName = widget.getItem();
        if (itemName != null) {
            State state = itemUIRegistry.getItemState(itemName);
            if (state != null && state instanceof StringType) {
                try {
                    uri = URI.create(state.toString());
                    request.setAttribute(ATTR_URI, uri);
                    return uri;
                } catch (IllegalArgumentException ex) {
                // fall thru
                }
            }
        }
        try {
            uri = URI.create(uriString);
            request.setAttribute(ATTR_URI, uri);
            return uri;
        } catch (IllegalArgumentException iae) {
            throw new ProxyServletException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, String.format("URI '%s' is not a valid URI.", uriString));
        }
    } catch (ProxyServletException pse) {
        request.setAttribute(ATTR_SERVLET_EXCEPTION, pse);
        return null;
    }
}
Also used : Sitemap(org.eclipse.smarthome.model.sitemap.Sitemap) StringType(org.eclipse.smarthome.core.library.types.StringType) Video(org.eclipse.smarthome.model.sitemap.Video) State(org.eclipse.smarthome.core.types.State) Widget(org.eclipse.smarthome.model.sitemap.Widget) Image(org.eclipse.smarthome.model.sitemap.Image) URI(java.net.URI)

Example 13 with State

use of org.eclipse.smarthome.core.types.State 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 14 with State

use of org.eclipse.smarthome.core.types.State in project smarthome by eclipse.

the class ItemUIRegistryImplTest method testStateConversionForSliderWidgetThroughGetState.

@Test
public void testStateConversionForSliderWidgetThroughGetState() throws ItemNotFoundException {
    State colorState = new HSBType("23,42,75");
    ColorItem colorItem = new ColorItem("myItem");
    colorItem.setLabel("myItem");
    colorItem.setState(colorState);
    when(registry.getItem("myItem")).thenReturn(colorItem);
    Slider sliderWidget = mock(Slider.class);
    when(sliderWidget.getItem()).thenReturn("myItem");
    State stateForSlider = uiRegistry.getState(sliderWidget);
    assertTrue(stateForSlider instanceof PercentType);
    PercentType pt = (PercentType) stateForSlider;
    assertEquals(75, pt.longValue());
}
Also used : Slider(org.eclipse.smarthome.model.sitemap.Slider) State(org.eclipse.smarthome.core.types.State) ColorItem(org.eclipse.smarthome.core.library.items.ColorItem) PercentType(org.eclipse.smarthome.core.library.types.PercentType) HSBType(org.eclipse.smarthome.core.library.types.HSBType) Test(org.junit.Test)

Example 15 with State

use of org.eclipse.smarthome.core.types.State 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)

Aggregations

State (org.eclipse.smarthome.core.types.State)130 Test (org.junit.Test)59 DecimalType (org.eclipse.smarthome.core.library.types.DecimalType)23 PercentType (org.eclipse.smarthome.core.library.types.PercentType)22 Item (org.eclipse.smarthome.core.items.Item)21 NumberItem (org.eclipse.smarthome.core.library.items.NumberItem)19 Temperature (javax.measure.quantity.Temperature)18 StringType (org.eclipse.smarthome.core.library.types.StringType)18 QuantityType (org.eclipse.smarthome.core.library.types.QuantityType)17 ItemNotFoundException (org.eclipse.smarthome.core.items.ItemNotFoundException)15 HSBType (org.eclipse.smarthome.core.library.types.HSBType)15 OnOffType (org.eclipse.smarthome.core.library.types.OnOffType)15 RollershutterItem (org.eclipse.smarthome.core.library.items.RollershutterItem)13 JavaOSGiTest (org.eclipse.smarthome.test.java.JavaOSGiTest)13 ColorItem (org.eclipse.smarthome.core.library.items.ColorItem)12 SwitchItem (org.eclipse.smarthome.core.library.items.SwitchItem)11 DimmerItem (org.eclipse.smarthome.core.library.items.DimmerItem)10 RawType (org.eclipse.smarthome.core.library.types.RawType)10 Pressure (javax.measure.quantity.Pressure)9 GroupItem (org.eclipse.smarthome.core.items.GroupItem)8