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;
}
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;
}
}
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);
}
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());
}
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);
}
Aggregations