[sitemap] Enhanced sitemap SSE event with a new boolean field informing if the (#2413)

item state or command description has been updated

Listen to the new ChannelDescriptionChangedEvent

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2021-06-25 07:17:54 +02:00 committed by GitHub
parent 57c716bf80
commit 2d71afe5bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 28 deletions

View File

@ -41,6 +41,7 @@ import org.openhab.core.model.sitemap.SitemapProvider;
import org.openhab.core.model.sitemap.sitemap.LinkableWidget;
import org.openhab.core.model.sitemap.sitemap.Sitemap;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.thing.events.ChannelDescriptionChangedEvent;
import org.openhab.core.ui.items.ItemUIRegistry;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
@ -343,7 +344,7 @@ public class SitemapSubscriptionService implements ModelRepositoryChangeListener
@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemStatePredictedEvent.TYPE);
return Set.of(ItemStatePredictedEvent.TYPE, ChannelDescriptionChangedEvent.TYPE);
}
@Override
@ -368,6 +369,13 @@ public class SitemapSubscriptionService implements ModelRepositoryChangeListener
pageChangeListener.changeStateTo(item, prediction.getPredictedState());
}
}
} else if (event instanceof ChannelDescriptionChangedEvent) {
ChannelDescriptionChangedEvent channelDescriptionChangedEvent = (ChannelDescriptionChangedEvent) event;
channelDescriptionChangedEvent.getLinkedItemNames().forEach(itemName -> {
for (PageChangeListener pageChangeListener : pageChangeListeners.values()) {
pageChangeListener.descriptionChanged(itemName);
}
});
}
}
}

View File

@ -222,39 +222,45 @@ public class PageChangeListener implements StateChangeListener {
skipWidget = chartWidget.getRefresh() > 0;
}
if (!skipWidget || definesVisibilityOrColor(w, item.getName())) {
SitemapWidgetEvent event = new SitemapWidgetEvent();
event.sitemapName = sitemapName;
event.pageId = pageId;
event.label = itemUIRegistry.getLabel(w);
event.labelcolor = itemUIRegistry.getLabelColor(w);
event.valuecolor = itemUIRegistry.getValueColor(w);
event.widgetId = itemUIRegistry.getWidgetId(w);
event.visibility = itemUIRegistry.getVisiblity(w);
// event.item contains the (potentially changed) data of the item belonging to
// the widget including its state (in event.item.state)
final Item itemToBeSent = itemBelongsToWidget ? item : getItemForWidget(w);
if (itemToBeSent != null) {
String widgetTypeName = w.eClass().getInstanceTypeName()
.substring(w.eClass().getInstanceTypeName().lastIndexOf(".") + 1);
boolean drillDown = "mapview".equalsIgnoreCase(widgetTypeName);
Predicate<Item> itemFilter = (i -> CoreItemFactory.LOCATION.equals(i.getType()));
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();
event.state = itemUIRegistry.convertState(w, 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;
}
}
SitemapWidgetEvent event = constructSitemapEventForWidget(item, state, w);
events.add(event);
}
}
return events;
}
private SitemapWidgetEvent constructSitemapEventForWidget(Item item, State state, Widget widget) {
SitemapWidgetEvent event = new SitemapWidgetEvent();
event.sitemapName = sitemapName;
event.pageId = pageId;
event.label = itemUIRegistry.getLabel(widget);
event.labelcolor = itemUIRegistry.getLabelColor(widget);
event.valuecolor = itemUIRegistry.getValueColor(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);
if (itemToBeSent != null) {
String widgetTypeName = widget.eClass().getInstanceTypeName()
.substring(widget.eClass().getInstanceTypeName().lastIndexOf(".") + 1);
boolean drillDown = "mapview".equalsIgnoreCase(widgetTypeName);
Predicate<Item> itemFilter = (i -> CoreItemFactory.LOCATION.equals(i.getType()));
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();
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;
}
}
return event;
}
private Item getItemForWidget(Widget w) {
final String itemName = w.getItem();
if (itemName != null) {
@ -305,4 +311,37 @@ public class PageChangeListener implements StateChangeListener {
callback.onEvent(aliveEvent);
}
}
public void descriptionChanged(String itemName) {
try {
Item item = itemUIRegistry.getItem(itemName);
Set<SitemapEvent> events = constructSitemapEventsForUpdatedDescr(item, widgets);
for (SitemapEvent event : events) {
for (SitemapSubscriptionCallback callback : distinctCallbacks) {
callback.onEvent(event);
}
}
} catch (ItemNotFoundException e) {
// ignore
}
}
private Set<SitemapEvent> constructSitemapEventsForUpdatedDescr(Item item, List<Widget> widgets) {
Set<SitemapEvent> events = new HashSet<>();
for (Widget w : widgets) {
if (w instanceof Frame) {
events.addAll(constructSitemapEventsForUpdatedDescr(item, itemUIRegistry.getChildren((Frame) w)));
}
boolean itemBelongsToWidget = w.getItem() != null && w.getItem().equals(item.getName());
if (itemBelongsToWidget) {
SitemapWidgetEvent event = constructSitemapEventForWidget(item, item.getState(), w);
event.descriptionChanged = true;
events.add(event);
}
}
return events;
}
}

View File

@ -30,6 +30,7 @@ public class SitemapWidgetEvent extends SitemapEvent {
public boolean visibility;
public String state;
public EnrichedItemDTO item;
public boolean descriptionChanged;
public SitemapWidgetEvent() {
}