diff --git a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/PageChangeListener.java b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/PageChangeListener.java index 30aa6bc5e92..74bc32b045a 100644 --- a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/PageChangeListener.java +++ b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/PageChangeListener.java @@ -23,6 +23,7 @@ import java.util.stream.Collectors; import org.eclipse.emf.common.util.EList; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.common.ThreadPoolManager; import org.openhab.core.events.Event; import org.openhab.core.events.EventSubscriber; @@ -36,7 +37,9 @@ import org.openhab.core.library.CoreItemFactory; import org.openhab.core.model.sitemap.sitemap.Chart; import org.openhab.core.model.sitemap.sitemap.ColorArray; +import org.openhab.core.model.sitemap.sitemap.Condition; import org.openhab.core.model.sitemap.sitemap.Frame; +import org.openhab.core.model.sitemap.sitemap.IconRule; import org.openhab.core.model.sitemap.sitemap.VisibilityRule; import org.openhab.core.model.sitemap.sitemap.Widget; import org.openhab.core.types.State; @@ -47,6 +50,7 @@ * * @author Kai Kreuzer - Initial contribution * @author Laurent Garnier - Added support for icon color + * @author Laurent Garnier - New widget icon parameter based on conditional rules + multiple AND conditions */ public class PageChangeListener implements EventSubscriber { @@ -119,27 +123,39 @@ private Set getAllItems(EList widgets) { if (widget instanceof Frame frame) { items.addAll(getAllItems(frame.getChildren())); } + // now scan dynamic icon rules + for (IconRule rule : widget.getDynamicIcon()) { + addItemsFromConditions(items, rule.getConditions()); + } // now scan visibility rules for (VisibilityRule rule : widget.getVisibility()) { - addItemWithName(items, rule.getItem()); + addItemsFromConditions(items, rule.getConditions()); } // now scan label color rules for (ColorArray rule : widget.getLabelColor()) { - addItemWithName(items, rule.getItem()); + addItemsFromConditions(items, rule.getConditions()); } // now scan value color rules for (ColorArray rule : widget.getValueColor()) { - addItemWithName(items, rule.getItem()); + addItemsFromConditions(items, rule.getConditions()); } - // now scan value icon rules + // now scan icon color rules for (ColorArray rule : widget.getIconColor()) { - addItemWithName(items, rule.getItem()); + addItemsFromConditions(items, rule.getConditions()); } } } return items; } + private void addItemsFromConditions(Set items, @Nullable EList conditions) { + if (conditions != null) { + for (Condition condition : conditions) { + addItemWithName(items, condition.getItem()); + } + } + } + private void addItemWithName(Set items, String itemName) { if (itemName != null) { try { @@ -183,7 +199,7 @@ private Set constructSitemapEvents(Item item, State state, List 0; } - if (!skipWidget || definesVisibilityOrColor(w, item.getName())) { + if (!skipWidget || definesVisibilityOrColorOrIcon(w, item.getName())) { SitemapWidgetEvent event = constructSitemapEventForWidget(item, state, w); events.add(event); } @@ -197,6 +213,9 @@ private SitemapWidgetEvent constructSitemapEventForWidget(Item item, State state event.pageId = pageId; event.label = itemUIRegistry.getLabel(widget); event.widgetId = itemUIRegistry.getWidgetId(widget); + if (widget.getStaticIcon() == null) { + event.icon = itemUIRegistry.getCategory(widget); + } event.visibility = itemUIRegistry.getVisiblity(widget); event.descriptionChanged = false; // event.item contains the (potentially changed) data of the item belonging to @@ -237,11 +256,16 @@ private Item getItemForWidget(Widget w) { return null; } - private boolean definesVisibilityOrColor(Widget w, String name) { - return w.getVisibility().stream().anyMatch(r -> name.equals(r.getItem())) - || w.getLabelColor().stream().anyMatch(r -> name.equals(r.getItem())) - || w.getValueColor().stream().anyMatch(r -> name.equals(r.getItem())) - || w.getIconColor().stream().anyMatch(r -> name.equals(r.getItem())); + private boolean definesVisibilityOrColorOrIcon(Widget w, String name) { + return w.getVisibility().stream().anyMatch(r -> conditionsDependsOnItem(r.getConditions(), name)) + || w.getLabelColor().stream().anyMatch(r -> conditionsDependsOnItem(r.getConditions(), name)) + || w.getValueColor().stream().anyMatch(r -> conditionsDependsOnItem(r.getConditions(), name)) + || w.getIconColor().stream().anyMatch(r -> conditionsDependsOnItem(r.getConditions(), name)) + || w.getDynamicIcon().stream().anyMatch(r -> conditionsDependsOnItem(r.getConditions(), name)); + } + + private boolean conditionsDependsOnItem(@Nullable EList conditions, String name) { + return conditions != null && conditions.stream().anyMatch(c -> name.equals(c.getItem())); } public void sitemapContentChanged(EList widgets) { diff --git a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/SitemapResource.java b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/SitemapResource.java index 2874f5a7654..2ef3230bbd2 100644 --- a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/SitemapResource.java +++ b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/SitemapResource.java @@ -78,7 +78,9 @@ import org.openhab.core.model.sitemap.SitemapProvider; import org.openhab.core.model.sitemap.sitemap.Chart; import org.openhab.core.model.sitemap.sitemap.ColorArray; +import org.openhab.core.model.sitemap.sitemap.Condition; import org.openhab.core.model.sitemap.sitemap.Frame; +import org.openhab.core.model.sitemap.sitemap.IconRule; import org.openhab.core.model.sitemap.sitemap.Image; import org.openhab.core.model.sitemap.sitemap.Input; import org.openhab.core.model.sitemap.sitemap.LinkableWidget; @@ -131,6 +133,7 @@ * @author Wouter Born - Migrated to OpenAPI annotations * @author Laurent Garnier - Added support for icon color * @author Mark Herwege - Added pattern and unit fields + * @author Laurent Garnier - New widget icon parameter based on conditional rules + multiple AND conditions */ @Component(service = { RESTResource.class, EventSubscriber.class }) @JaxrsResource @@ -523,7 +526,7 @@ private PageDTO createPageBean(String sitemapName, @Nullable String title, @Null } bean.widgetId = widgetId; bean.icon = itemUIRegistry.getCategory(widget); - bean.staticIcon = widget.getStaticIcon() != null; + bean.staticIcon = widget.getStaticIcon() != null || !widget.getDynamicIcon().isEmpty(); bean.labelcolor = convertItemValueColor(itemUIRegistry.getLabelColor(widget), itemState); bean.valuecolor = convertItemValueColor(itemUIRegistry.getValueColor(widget), itemState); bean.iconcolor = convertItemValueColor(itemUIRegistry.getIconColor(widget), itemState); @@ -741,6 +744,8 @@ private Set getAllItems(EList widgets) { if (widget instanceof Frame frame) { items.addAll(getAllItems(frame.getChildren())); } + // Consider items involved in any icon condition + items.addAll(getItemsInIconCond(widget.getDynamicIcon())); // Consider items involved in any visibility, labelcolor, valuecolor and iconcolor condition items.addAll(getItemsInVisibilityCond(widget.getVisibility())); items.addAll(getItemsInColorCond(widget.getLabelColor())); @@ -753,37 +758,43 @@ private Set getAllItems(EList widgets) { private Set getItemsInVisibilityCond(EList ruleList) { Set items = new HashSet<>(); for (VisibilityRule rule : ruleList) { - String itemName = rule.getItem(); - if (itemName != null) { - try { - Item item = itemUIRegistry.getItem(itemName); - if (item instanceof GenericItem genericItem) { - items.add(genericItem); - } - } catch (ItemNotFoundException e) { - // ignore - } - } + getItemsInConditions(rule.getConditions(), items); } return items; } private Set getItemsInColorCond(EList colorList) { Set items = new HashSet<>(); - for (ColorArray color : colorList) { - String itemName = color.getItem(); - if (itemName != null) { - try { - Item item = itemUIRegistry.getItem(itemName); - if (item instanceof GenericItem genericItem) { - items.add(genericItem); + for (ColorArray rule : colorList) { + getItemsInConditions(rule.getConditions(), items); + } + return items; + } + + private Set getItemsInIconCond(EList ruleList) { + Set items = new HashSet<>(); + for (IconRule rule : ruleList) { + getItemsInConditions(rule.getConditions(), items); + } + return items; + } + + private void getItemsInConditions(@Nullable EList conditions, Set items) { + if (conditions != null) { + for (Condition condition : conditions) { + String itemName = condition.getItem(); + if (itemName != null) { + try { + Item item = itemUIRegistry.getItem(itemName); + if (item instanceof GenericItem genericItem) { + items.add(genericItem); + } + } catch (ItemNotFoundException e) { + // ignore } - } catch (ItemNotFoundException e) { - // ignore } } } - return items; } @Override diff --git a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/WidgetDTO.java b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/WidgetDTO.java index ce947faadba..82cd76f616e 100644 --- a/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/WidgetDTO.java +++ b/bundles/org.openhab.core.io.rest.sitemap/src/main/java/org/openhab/core/io/rest/sitemap/internal/WidgetDTO.java @@ -35,6 +35,12 @@ public class WidgetDTO { public String label; public String icon; + /** + * staticIcon is a boolean indicating if the widget state must be ignored when requesting the icon. + * It is set to true when the widget has either the staticIcon or dynamciIcon property set. + * Without staticIcon and dynamicIcon properties set, the default is to consider the widget state + * and this field will be set to false. + */ public Boolean staticIcon; public String labelcolor; public String valuecolor; diff --git a/bundles/org.openhab.core.io.rest.sitemap/src/test/java/org/openhab/core/io/rest/sitemap/internal/SitemapResourceTest.java b/bundles/org.openhab.core.io.rest.sitemap/src/test/java/org/openhab/core/io/rest/sitemap/internal/SitemapResourceTest.java index b8a0b0da6ab..eb4310dc94a 100644 --- a/bundles/org.openhab.core.io.rest.sitemap/src/test/java/org/openhab/core/io/rest/sitemap/internal/SitemapResourceTest.java +++ b/bundles/org.openhab.core.io.rest.sitemap/src/test/java/org/openhab/core/io/rest/sitemap/internal/SitemapResourceTest.java @@ -49,6 +49,8 @@ import org.openhab.core.library.types.PercentType; import org.openhab.core.model.sitemap.SitemapProvider; import org.openhab.core.model.sitemap.sitemap.ColorArray; +import org.openhab.core.model.sitemap.sitemap.Condition; +import org.openhab.core.model.sitemap.sitemap.IconRule; import org.openhab.core.model.sitemap.sitemap.Sitemap; import org.openhab.core.model.sitemap.sitemap.VisibilityRule; import org.openhab.core.model.sitemap.sitemap.Widget; @@ -61,6 +63,7 @@ * Test aspects of the {@link SitemapResource}. * * @author Henning Treu - Initial contribution + * @author Laurent Garnier - Extended tests for static icon and conditional rules for icon */ @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) @@ -79,10 +82,16 @@ public class SitemapResourceTest extends JavaTest { private static final String LABEL_COLOR_ITEM_NAME = "labelColorItemName"; private static final String VALUE_COLOR_ITEM_NAME = "valueColorItemName"; private static final String ICON_COLOR_ITEM_NAME = "iconColorItemName"; + private static final String ICON_ITEM_NAME = "iconItemName"; private static final String WIDGET1_LABEL = "widget 1"; private static final String WIDGET2_LABEL = "widget 2"; + private static final String WIDGET3_LABEL = "widget 3"; private static final String WIDGET1_ID = "00"; private static final String WIDGET2_ID = "01"; + private static final String WIDGET3_ID = "02"; + private static final String WIDGET1_ICON = "icon1"; + private static final String WIDGET2_ICON = "icon2"; + private static final String WIDGET3_ICON = "icon3"; private static final String CLIENT_IP = "127.0.0.1"; private @NonNullByDefault({}) SitemapResource sitemapResource; @@ -92,6 +101,7 @@ public class SitemapResourceTest extends JavaTest { private @NonNullByDefault({}) GenericItem labelColorItem; private @NonNullByDefault({}) GenericItem valueColorItem; private @NonNullByDefault({}) GenericItem iconColorItem; + private @NonNullByDefault({}) GenericItem iconItem; private @Mock @NonNullByDefault({}) HttpHeaders headersMock; private @Mock @NonNullByDefault({}) Sitemap defaultSitemapMock; @@ -120,6 +130,7 @@ public void setup() throws Exception { labelColorItem = new TestItem(LABEL_COLOR_ITEM_NAME); valueColorItem = new TestItem(VALUE_COLOR_ITEM_NAME); iconColorItem = new TestItem(ICON_COLOR_ITEM_NAME); + iconItem = new TestItem(ICON_ITEM_NAME); when(localeServiceMock.getLocale(null)).thenReturn(Locale.US); @@ -251,6 +262,54 @@ public void whenLongPollingShouldObserveItemsFromValueColorConditions() { // return } + @Test + public void whenLongPollingShouldObserveItemsFromIconColorConditions() { + ItemEvent itemEvent = mock(ItemEvent.class); + when(itemEvent.getItemName()).thenReturn(iconColorItem.getName()); + new Thread(() -> { + try { + Thread.sleep(STATE_UPDATE_WAIT_TIME); // wait for the #getPageData call and listeners to attach to the + // item + sitemapResource.receive(itemEvent); + } catch (InterruptedException e) { + } + }).start(); + + // non-null is sufficient here. + when(headersMock.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(List.of()); + + Response response = sitemapResource.getPageData(headersMock, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null, + false); + + PageDTO pageDTO = (PageDTO) response.getEntity(); + assertThat(pageDTO.timeout, is(false)); // assert that the item state change did trigger the blocking method to + // return + } + + @Test + public void whenLongPollingShouldObserveItemsFromIconConditions() { + ItemEvent itemEvent = mock(ItemEvent.class); + when(itemEvent.getItemName()).thenReturn(iconItem.getName()); + new Thread(() -> { + try { + Thread.sleep(STATE_UPDATE_WAIT_TIME); // wait for the #getPageData call and listeners to attach to the + // item + sitemapResource.receive(itemEvent); + } catch (InterruptedException e) { + } + }).start(); + + // non-null is sufficient here. + when(headersMock.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(List.of()); + + Response response = sitemapResource.getPageData(headersMock, null, SITEMAP_MODEL_NAME, SITEMAP_NAME, null, + false); + + PageDTO pageDTO = (PageDTO) response.getEntity(); + assertThat(pageDTO.timeout, is(false)); // assert that the item state change did trigger the blocking method to + // return + } + @Test public void whenGetPageDataShouldReturnPageBean() throws ItemNotFoundException { item.setState(new PercentType(50)); @@ -269,13 +328,15 @@ public void whenGetPageDataShouldReturnPageBean() throws ItemNotFoundException { assertThat(pageDTO.timeout, is(false)); assertThat(pageDTO.widgets, notNullValue()); - assertThat((Collection) pageDTO.widgets, hasSize(2)); + assertThat((Collection) pageDTO.widgets, hasSize(3)); assertThat(pageDTO.widgets.get(0).widgetId, is(WIDGET1_ID)); assertThat(pageDTO.widgets.get(0).label, is(WIDGET1_LABEL)); assertThat(pageDTO.widgets.get(0).labelcolor, is("GREEN")); assertThat(pageDTO.widgets.get(0).valuecolor, is("BLUE")); assertThat(pageDTO.widgets.get(0).iconcolor, is("ORANGE")); + assertThat(pageDTO.widgets.get(0).icon, is(WIDGET1_ICON)); + assertThat(pageDTO.widgets.get(0).staticIcon, is(true)); assertThat(pageDTO.widgets.get(0).state, nullValue()); assertThat(pageDTO.widgets.get(0).item, notNullValue()); assertThat(pageDTO.widgets.get(0).item.name, is(ITEM_NAME)); @@ -286,10 +347,24 @@ public void whenGetPageDataShouldReturnPageBean() throws ItemNotFoundException { assertThat(pageDTO.widgets.get(1).labelcolor, nullValue()); assertThat(pageDTO.widgets.get(1).valuecolor, nullValue()); assertThat(pageDTO.widgets.get(1).iconcolor, nullValue()); + assertThat(pageDTO.widgets.get(1).icon, is(WIDGET2_ICON)); + assertThat(pageDTO.widgets.get(1).staticIcon, is(false)); assertThat(pageDTO.widgets.get(1).state, is("ON")); assertThat(pageDTO.widgets.get(1).item, notNullValue()); assertThat(pageDTO.widgets.get(1).item.name, is(ITEM_NAME)); assertThat(pageDTO.widgets.get(1).item.state, is("50")); + + assertThat(pageDTO.widgets.get(2).widgetId, is(WIDGET3_ID)); + assertThat(pageDTO.widgets.get(2).label, is(WIDGET3_LABEL)); + assertThat(pageDTO.widgets.get(2).labelcolor, nullValue()); + assertThat(pageDTO.widgets.get(2).valuecolor, nullValue()); + assertThat(pageDTO.widgets.get(2).iconcolor, nullValue()); + assertThat(pageDTO.widgets.get(2).icon, is(WIDGET3_ICON)); + assertThat(pageDTO.widgets.get(2).staticIcon, is(true)); + assertThat(pageDTO.widgets.get(2).state, is("ON")); + assertThat(pageDTO.widgets.get(2).item, notNullValue()); + assertThat(pageDTO.widgets.get(2).item.name, is(ITEM_NAME)); + assertThat(pageDTO.widgets.get(2).item.state, is("50")); } private void configureItemUIRegistry(State state1, State state2) throws ItemNotFoundException { @@ -299,9 +374,10 @@ private void configureItemUIRegistry(State state1, State state2) throws ItemNotF when(itemUIRegistryMock.getItem(LABEL_COLOR_ITEM_NAME)).thenReturn(labelColorItem); when(itemUIRegistryMock.getItem(VALUE_COLOR_ITEM_NAME)).thenReturn(valueColorItem); when(itemUIRegistryMock.getItem(ICON_COLOR_ITEM_NAME)).thenReturn(iconColorItem); + when(itemUIRegistryMock.getItem(ICON_ITEM_NAME)).thenReturn(iconItem); when(itemUIRegistryMock.getWidgetId(widgets.get(0))).thenReturn(WIDGET1_ID); - when(itemUIRegistryMock.getCategory(widgets.get(0))).thenReturn(""); + when(itemUIRegistryMock.getCategory(widgets.get(0))).thenReturn(WIDGET1_ICON); when(itemUIRegistryMock.getLabel(widgets.get(0))).thenReturn(WIDGET1_LABEL); when(itemUIRegistryMock.getVisiblity(widgets.get(0))).thenReturn(true); when(itemUIRegistryMock.getLabelColor(widgets.get(0))).thenReturn("GREEN"); @@ -310,13 +386,22 @@ private void configureItemUIRegistry(State state1, State state2) throws ItemNotF when(itemUIRegistryMock.getState(widgets.get(0))).thenReturn(state1); when(itemUIRegistryMock.getWidgetId(widgets.get(1))).thenReturn(WIDGET2_ID); - when(itemUIRegistryMock.getCategory(widgets.get(1))).thenReturn(""); + when(itemUIRegistryMock.getCategory(widgets.get(1))).thenReturn(WIDGET2_ICON); when(itemUIRegistryMock.getLabel(widgets.get(1))).thenReturn(WIDGET2_LABEL); when(itemUIRegistryMock.getVisiblity(widgets.get(1))).thenReturn(true); when(itemUIRegistryMock.getLabelColor(widgets.get(1))).thenReturn(null); when(itemUIRegistryMock.getValueColor(widgets.get(1))).thenReturn(null); when(itemUIRegistryMock.getIconColor(widgets.get(1))).thenReturn(null); when(itemUIRegistryMock.getState(widgets.get(1))).thenReturn(state2); + + when(itemUIRegistryMock.getWidgetId(widgets.get(2))).thenReturn(WIDGET3_ID); + when(itemUIRegistryMock.getCategory(widgets.get(2))).thenReturn(WIDGET3_ICON); + when(itemUIRegistryMock.getLabel(widgets.get(2))).thenReturn(WIDGET3_LABEL); + when(itemUIRegistryMock.getVisiblity(widgets.get(2))).thenReturn(true); + when(itemUIRegistryMock.getLabelColor(widgets.get(2))).thenReturn(null); + when(itemUIRegistryMock.getValueColor(widgets.get(2))).thenReturn(null); + when(itemUIRegistryMock.getIconColor(widgets.get(2))).thenReturn(null); + when(itemUIRegistryMock.getState(widgets.get(2))).thenReturn(state2); } private EList initSitemapWidgets() { @@ -330,35 +415,65 @@ private EList initSitemapWidgets() { when(w1.eClass()).thenReturn(sliderEClass); when(w1.getLabel()).thenReturn(WIDGET1_LABEL); when(w1.getItem()).thenReturn(ITEM_NAME); + when(w1.getIcon()).thenReturn(null); + when(w1.getStaticIcon()).thenReturn(null); + + // add icon rules to the mock widget: + IconRule iconRule = mock(IconRule.class); + Condition conditon0 = mock(Condition.class); + when(conditon0.getItem()).thenReturn(ICON_ITEM_NAME); + EList conditions0 = new BasicEList<>(); + conditions0.add(conditon0); + when(iconRule.getConditions()).thenReturn(conditions0); + EList iconRules = new BasicEList<>(); + iconRules.add(iconRule); + when(w1.getDynamicIcon()).thenReturn(iconRules); // add visibility rules to the mock widget: VisibilityRule visibilityRule = mock(VisibilityRule.class); - when(visibilityRule.getItem()).thenReturn(VISIBILITY_RULE_ITEM_NAME); - BasicEList visibilityRules = new BasicEList<>(1); + Condition conditon = mock(Condition.class); + when(conditon.getItem()).thenReturn(VISIBILITY_RULE_ITEM_NAME); + EList conditions = new BasicEList<>(); + conditions.add(conditon); + when(visibilityRule.getConditions()).thenReturn(conditions); + EList visibilityRules = new BasicEList<>(1); visibilityRules.add(visibilityRule); when(w1.getVisibility()).thenReturn(visibilityRules); // add label color conditions to the item: ColorArray labelColor = mock(ColorArray.class); - when(labelColor.getItem()).thenReturn(LABEL_COLOR_ITEM_NAME); + Condition conditon1 = mock(Condition.class); + when(conditon1.getItem()).thenReturn(LABEL_COLOR_ITEM_NAME); + EList conditions1 = new BasicEList<>(); + conditions1.add(conditon1); + when(labelColor.getConditions()).thenReturn(conditions1); EList labelColors = new BasicEList<>(); labelColors.add(labelColor); when(w1.getLabelColor()).thenReturn(labelColors); // add value color conditions to the item: ColorArray valueColor = mock(ColorArray.class); - when(valueColor.getItem()).thenReturn(VALUE_COLOR_ITEM_NAME); + Condition conditon2 = mock(Condition.class); + when(conditon2.getItem()).thenReturn(VALUE_COLOR_ITEM_NAME); + EList conditions2 = new BasicEList<>(); + conditions2.add(conditon2); + when(valueColor.getConditions()).thenReturn(conditions2); EList valueColors = new BasicEList<>(); valueColors.add(valueColor); when(w1.getValueColor()).thenReturn(valueColors); // add icon color conditions to the item: ColorArray iconColor = mock(ColorArray.class); - when(iconColor.getItem()).thenReturn(ICON_COLOR_ITEM_NAME); + Condition conditon3 = mock(Condition.class); + when(conditon3.getItem()).thenReturn(ICON_COLOR_ITEM_NAME); + EList conditions3 = new BasicEList<>(); + conditions3.add(conditon3); + when(iconColor.getConditions()).thenReturn(conditions3); EList iconColors = new BasicEList<>(); iconColors.add(iconColor); when(w1.getIconColor()).thenReturn(iconColors); + iconRules = new BasicEList<>(); visibilityRules = new BasicEList<>(); labelColors = new BasicEList<>(); valueColors = new BasicEList<>(); @@ -371,14 +486,30 @@ private EList initSitemapWidgets() { when(w2.eClass()).thenReturn(switchEClass); when(w2.getLabel()).thenReturn(WIDGET2_LABEL); when(w2.getItem()).thenReturn(ITEM_NAME); + when(w2.getIcon()).thenReturn(WIDGET2_ICON); + when(w2.getStaticIcon()).thenReturn(null); + when(w2.getDynamicIcon()).thenReturn(iconRules); when(w2.getVisibility()).thenReturn(visibilityRules); when(w2.getLabelColor()).thenReturn(labelColors); when(w2.getValueColor()).thenReturn(valueColors); when(w2.getIconColor()).thenReturn(iconColors); - BasicEList widgets = new BasicEList<>(2); + Widget w3 = mock(Widget.class); + when(w3.eClass()).thenReturn(switchEClass); + when(w3.getLabel()).thenReturn(WIDGET3_LABEL); + when(w3.getItem()).thenReturn(ITEM_NAME); + when(w3.getIcon()).thenReturn(null); + when(w3.getStaticIcon()).thenReturn(WIDGET3_ICON); + when(w3.getDynamicIcon()).thenReturn(iconRules); + when(w3.getVisibility()).thenReturn(visibilityRules); + when(w3.getLabelColor()).thenReturn(labelColors); + when(w3.getValueColor()).thenReturn(valueColors); + when(w3.getIconColor()).thenReturn(iconColors); + + EList widgets = new BasicEList<>(3); widgets.add(w1); widgets.add(w2); + widgets.add(w3); return widgets; } diff --git a/bundles/org.openhab.core.model.sitemap/src/org/openhab/core/model/sitemap/Sitemap.xtext b/bundles/org.openhab.core.model.sitemap/src/org/openhab/core/model/sitemap/Sitemap.xtext index 2a3088db948..a188553c44b 100644 --- a/bundles/org.openhab.core.model.sitemap/src/org/openhab/core/model/sitemap/Sitemap.xtext +++ b/bundles/org.openhab.core.model.sitemap/src/org/openhab/core/model/sitemap/Sitemap.xtext @@ -25,7 +25,9 @@ LinkableWidget: Frame: {Frame} 'Frame' (('item=' item=ItemRef)? & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & ('iconcolor=[' (IconColor+=ColorArray (',' IconColor+=ColorArray)* ']'))? & @@ -33,7 +35,9 @@ Frame: Text: {Text} 'Text' (('item=' item=ItemRef)? & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & ('iconcolor=[' (IconColor+=ColorArray (',' IconColor+=ColorArray)* ']'))? & @@ -41,7 +45,9 @@ Text: Group: 'Group' (('item=' item=GroupItemRef) & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & ('iconcolor=[' (IconColor+=ColorArray (',' IconColor+=ColorArray)* ']'))? & @@ -49,7 +55,9 @@ Group: Image: 'Image' (('item=' item=ItemRef)? & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('url=' url=STRING)? & ('refresh=' refresh=INT)? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & @@ -58,7 +66,9 @@ Image: Video: 'Video' (('item=' item=ItemRef)? & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('url=' url=STRING) & ('encoding=' encoding=STRING)? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & @@ -67,7 +77,9 @@ Video: Chart: 'Chart' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('service=' service=STRING)? & ('refresh=' refresh=INT)? & ('period=' period=ID) & ('legend=' legend=BOOLEAN_OBJECT)? & ('forceasitem=' forceAsItem=BOOLEAN_OBJECT)? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & @@ -78,7 +90,9 @@ Chart: Webview: 'Webview' (('item=' item=ItemRef)? & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('height=' height=INT)? & ('url=' url=STRING) & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & @@ -87,7 +101,9 @@ Webview: Switch: 'Switch' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('mappings=[' mappings+=Mapping (',' mappings+=Mapping)* ']')? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & @@ -96,7 +112,9 @@ Switch: Mapview: 'Mapview' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('height=' height=INT)? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & @@ -105,7 +123,9 @@ Mapview: Slider: 'Slider' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('sendFrequency=' frequency=INT)? & (switchEnabled?='switchSupport')? & ('minValue=' minValue=Number)? & ('maxValue=' maxValue=Number)? & ('step=' step=Number)? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & @@ -115,7 +135,9 @@ Slider: Selection: 'Selection' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('mappings=[' mappings+=Mapping (',' mappings+=Mapping)* ']')? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & @@ -124,7 +146,9 @@ Selection: Setpoint: 'Setpoint' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('minValue=' minValue=Number)? & ('maxValue=' maxValue=Number)? & ('step=' step=Number)? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & @@ -133,7 +157,9 @@ Setpoint: Colorpicker: 'Colorpicker' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('sendFrequency=' frequency=INT)? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & @@ -142,7 +168,9 @@ Colorpicker: Input: 'Input' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('inputHint=' inputHint=STRING)? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & @@ -151,7 +179,9 @@ Input: Default: 'Default' (('item=' item=ItemRef) & ('label=' label=(ID | STRING))? & - (('icon=' icon=Icon) | ('staticIcon=' staticIcon=Icon))? & + (('icon=' icon=Icon) | + ('icon=[' (dynamicIcon+=IconRule (',' dynamicIcon+=IconRule)*) ']') | + ('staticIcon=' staticIcon=Icon))? & ('height=' height=INT)? & ('labelcolor=[' (LabelColor+=ColorArray (',' LabelColor+=ColorArray)* ']'))? & ('valuecolor=[' (ValueColor+=ColorArray (',' ValueColor+=ColorArray)* ']'))? & @@ -162,7 +192,7 @@ Mapping: cmd=Command '=' label=(ID | STRING); VisibilityRule: - (item=ID) (condition=('==' | '>' | '<' | '>=' | '<=' | '!=')) (sign=('-' | '+'))? (state=XState); + conditions+=Condition ('AND' conditions+=Condition)*; ItemRef: ID; @@ -178,8 +208,13 @@ IconName: (ID '-')* ID; ColorArray: - ((item=ID)? (condition=('==' | '>' | '<' | '>=' | '<=' | '!='))? (sign=('-' | '+'))? (state=XState) '=')? - (arg=STRING); + ((conditions+=Condition ('AND' conditions+=Condition)*) '=')? (arg=STRING); + +IconRule: + ((conditions+=Condition ('AND' conditions+=Condition)*) '=')? (arg=Icon); + +Condition: + (item=ID)? (condition=('==' | '>' | '<' | '>=' | '<=' | '!='))? (sign=('-' | '+'))? (state=XState); Command returns ecore::EString: Number | ID | STRING; diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java index 39c0f47dce5..abca5fcffe4 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/components/UIComponentSitemapProvider.java @@ -31,6 +31,7 @@ import org.openhab.core.model.core.ModelRepositoryChangeListener; import org.openhab.core.model.sitemap.SitemapProvider; import org.openhab.core.model.sitemap.sitemap.ColorArray; +import org.openhab.core.model.sitemap.sitemap.IconRule; import org.openhab.core.model.sitemap.sitemap.LinkableWidget; import org.openhab.core.model.sitemap.sitemap.Mapping; import org.openhab.core.model.sitemap.sitemap.Sitemap; @@ -41,9 +42,11 @@ import org.openhab.core.model.sitemap.sitemap.impl.ChartImpl; import org.openhab.core.model.sitemap.sitemap.impl.ColorArrayImpl; import org.openhab.core.model.sitemap.sitemap.impl.ColorpickerImpl; +import org.openhab.core.model.sitemap.sitemap.impl.ConditionImpl; import org.openhab.core.model.sitemap.sitemap.impl.DefaultImpl; import org.openhab.core.model.sitemap.sitemap.impl.FrameImpl; import org.openhab.core.model.sitemap.sitemap.impl.GroupImpl; +import org.openhab.core.model.sitemap.sitemap.impl.IconRuleImpl; import org.openhab.core.model.sitemap.sitemap.impl.ImageImpl; import org.openhab.core.model.sitemap.sitemap.impl.InputImpl; import org.openhab.core.model.sitemap.sitemap.impl.MappingImpl; @@ -75,6 +78,7 @@ * * @author Yannick Schaus - Initial contribution * @author Laurent Garnier - icon color support for all widgets + * @author Laurent Garnier - new icon parameter based on conditional rules + multiple AND conditions */ @NonNullByDefault @Component(service = SitemapProvider.class) @@ -90,6 +94,7 @@ public class UIComponentSitemapProvider implements SitemapProvider, RegistryChan .compile("(?[A-Za-z]\\w*)\\s*(?==|!=|<=|>=|<|>)\\s*(?\\+|-)?(?.+)"); private static final Pattern COLOR_PATTERN = Pattern.compile( "((?[A-Za-z]\\w*)?\\s*((?==|!=|<=|>=|<|>)\\s*(?\\+|-)?(?[^=]+))?\\s*=)?\\s*(?\\S+)"); + private static final Pattern ICON_PATTERN = COLOR_PATTERN; private Map sitemaps = new HashMap<>(); private @Nullable UIComponentRegistryFactory componentRegistryFactory; @@ -285,6 +290,7 @@ protected Sitemap buildSitemap(RootUIComponent rootComponent) { addLabelColor(widget.getLabelColor(), component); addValueColor(widget.getValueColor(), component); addIconColor(widget.getIconColor(), component); + addDynamicIcon(widget.getDynamicIcon(), component); } return widget; @@ -341,10 +347,12 @@ private void addWidgetVisibility(EList visibility, UIComponent c if (matcher.matches()) { VisibilityRuleImpl visibilityRule = (VisibilityRuleImpl) SitemapFactory.eINSTANCE .createVisibilityRule(); - visibilityRule.setItem(matcher.group("item")); - visibilityRule.setCondition(matcher.group("condition")); - visibilityRule.setSign(matcher.group("sign")); - visibilityRule.setState(matcher.group("state")); + ConditionImpl condition = (ConditionImpl) SitemapFactory.eINSTANCE.createCondition(); + condition.setItem(matcher.group("item")); + condition.setCondition(matcher.group("condition")); + condition.setSign(matcher.group("sign")); + condition.setState(matcher.group("state")); + visibilityRule.eSet(SitemapPackage.VISIBILITY_RULE__CONDITIONS, condition); visibility.add(visibilityRule); } else { logger.warn("Syntax error in visibility rule '{}' for widget {}", sourceVisibility, @@ -374,10 +382,12 @@ private void addColor(EList color, UIComponent component, String key Matcher matcher = COLOR_PATTERN.matcher(sourceColor.toString()); if (matcher.matches()) { ColorArrayImpl colorArray = (ColorArrayImpl) SitemapFactory.eINSTANCE.createColorArray(); - colorArray.setItem(matcher.group("item")); - colorArray.setCondition(matcher.group("condition")); - colorArray.setSign(matcher.group("sign")); - colorArray.setState(matcher.group("state")); + ConditionImpl condition = (ConditionImpl) SitemapFactory.eINSTANCE.createCondition(); + condition.setItem(matcher.group("item")); + condition.setCondition(matcher.group("condition")); + condition.setSign(matcher.group("sign")); + condition.setState(matcher.group("state")); + colorArray.eSet(SitemapPackage.COLOR_ARRAY__CONDITIONS, condition); colorArray.setArg(matcher.group("arg")); color.add(colorArray); } else { @@ -389,6 +399,29 @@ private void addColor(EList color, UIComponent component, String key } } + private void addDynamicIcon(EList iconDef, UIComponent component) { + if (component.getConfig() != null && component.getConfig().containsKey("icon")) { + for (Object sourceIcon : (Collection) component.getConfig().get("icon")) { + if (sourceIcon instanceof String) { + Matcher matcher = ICON_PATTERN.matcher(sourceIcon.toString()); + if (matcher.matches()) { + IconRuleImpl iconRule = (IconRuleImpl) SitemapFactory.eINSTANCE.createIconRule(); + ConditionImpl condition = (ConditionImpl) SitemapFactory.eINSTANCE.createCondition(); + condition.setItem(matcher.group("item")); + condition.setCondition(matcher.group("condition")); + condition.setSign(matcher.group("sign")); + condition.setState(matcher.group("state")); + iconRule.eSet(SitemapPackage.ICON_RULE__CONDITIONS, condition); + iconRule.setArg(matcher.group("arg")); + iconDef.add(iconRule); + } else { + logger.warn("Syntax error in icon rule '{}' for widget {}", sourceIcon, component.getType()); + } + } + } + } + } + @Override public void addModelChangeListener(ModelRepositoryChangeListener listener) { modelChangeListeners.add(listener); diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java index c290fb52640..084f39c952c 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/internal/items/ItemUIRegistryImpl.java @@ -68,6 +68,7 @@ import org.openhab.core.model.sitemap.sitemap.ColorArray; import org.openhab.core.model.sitemap.sitemap.Default; import org.openhab.core.model.sitemap.sitemap.Group; +import org.openhab.core.model.sitemap.sitemap.IconRule; import org.openhab.core.model.sitemap.sitemap.LinkableWidget; import org.openhab.core.model.sitemap.sitemap.Mapping; import org.openhab.core.model.sitemap.sitemap.Sitemap; @@ -109,6 +110,7 @@ * @author Erdoan Hadzhiyusein - Adapted the class to work with the new DateTimeType * @author Laurent Garnier - new method getIconColor * @author Mark Herwege - new method getFormatPattern(widget), clean pattern + * @author Laurent Garnier - new icon parameter based on conditional rules + multiple AND conditions */ @NonNullByDefault @Component(immediate = true, configurationPid = "org.openhab.sitemap", // @@ -638,11 +640,14 @@ private String transform(String label, boolean matchTransform, @Nullable String // the default is the widget type name, e.g. "switch" String category = widgetTypeName.toLowerCase(); + String dynamicIcon = getDynamicIcon(w); // if an icon is defined for the widget, use it if (w.getIcon() != null) { category = w.getIcon(); } else if (w.getStaticIcon() != null) { category = w.getStaticIcon(); + } else if (dynamicIcon != null) { + category = dynamicIcon; } else { // otherwise check if any item ui provider provides an icon for this item String itemName = w.getItem(); @@ -800,6 +805,7 @@ private void copyProperties(Widget source, Widget target) { target.getLabelColor().addAll(EcoreUtil.copyAll(source.getLabelColor())); target.getValueColor().addAll(EcoreUtil.copyAll(source.getValueColor())); target.getIconColor().addAll(EcoreUtil.copyAll(source.getIconColor())); + target.getDynamicIcon().addAll(EcoreUtil.copyAll(source.getDynamicIcon())); } /** @@ -1149,152 +1155,156 @@ private boolean matchStateToValue(State state, String value, @Nullable String ma return matched; } - private @Nullable String processColorDefinition(@Nullable State state, @Nullable List colorList) { + private @Nullable String processColorDefinition(Widget w, @Nullable List colorList, String colorType) { // Sanity check - if (colorList == null) { + if (colorList == null || colorList.isEmpty()) { return null; } - if (colorList.isEmpty()) { - return null; - } - - String colorString = null; - - // Check for the "arg". If it doesn't exist, assume there's just an - // static colour - if (colorList.size() == 1 && colorList.get(0).getState() == null) { - colorString = colorList.get(0).getArg(); - } else { - // Loop through all elements looking for the definition associated - // 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) { - // If no state associated to the condition, we consider the condition as fulfilled. - // It allows defining a default color as last condition in particular. - colorString = color.getArg(); - break; - } - - // 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()); - } - } + logger.debug("Checking {} color for widget '{}'.", colorType, w.getLabel()); - // Handle the sign - String value; - if (color.getSign() != null) { - value = color.getSign() + color.getState(); - } else { - value = color.getState(); - } + String colorString = null; - if (cmpState != null && matchStateToValue(cmpState, value, color.getCondition())) { - // We have the color for this value - break! - colorString = color.getArg(); - break; - } + // Loop through all elements looking for the definition associated + // with the supplied value + for (ColorArray rule : colorList) { + if (allConditionsOk(rule.getConditions(), w)) { + // We have the color for this value - break! + colorString = rule.getArg(); + break; } } - // Remove quotes off the colour - if they exist if (colorString == null) { + logger.debug("No {} color found for widget '{}'.", colorType, w.getLabel()); return null; } + // Remove quotes off the colour - if they exist if (colorString.startsWith("\"") && colorString.endsWith("\"")) { colorString = colorString.substring(1, colorString.length() - 1); } + logger.debug("{} color for widget '{}' is '{}'.", colorType, w.getLabel(), colorString); return colorString; } @Override public @Nullable String getLabelColor(Widget w) { - return processColorDefinition(getState(w), w.getLabelColor()); + return processColorDefinition(w, w.getLabelColor(), "label"); } @Override public @Nullable String getValueColor(Widget w) { - return processColorDefinition(getState(w), w.getValueColor()); + return processColorDefinition(w, w.getValueColor(), "value"); } @Override public @Nullable String getIconColor(Widget w) { - return processColorDefinition(getState(w), w.getIconColor()); + return processColorDefinition(w, w.getIconColor(), "icon"); } @Override - public boolean getVisiblity(Widget w) { - // Default to visible if parameters not set - List ruleList = w.getVisibility(); - if (ruleList == null) { - return true; - } - if (ruleList.isEmpty()) { - return true; + public @Nullable String getDynamicIcon(Widget w) { + List ruleList = w.getDynamicIcon(); + // Sanity check + if (ruleList == null || ruleList.isEmpty()) { + return null; } - logger.debug("Checking visiblity for widget '{}'.", w.getLabel()); + logger.debug("Checking icon for widget '{}'.", w.getLabel()); - for (VisibilityRule rule : w.getVisibility()) { - String itemName = rule.getItem(); - if (itemName == null) { - continue; - } - if (rule.getState() == null) { - continue; + String icon = null; + + // Loop through all elements looking for the definition associated + // with the supplied value + for (IconRule rule : ruleList) { + if (allConditionsOk(rule.getConditions(), w)) { + // We have the icon for this value - break! + icon = rule.getArg(); + break; } + } - // 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.warn("Cannot retrieve visibility item {} for widget {}", rule.getItem(), - w.eClass().getInstanceTypeName()); + if (icon == null) { + logger.debug("No icon found for widget '{}'.", w.getLabel()); + return null; + } - // Default to visible! - return true; - } + // Remove quotes off the icon - if they exist + if (icon.startsWith("\"") && icon.endsWith("\"")) { + icon = icon.substring(1, icon.length() - 1); + } + logger.debug("icon for widget '{}' is '{}'.", w.getLabel(), icon); - // Get the item state - State state = item.getState(); + return icon; + } - // Handle the sign - String value; - if (rule.getSign() != null) { - value = rule.getSign() + rule.getState(); - } else { - value = rule.getState(); - } + @Override + public boolean getVisiblity(Widget w) { + // Default to visible if parameters not set + List ruleList = w.getVisibility(); + if (ruleList == null || ruleList.isEmpty()) { + return true; + } + + logger.debug("Checking visiblity for widget '{}'.", w.getLabel()); - if (matchStateToValue(state, value, rule.getCondition())) { - // We have the name for this value! + for (VisibilityRule rule : ruleList) { + if (allConditionsOk(rule.getConditions(), w)) { 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; } + private boolean allConditionsOk(@Nullable List conditions, + Widget w) { + boolean allConditionsOk = true; + if (conditions != null) { + State defaultState = getState(w); + + // Go through all AND conditions + for (org.openhab.core.model.sitemap.sitemap.Condition condition : conditions) { + // Use a local state variable in case it gets overridden below + State state = defaultState; + + // If there's an item defined here, get its state + String itemName = condition.getItem(); + if (itemName != null) { + // Try and find the item to test. + Item item; + try { + item = itemRegistry.getItem(itemName); + + // Get the item state + state = item.getState(); + } catch (ItemNotFoundException e) { + logger.warn("Cannot retrieve item {} for widget {}", itemName, + w.eClass().getInstanceTypeName()); + } + } + + // Handle the sign + String value; + if (condition.getSign() != null) { + value = condition.getSign() + condition.getState(); + } else { + value = condition.getState(); + } + + if (state == null || !matchStateToValue(state, value, condition.getCondition())) { + allConditionsOk = false; + break; + } + } + } + return allConditionsOk; + } + enum Condition { EQUAL("=="), GTE(">="), diff --git a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/items/ItemUIRegistry.java b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/items/ItemUIRegistry.java index a979543d4cf..eac87bc7a62 100644 --- a/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/items/ItemUIRegistry.java +++ b/bundles/org.openhab.core.ui/src/main/java/org/openhab/core/ui/items/ItemUIRegistry.java @@ -35,6 +35,7 @@ * @author Chris Jackson - Initial contribution * @author Laurent Garnier - new method getIconColor * @author Mark Herwege - new method getFormatPattern + * @author Laurent Garnier - new method getDynamicIcon */ @NonNullByDefault public interface ItemUIRegistry extends ItemRegistry, ItemUIProvider { @@ -170,6 +171,16 @@ public interface ItemUIRegistry extends ItemRegistry, ItemUIProvider { @Nullable String getIconColor(Widget w); + /** + * Gets the dynamic icon for the widget. Checks conditional statements to + * find the icon based on the item value + * + * @param w Widget + * @return String with the icon reference + */ + @Nullable + String getDynamicIcon(Widget w); + /** * Gets the widget visibility based on the item state * diff --git a/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java b/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java index cd00015cac1..16b5651f6c0 100644 --- a/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java +++ b/bundles/org.openhab.core.ui/src/test/java/org/openhab/core/ui/internal/items/ItemUIRegistryImplTest.java @@ -27,6 +27,7 @@ import javax.measure.quantity.Temperature; import org.eclipse.emf.common.util.BasicEList; +import org.eclipse.emf.ecore.EClass; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -60,7 +61,9 @@ import org.openhab.core.library.types.StringType; import org.openhab.core.model.sitemap.sitemap.ColorArray; import org.openhab.core.model.sitemap.sitemap.Colorpicker; +import org.openhab.core.model.sitemap.sitemap.Condition; import org.openhab.core.model.sitemap.sitemap.Group; +import org.openhab.core.model.sitemap.sitemap.IconRule; import org.openhab.core.model.sitemap.sitemap.Image; import org.openhab.core.model.sitemap.sitemap.Mapping; import org.openhab.core.model.sitemap.sitemap.Mapview; @@ -70,6 +73,7 @@ import org.openhab.core.model.sitemap.sitemap.Slider; import org.openhab.core.model.sitemap.sitemap.Switch; import org.openhab.core.model.sitemap.sitemap.Text; +import org.openhab.core.model.sitemap.sitemap.VisibilityRule; import org.openhab.core.model.sitemap.sitemap.Widget; import org.openhab.core.types.CommandDescriptionBuilder; import org.openhab.core.types.CommandOption; @@ -83,6 +87,7 @@ /** * @author Kai Kreuzer - Initial contribution + * @author Laurent Garnier - Tests added for getCategory and getVisiblity including case with multiple AND conditions */ @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) @@ -736,18 +741,27 @@ public void getLabelColorLabelWithDecimalValue() { when(widgetMock.getLabel()).thenReturn(testLabel); - ColorArray colorArray = mock(ColorArray.class); - when(colorArray.getState()).thenReturn("21"); - when(colorArray.getCondition()).thenReturn("<"); - when(colorArray.getArg()).thenReturn("yellow"); - BasicEList colorArrays = new BasicEList<>(); - colorArrays.add(colorArray); - when(widgetMock.getLabelColor()).thenReturn(colorArrays); + Condition conditon = mock(Condition.class); + when(conditon.getState()).thenReturn("21"); + when(conditon.getCondition()).thenReturn("<"); + BasicEList conditions = new BasicEList<>(); + conditions.add(conditon); + ColorArray rule = mock(ColorArray.class); + when(rule.getConditions()).thenReturn(conditions); + when(rule.getArg()).thenReturn("yellow"); + BasicEList rules = new BasicEList<>(); + rules.add(rule); + when(widgetMock.getLabelColor()).thenReturn(rules); when(itemMock.getState()).thenReturn(new DecimalType(10f / 3f)); String color = uiRegistry.getLabelColor(widgetMock); assertEquals("yellow", color); + + when(itemMock.getState()).thenReturn(new DecimalType(21f)); + + color = uiRegistry.getLabelColor(widgetMock); + assertNull(color); } @Test @@ -756,18 +770,27 @@ public void getLabelColorLabelWithUnitValue() { when(widgetMock.getLabel()).thenReturn(testLabel); - ColorArray colorArray = mock(ColorArray.class); - when(colorArray.getState()).thenReturn("20"); - when(colorArray.getCondition()).thenReturn("=="); - when(colorArray.getArg()).thenReturn("yellow"); - BasicEList colorArrays = new BasicEList<>(); - colorArrays.add(colorArray); - when(widgetMock.getLabelColor()).thenReturn(colorArrays); + Condition conditon = mock(Condition.class); + when(conditon.getState()).thenReturn("20"); + when(conditon.getCondition()).thenReturn("=="); + BasicEList conditions = new BasicEList<>(); + conditions.add(conditon); + ColorArray rule = mock(ColorArray.class); + when(rule.getConditions()).thenReturn(conditions); + when(rule.getArg()).thenReturn("yellow"); + BasicEList rules = new BasicEList<>(); + rules.add(rule); + when(widgetMock.getLabelColor()).thenReturn(rules); when(itemMock.getState()).thenReturn(new QuantityType<>("20 °C")); String color = uiRegistry.getLabelColor(widgetMock); assertEquals("yellow", color); + + when(itemMock.getState()).thenReturn(new QuantityType<>("20.1 °C")); + + color = uiRegistry.getLabelColor(widgetMock); + assertNull(color); } @Test @@ -925,44 +948,108 @@ public void getLabelColorDefaultColor() { when(widgetMock.getLabel()).thenReturn(testLabel); - ColorArray colorArray = mock(ColorArray.class); - when(colorArray.getState()).thenReturn("21"); - when(colorArray.getCondition()).thenReturn("<"); - when(colorArray.getArg()).thenReturn("yellow"); - BasicEList colorArrays = new BasicEList<>(); - colorArrays.add(colorArray); - ColorArray colorArray2 = mock(ColorArray.class); - when(colorArray2.getState()).thenReturn(null); - when(colorArray2.getCondition()).thenReturn(null); - when(colorArray2.getArg()).thenReturn("blue"); - colorArrays.add(colorArray2); - when(widgetMock.getLabelColor()).thenReturn(colorArrays); + Condition conditon = mock(Condition.class); + when(conditon.getState()).thenReturn("18"); + when(conditon.getCondition()).thenReturn(">="); + Condition conditon2 = mock(Condition.class); + when(conditon2.getState()).thenReturn("21"); + when(conditon2.getCondition()).thenReturn("<"); + BasicEList conditions = new BasicEList<>(); + conditions.add(conditon); + conditions.add(conditon2); + ColorArray rule = mock(ColorArray.class); + when(rule.getConditions()).thenReturn(conditions); + when(rule.getArg()).thenReturn("yellow"); + BasicEList rules = new BasicEList<>(); + rules.add(rule); + Condition conditon3 = mock(Condition.class); + when(conditon3.getState()).thenReturn("21"); + when(conditon3.getCondition()).thenReturn(">="); + Condition conditon4 = mock(Condition.class); + when(conditon4.getState()).thenReturn("24"); + when(conditon4.getCondition()).thenReturn("<"); + BasicEList conditions2 = new BasicEList<>(); + conditions2.add(conditon3); + conditions2.add(conditon4); + ColorArray rule2 = mock(ColorArray.class); + when(rule2.getConditions()).thenReturn(conditions2); + when(rule2.getArg()).thenReturn("red"); + rules.add(rule2); + BasicEList conditions5 = new BasicEList<>(); + ColorArray rule3 = mock(ColorArray.class); + when(rule3.getConditions()).thenReturn(conditions5); + when(rule3.getArg()).thenReturn("blue"); + rules.add(rule3); + when(widgetMock.getLabelColor()).thenReturn(rules); - when(itemMock.getState()).thenReturn(new DecimalType(21.0)); + when(itemMock.getState()).thenReturn(new DecimalType(20.9)); String color = uiRegistry.getLabelColor(widgetMock); + assertEquals("yellow", color); + + when(itemMock.getState()).thenReturn(new DecimalType(23.5)); + + color = uiRegistry.getLabelColor(widgetMock); + assertEquals("red", color); + + when(itemMock.getState()).thenReturn(new DecimalType(24.0)); + + color = uiRegistry.getLabelColor(widgetMock); + assertEquals("blue", color); + + when(itemMock.getState()).thenReturn(new DecimalType(17.5)); + + color = uiRegistry.getLabelColor(widgetMock); + assertEquals("blue", color); + + conditions5 = null; + + when(itemMock.getState()).thenReturn(new DecimalType(24.0)); + + color = uiRegistry.getLabelColor(widgetMock); + assertEquals("blue", color); + + when(itemMock.getState()).thenReturn(new DecimalType(17.5)); + + color = uiRegistry.getLabelColor(widgetMock); assertEquals("blue", color); } @Test public void getValueColor() { - ColorArray colorArray = mock(ColorArray.class); - when(colorArray.getState()).thenReturn("21"); - when(colorArray.getCondition()).thenReturn("<"); - when(colorArray.getArg()).thenReturn("yellow"); - BasicEList colorArrays = new BasicEList<>(); - colorArrays.add(colorArray); - ColorArray colorArray2 = mock(ColorArray.class); - when(colorArray2.getState()).thenReturn("24"); - when(colorArray2.getCondition()).thenReturn("<"); - when(colorArray2.getArg()).thenReturn("red"); - colorArrays.add(colorArray2); - ColorArray colorArray3 = mock(ColorArray.class); - when(colorArray3.getState()).thenReturn(null); - when(colorArray3.getCondition()).thenReturn(null); - when(colorArray3.getArg()).thenReturn("blue"); - colorArrays.add(colorArray3); - when(widgetMock.getValueColor()).thenReturn(colorArrays); + Condition conditon = mock(Condition.class); + when(conditon.getState()).thenReturn("18"); + when(conditon.getCondition()).thenReturn(">="); + Condition conditon2 = mock(Condition.class); + when(conditon2.getState()).thenReturn("21"); + when(conditon2.getCondition()).thenReturn("<"); + BasicEList conditions = new BasicEList<>(); + conditions.add(conditon); + conditions.add(conditon2); + ColorArray rule = mock(ColorArray.class); + when(rule.getConditions()).thenReturn(conditions); + when(rule.getArg()).thenReturn("yellow"); + BasicEList rules = new BasicEList<>(); + rules.add(rule); + Condition conditon3 = mock(Condition.class); + when(conditon3.getState()).thenReturn("21"); + when(conditon3.getCondition()).thenReturn(">="); + Condition conditon4 = mock(Condition.class); + when(conditon4.getState()).thenReturn("24"); + when(conditon4.getCondition()).thenReturn("<"); + BasicEList conditions2 = new BasicEList<>(); + conditions2.add(conditon3); + conditions2.add(conditon4); + ColorArray rule2 = mock(ColorArray.class); + when(rule2.getConditions()).thenReturn(conditions2); + when(rule2.getArg()).thenReturn("red"); + rules.add(rule2); + BasicEList conditions5 = new BasicEList<>(); + ColorArray rule3 = mock(ColorArray.class); + when(rule3.getConditions()).thenReturn(conditions5); + when(rule3.getArg()).thenReturn("blue"); + rules.add(rule3); + when(widgetMock.getValueColor()).thenReturn(rules); when(itemMock.getState()).thenReturn(new DecimalType(20.9)); @@ -974,7 +1061,24 @@ public void getValueColor() { color = uiRegistry.getValueColor(widgetMock); assertEquals("red", color); - when(itemMock.getState()).thenReturn(new DecimalType(30.0)); + when(itemMock.getState()).thenReturn(new DecimalType(24.0)); + + color = uiRegistry.getValueColor(widgetMock); + assertEquals("blue", color); + + when(itemMock.getState()).thenReturn(new DecimalType(17.5)); + + color = uiRegistry.getValueColor(widgetMock); + assertEquals("blue", color); + + conditions5 = null; + + when(itemMock.getState()).thenReturn(new DecimalType(24.0)); + + color = uiRegistry.getValueColor(widgetMock); + assertEquals("blue", color); + + when(itemMock.getState()).thenReturn(new DecimalType(17.5)); color = uiRegistry.getValueColor(widgetMock); assertEquals("blue", color); @@ -982,23 +1086,39 @@ public void getValueColor() { @Test public void getIconColor() { - ColorArray colorArray = mock(ColorArray.class); - when(colorArray.getState()).thenReturn("21"); - when(colorArray.getCondition()).thenReturn("<"); - when(colorArray.getArg()).thenReturn("yellow"); - BasicEList colorArrays = new BasicEList<>(); - colorArrays.add(colorArray); - ColorArray colorArray2 = mock(ColorArray.class); - when(colorArray2.getState()).thenReturn("24"); - when(colorArray2.getCondition()).thenReturn("<"); - when(colorArray2.getArg()).thenReturn("red"); - colorArrays.add(colorArray2); - ColorArray colorArray3 = mock(ColorArray.class); - when(colorArray3.getState()).thenReturn(null); - when(colorArray3.getCondition()).thenReturn(null); - when(colorArray3.getArg()).thenReturn("blue"); - colorArrays.add(colorArray3); - when(widgetMock.getIconColor()).thenReturn(colorArrays); + Condition conditon = mock(Condition.class); + when(conditon.getState()).thenReturn("18"); + when(conditon.getCondition()).thenReturn(">="); + Condition conditon2 = mock(Condition.class); + when(conditon2.getState()).thenReturn("21"); + when(conditon2.getCondition()).thenReturn("<"); + BasicEList conditions = new BasicEList<>(); + conditions.add(conditon); + conditions.add(conditon2); + ColorArray rule = mock(ColorArray.class); + when(rule.getConditions()).thenReturn(conditions); + when(rule.getArg()).thenReturn("yellow"); + BasicEList rules = new BasicEList<>(); + rules.add(rule); + Condition conditon3 = mock(Condition.class); + when(conditon3.getState()).thenReturn("21"); + when(conditon3.getCondition()).thenReturn(">="); + Condition conditon4 = mock(Condition.class); + when(conditon4.getState()).thenReturn("24"); + when(conditon4.getCondition()).thenReturn("<"); + BasicEList conditions2 = new BasicEList<>(); + conditions2.add(conditon3); + conditions2.add(conditon4); + ColorArray rule2 = mock(ColorArray.class); + when(rule2.getConditions()).thenReturn(conditions2); + when(rule2.getArg()).thenReturn("red"); + rules.add(rule2); + BasicEList conditions5 = new BasicEList<>(); + ColorArray rule3 = mock(ColorArray.class); + when(rule3.getConditions()).thenReturn(conditions5); + when(rule3.getArg()).thenReturn("blue"); + rules.add(rule3); + when(widgetMock.getIconColor()).thenReturn(rules); when(itemMock.getState()).thenReturn(new DecimalType(20.9)); @@ -1010,9 +1130,170 @@ public void getIconColor() { color = uiRegistry.getIconColor(widgetMock); assertEquals("red", color); - when(itemMock.getState()).thenReturn(new DecimalType(30.0)); + when(itemMock.getState()).thenReturn(new DecimalType(24.0)); + + color = uiRegistry.getIconColor(widgetMock); + assertEquals("blue", color); + + when(itemMock.getState()).thenReturn(new DecimalType(17.5)); + + color = uiRegistry.getIconColor(widgetMock); + assertEquals("blue", color); + + conditions5 = null; + + when(itemMock.getState()).thenReturn(new DecimalType(24.0)); color = uiRegistry.getIconColor(widgetMock); assertEquals("blue", color); + + when(itemMock.getState()).thenReturn(new DecimalType(17.5)); + + color = uiRegistry.getIconColor(widgetMock); + assertEquals("blue", color); + } + + @Test + public void getVisibility() { + Condition conditon = mock(Condition.class); + when(conditon.getState()).thenReturn("21"); + when(conditon.getCondition()).thenReturn(">="); + Condition conditon2 = mock(Condition.class); + when(conditon2.getState()).thenReturn("24"); + when(conditon2.getCondition()).thenReturn("<"); + BasicEList conditions = new BasicEList<>(); + conditions.add(conditon); + conditions.add(conditon2); + VisibilityRule rule = mock(VisibilityRule.class); + when(rule.getConditions()).thenReturn(conditions); + BasicEList rules = new BasicEList<>(); + rules.add(rule); + when(widgetMock.getVisibility()).thenReturn(rules); + + when(itemMock.getState()).thenReturn(new DecimalType(20.9)); + + assertFalse(uiRegistry.getVisiblity(widgetMock)); + + when(itemMock.getState()).thenReturn(new DecimalType(21.0)); + + assertTrue(uiRegistry.getVisiblity(widgetMock)); + + when(itemMock.getState()).thenReturn(new DecimalType(23.5)); + + assertTrue(uiRegistry.getVisiblity(widgetMock)); + + when(itemMock.getState()).thenReturn(new DecimalType(24.0)); + + assertFalse(uiRegistry.getVisiblity(widgetMock)); + } + + @Test + public void getCategoryWhenIconSet() { + EClass textEClass = mock(EClass.class); + when(textEClass.getName()).thenReturn("text"); + when(textEClass.getInstanceTypeName()).thenReturn("org.openhab.core.model.sitemap.Text"); + when(widgetMock.eClass()).thenReturn(textEClass); + when(widgetMock.getIcon()).thenReturn("temperature"); + when(widgetMock.getStaticIcon()).thenReturn(null); + when(widgetMock.getDynamicIcon()).thenReturn(null); + + String icon = uiRegistry.getCategory(widgetMock); + assertEquals("temperature", icon); + } + + @Test + public void getCategoryWhenStaticIconSet() { + EClass textEClass = mock(EClass.class); + when(textEClass.getName()).thenReturn("text"); + when(textEClass.getInstanceTypeName()).thenReturn("org.openhab.core.model.sitemap.Text"); + when(widgetMock.eClass()).thenReturn(textEClass); + when(widgetMock.getIcon()).thenReturn(null); + when(widgetMock.getStaticIcon()).thenReturn("temperature"); + when(widgetMock.getDynamicIcon()).thenReturn(null); + + String icon = uiRegistry.getCategory(widgetMock); + assertEquals("temperature", icon); + } + + @Test + public void getCategoryWhenDynamicIconSet() { + EClass textEClass = mock(EClass.class); + when(textEClass.getName()).thenReturn("text"); + when(textEClass.getInstanceTypeName()).thenReturn("org.openhab.core.model.sitemap.Text"); + when(widgetMock.eClass()).thenReturn(textEClass); + when(widgetMock.getIcon()).thenReturn(null); + when(widgetMock.getStaticIcon()).thenReturn(null); + Condition conditon = mock(Condition.class); + when(conditon.getState()).thenReturn("21"); + when(conditon.getCondition()).thenReturn(">="); + Condition conditon2 = mock(Condition.class); + when(conditon2.getState()).thenReturn("24"); + when(conditon2.getCondition()).thenReturn("<"); + BasicEList conditions = new BasicEList<>(); + conditions.add(conditon); + conditions.add(conditon2); + IconRule rule = mock(IconRule.class); + when(rule.getConditions()).thenReturn(conditions); + when(rule.getArg()).thenReturn("temperature"); + BasicEList rules = new BasicEList<>(); + rules.add(rule); + BasicEList conditions2 = new BasicEList<>(); + IconRule rule2 = mock(IconRule.class); + when(rule2.getConditions()).thenReturn(conditions2); + when(rule2.getArg()).thenReturn("humidity"); + rules.add(rule2); + when(widgetMock.getDynamicIcon()).thenReturn(rules); + + when(itemMock.getState()).thenReturn(new DecimalType(20.9)); + + String icon = uiRegistry.getCategory(widgetMock); + assertEquals("humidity", icon); + + when(itemMock.getState()).thenReturn(new DecimalType(21.0)); + + icon = uiRegistry.getCategory(widgetMock); + assertEquals("temperature", icon); + + when(itemMock.getState()).thenReturn(new DecimalType(23.5)); + + icon = uiRegistry.getCategory(widgetMock); + assertEquals("temperature", icon); + + when(itemMock.getState()).thenReturn(new DecimalType(24.0)); + + icon = uiRegistry.getCategory(widgetMock); + assertEquals("humidity", icon); + } + + @Test + public void getCategoryWhenIconSetOnItem() { + EClass textEClass = mock(EClass.class); + when(textEClass.getName()).thenReturn("text"); + when(textEClass.getInstanceTypeName()).thenReturn("org.openhab.core.model.sitemap.Text"); + when(widgetMock.eClass()).thenReturn(textEClass); + when(widgetMock.getIcon()).thenReturn(null); + when(widgetMock.getStaticIcon()).thenReturn(null); + when(widgetMock.getDynamicIcon()).thenReturn(null); + + when(itemMock.getCategory()).thenReturn("temperature"); + + String icon = uiRegistry.getCategory(widgetMock); + assertEquals("temperature", icon); + } + + @Test + public void getCategoryDefaultIcon() { + EClass textEClass = mock(EClass.class); + when(textEClass.getName()).thenReturn("text"); + when(textEClass.getInstanceTypeName()).thenReturn("org.openhab.core.model.sitemap.Text"); + when(widgetMock.eClass()).thenReturn(textEClass); + when(widgetMock.getIcon()).thenReturn(null); + when(widgetMock.getStaticIcon()).thenReturn(null); + when(widgetMock.getDynamicIcon()).thenReturn(null); + + when(itemMock.getCategory()).thenReturn(null); + + String icon = uiRegistry.getCategory(widgetMock); + assertEquals("text", icon); } }