Skip to content

Commit

Permalink
Enhanced sitemap SSE event with a new boolean field informing if the
Browse files Browse the repository at this point in the history
item state or command description has been updated

Listen to the new ChannelDescriptionChangedEvent

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Jun 24, 2021
1 parent 57c716b commit 230b4ae
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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;
Expand Down Expand Up @@ -343,7 +344,7 @@ public void checkAliveClients() {

@Override
public Set<String> getSubscribedEventTypes() {
return Set.of(ItemStatePredictedEvent.TYPE);
return Set.of(ItemStatePredictedEvent.TYPE, ChannelDescriptionChangedEvent.TYPE);
}

@Override
Expand All @@ -368,6 +369,13 @@ public void receive(Event event) {
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);
}
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,39 +222,45 @@ private Set<SitemapEvent> constructSitemapEvents(Item item, State state, List<Wi
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) {
Expand Down Expand Up @@ -305,4 +311,37 @@ public void sendAliveEvent() {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class SitemapWidgetEvent extends SitemapEvent {
public boolean visibility;
public String state;
public EnrichedItemDTO item;
public boolean descriptionChanged;

public SitemapWidgetEvent() {
}
Expand Down

0 comments on commit 230b4ae

Please sign in to comment.