Skip to content

Commit

Permalink
Sitemap (openhab#46)
Browse files Browse the repository at this point in the history
* updated default sitemap provider to new ESH concept

Signed-off-by: Kai Kreuzer <[email protected]>
  • Loading branch information
kaikreuzer committed May 21, 2016
1 parent c55513a commit 25cd02b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 58 deletions.
3 changes: 2 additions & 1 deletion bundles/org.openhab.core/OSGI-INF/defaultsitemapprovider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
<service>
<provide interface="org.eclipse.smarthome.model.sitemap.SitemapProvider"/>
</service>
<reference bind="setItemRegistry" cardinality="1..1" interface="org.eclipse.smarthome.core.items.ItemRegistry" name="ItemRegistry" policy="static" unbind="unsetItemRegistry"/>
<reference bind="setThingRegistry" cardinality="1..1" interface="org.eclipse.smarthome.core.thing.ThingRegistry" name="ThingRegistry" policy="static" unbind="unsetThingRegistry"/>
<reference bind="setItemChannelLinkRegistry" cardinality="1..1" interface="org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry" name="ItemChannelLinkRegistry" policy="static" unbind="unsetItemChannelLinkRegistry"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,91 @@
import java.util.Collections;
import java.util.Set;

import org.eclipse.smarthome.core.items.GroupItem;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.thing.setup.ThingSetupManager;
import org.eclipse.smarthome.core.thing.Channel;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingRegistry;
import org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry;
import org.eclipse.smarthome.model.sitemap.Sitemap;
import org.eclipse.smarthome.model.sitemap.SitemapFactory;
import org.eclipse.smarthome.model.sitemap.SitemapProvider;
import org.eclipse.smarthome.model.sitemap.impl.DefaultImpl;
import org.eclipse.smarthome.model.sitemap.impl.FrameImpl;
import org.eclipse.smarthome.model.sitemap.impl.GroupImpl;
import org.eclipse.smarthome.model.sitemap.impl.SitemapImpl;
import org.eclipse.smarthome.model.sitemap.impl.TextImpl;

/**
* This class dynamically provides a default sitemap which comprises
* all group items that do not have any parent group.
*
*
* @author Kai Kreuzer
*
*/
public class DefaultSitemapProvider implements SitemapProvider {

private static final String SITEMAP_NAME = "_default";

private ItemRegistry itemRegistry;

protected void setItemRegistry(ItemRegistry itemRegistry) {
this.itemRegistry = itemRegistry;
}

protected void unsetItemRegistry(ItemRegistry itemRegistry) {
this.itemRegistry = null;
}

@Override
public Sitemap getSitemap(String sitemapName) {
if(sitemapName.equals(SITEMAP_NAME)) {
SitemapImpl sitemap = (SitemapImpl) SitemapFactory.eINSTANCE.createSitemap();
FrameImpl mainFrame = (FrameImpl) SitemapFactory.eINSTANCE.createFrame();

FrameImpl thingFrame = (FrameImpl) SitemapFactory.eINSTANCE.createFrame();
thingFrame.setLabel("Things");

sitemap.setLabel("Home");
sitemap.setName(SITEMAP_NAME);

for(Item item : itemRegistry.getAll()) {
if(item instanceof GroupItem && (item.getTags().contains(ThingSetupManager.TAG_HOME_GROUP) || item.getTags().contains(ThingSetupManager.TAG_THING))) {
GroupImpl group = (GroupImpl) SitemapFactory.eINSTANCE.createGroup();
group.setItem(item.getName());
group.setLabel(item.getLabel());
String category = item.getCategory();
if(category != null) {
group.setIcon(item.getCategory());
}
if(item.getTags().contains(ThingSetupManager.TAG_HOME_GROUP)) {
mainFrame.getChildren().add(group);
} else {
thingFrame.getChildren().add(group);
}
}
}

if(!mainFrame.getChildren().isEmpty()) {
sitemap.getChildren().add(mainFrame);
}
if(!thingFrame.getChildren().isEmpty()) {
sitemap.getChildren().add(thingFrame);
private static final String SITEMAP_NAME = "_default";

private ThingRegistry thingRegistry;
private ItemChannelLinkRegistry linkRegistry;

protected void setThingRegistry(ThingRegistry thingRegistry) {
this.thingRegistry = thingRegistry;
}

protected void unsetThingRegistry(ThingRegistry thingRegistry) {
this.thingRegistry = null;
}

protected void setItemChannelLinkRegistry(ItemChannelLinkRegistry linkRegistry) {
this.linkRegistry = linkRegistry;
}

protected void unsetItemChannelLinkRegistry(ItemChannelLinkRegistry linkRegistry) {
this.linkRegistry = null;
}

@Override
public Sitemap getSitemap(String sitemapName) {
if (sitemapName.equals(SITEMAP_NAME)) {
SitemapImpl sitemap = (SitemapImpl) SitemapFactory.eINSTANCE.createSitemap();
FrameImpl mainFrame = (FrameImpl) SitemapFactory.eINSTANCE.createFrame();

FrameImpl thingFrame = (FrameImpl) SitemapFactory.eINSTANCE.createFrame();
thingFrame.setLabel("Things");

sitemap.setLabel("Home");
sitemap.setName(SITEMAP_NAME);

for (Thing thing : thingRegistry.getAll()) {
TextImpl thingWidget = (TextImpl) SitemapFactory.eINSTANCE.createText();
thingWidget.setLabel(thing.getLabel());
thingWidget.setIcon("player");

for (Channel channel : thing.getChannels()) {
Set<String> items = linkRegistry.getLinkedItems(channel.getUID());
if (!items.isEmpty()) {
DefaultImpl widget = (DefaultImpl) SitemapFactory.eINSTANCE.createDefault();
widget.setItem(items.iterator().next());
thingWidget.getChildren().add(widget);
}
}
if (!thingWidget.getChildren().isEmpty()) {
thingFrame.getChildren().add(thingWidget);
}
}

if (!mainFrame.getChildren().isEmpty()) {
sitemap.getChildren().add(mainFrame);
}

return sitemap;
}
return null;
}
if (!thingFrame.getChildren().isEmpty()) {
sitemap.getChildren().add(thingFrame);
}

return sitemap;

}
return null;

}

@Override
public Set<String> getSitemapNames() {
Expand Down

0 comments on commit 25cd02b

Please sign in to comment.