From 52e36a02168c78f623a74e35084ac3dee4b2e18d Mon Sep 17 00:00:00 2001 From: lolodomo Date: Thu, 16 Mar 2023 18:59:10 +0100 Subject: [PATCH] [sitemap] Add new color keyword "itemValue" (#3453) Closes #3429 Signed-off-by: Laurent Garnier --- .../sitemap/internal/PageChangeListener.java | 9 +++++---- .../sitemap/internal/SitemapResource.java | 19 ++++++++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) 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 0f9f4eef4..263e6821e 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 @@ -239,9 +239,6 @@ public class PageChangeListener implements StateChangeListener { event.sitemapName = sitemapName; event.pageId = pageId; event.label = itemUIRegistry.getLabel(widget); - event.labelcolor = itemUIRegistry.getLabelColor(widget); - event.valuecolor = itemUIRegistry.getValueColor(widget); - event.iconcolor = itemUIRegistry.getIconColor(widget); event.widgetId = itemUIRegistry.getWidgetId(widget); event.visibility = itemUIRegistry.getVisiblity(widget); event.descriptionChanged = false; @@ -249,6 +246,7 @@ public class PageChangeListener implements StateChangeListener { // the widget including its state (in event.item.state) boolean itemBelongsToWidget = widget.getItem() != null && widget.getItem().equals(item.getName()); final Item itemToBeSent = itemBelongsToWidget ? item : getItemForWidget(widget); + State stateToBeSent = null; if (itemToBeSent != null) { String widgetTypeName = widget.eClass().getInstanceTypeName() .substring(widget.eClass().getInstanceTypeName().lastIndexOf(".") + 1); @@ -257,13 +255,16 @@ public class PageChangeListener implements StateChangeListener { event.item = EnrichedItemDTOMapper.map(itemToBeSent, drillDown, itemFilter, null, null); // event.state is an adjustment of the item state to the widget type. - final State stateToBeSent = itemBelongsToWidget ? state : itemToBeSent.getState(); + stateToBeSent = itemBelongsToWidget ? state : itemToBeSent.getState(); event.state = itemUIRegistry.convertState(widget, itemToBeSent, stateToBeSent).toFullString(); // In case this state is identical to the item state, its value is set to null. if (event.state != null && event.state.equals(event.item.state)) { event.state = null; } } + event.labelcolor = SitemapResource.convertItemValueColor(itemUIRegistry.getLabelColor(widget), stateToBeSent); + event.valuecolor = SitemapResource.convertItemValueColor(itemUIRegistry.getValueColor(widget), stateToBeSent); + event.iconcolor = SitemapResource.convertItemValueColor(itemUIRegistry.getIconColor(widget), stateToBeSent); return event; } 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 ec4890563..a0597d54f 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 @@ -69,6 +69,7 @@ import org.openhab.core.items.Item; import org.openhab.core.items.ItemNotFoundException; import org.openhab.core.items.StateChangeListener; import org.openhab.core.library.CoreItemFactory; +import org.openhab.core.library.types.HSBType; import org.openhab.core.model.sitemap.SitemapProvider; import org.openhab.core.model.sitemap.sitemap.Chart; import org.openhab.core.model.sitemap.sitemap.ColorArray; @@ -492,9 +493,11 @@ public class SitemapResource } WidgetDTO bean = new WidgetDTO(); + State itemState = null; if (widget.getItem() != null) { try { Item item = itemUIRegistry.getItem(widget.getItem()); + itemState = item.getState(); String widgetTypeName = widget.eClass().getInstanceTypeName() .substring(widget.eClass().getInstanceTypeName().lastIndexOf(".") + 1); boolean isMapview = "mapview".equalsIgnoreCase(widgetTypeName); @@ -512,9 +515,9 @@ public class SitemapResource } bean.widgetId = widgetId; bean.icon = itemUIRegistry.getCategory(widget); - bean.labelcolor = itemUIRegistry.getLabelColor(widget); - bean.valuecolor = itemUIRegistry.getValueColor(widget); - bean.iconcolor = itemUIRegistry.getIconColor(widget); + bean.labelcolor = convertItemValueColor(itemUIRegistry.getLabelColor(widget), itemState); + bean.valuecolor = convertItemValueColor(itemUIRegistry.getValueColor(widget), itemState); + bean.iconcolor = convertItemValueColor(itemUIRegistry.getIconColor(widget), itemState); bean.label = itemUIRegistry.getLabel(widget); bean.type = widget.eClass().getName(); bean.visibility = itemUIRegistry.getVisiblity(widget); @@ -610,6 +613,16 @@ public class SitemapResource return bean; } + public static @Nullable String convertItemValueColor(@Nullable String color, @Nullable State itemState) { + if ("itemValue".equals(color)) { + if (itemState instanceof HSBType hsbState) { + return "#" + Integer.toHexString(hsbState.getRGB()).substring(2); + } + return null; + } + return color; + } + private String buildProxyUrl(String sitemapName, Widget widget, URI uri) { String wId = itemUIRegistry.getWidgetId(widget); StringBuilder sb = new StringBuilder();