Skip to content

Commit

Permalink
[sitemap] Add new color keyword "itemValue" (openhab#3453)
Browse files Browse the repository at this point in the history
Closes openhab#3429

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored Mar 16, 2023
1 parent 11e29c4 commit 52e36a0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,14 @@ private SitemapWidgetEvent constructSitemapEventForWidget(Item item, State state
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;
// event.item contains the (potentially changed) data of the item belonging to
// 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);
Expand All @@ -257,13 +255,16 @@ private SitemapWidgetEvent constructSitemapEventForWidget(Item item, State state
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
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;
Expand Down Expand Up @@ -492,9 +493,11 @@ private PageDTO createPageBean(String sitemapName, @Nullable String title, @Null
}

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);
Expand All @@ -512,9 +515,9 @@ private PageDTO createPageBean(String sitemapName, @Nullable String title, @Null
}
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);
Expand Down Expand Up @@ -610,6 +613,16 @@ private PageDTO createPageBean(String sitemapName, @Nullable String title, @Null
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();
Expand Down

0 comments on commit 52e36a0

Please sign in to comment.