Skip to content

Commit

Permalink
updated thing item provider and default sitemap provider to work with…
Browse files Browse the repository at this point in the history
… home-groups

Signed-off-by: Kai Kreuzer <[email protected]> (github: @kaikreuzer)
  • Loading branch information
kaikreuzer committed Jan 19, 2015
1 parent f1345f0 commit 5173ecd
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 478 deletions.
3 changes: 1 addition & 2 deletions bundles/core/org.openhab.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Manifest-Version: 1.0
Service-Component: OSGI-INF/autoapprove.xml,OSGI-INF/thingitemprovider.xml,
OSGI-INF/thinglinkprovider.xml,OSGI-INF/defaultsitemapprovider.xml
Service-Component: OSGI-INF/*.xml
Private-Package: org.openhab.core.internal,org.openhab.core.internal.e
vents,org.openhab.core.internal.items
Ignore-Package: org.openhab.core.internal.items,org.openhab.core.inter
Expand Down
8 changes: 2 additions & 6 deletions bundles/core/org.openhab.core/OSGI-INF/thingitemprovider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" configuration-policy="optional" deactivate="deactivate" immediate="false" name="org.openhab.thingitemprovider">
<implementation class="org.openhab.core.internal.item.ThingItemUIProvider"/>
<reference bind="setThingRegistry" cardinality="1..1" interface="org.eclipse.smarthome.core.thing.ThingRegistry" name="ThingRegistry" policy="static" unbind="unsetThingRegistry"/>
<reference bind="setItemFactory" cardinality="1..1" interface="org.eclipse.smarthome.core.items.ItemFactory" name="ItemFactory" policy="static" unbind="unsetItemFactory"/>
<implementation class="org.openhab.core.internal.item.ThingItemProvider"/>
<service>
<provide interface="org.eclipse.smarthome.core.items.ItemProvider"/>
<provide interface="org.eclipse.smarthome.ui.items.ItemUIProvider"/>
<provide interface="org.openhab.core.internal.item.ThingItemUIProvider"/>
</service>
<reference bind="setThingTypeRegistry" cardinality="1..1" interface="org.eclipse.smarthome.core.thing.type.ThingTypeRegistry" name="ThingTypeRegistry" policy="static" unbind="unsetThingTypeRegistry"/>
<reference bind="setItemRegistry" cardinality="1..1" interface="org.eclipse.smarthome.core.items.ItemRegistry" name="ItemRegistry" policy="static" unbind="unsetItemRegistry"/>
<reference bind="setEventPublisher" cardinality="1..1" interface="org.eclipse.smarthome.core.events.EventPublisher" name="EventPublisher" policy="dynamic" unbind="unsetEventPublisher"/>
</scr:component>
19 changes: 0 additions & 19 deletions bundles/core/org.openhab.core/OSGI-INF/thinglinkprovider.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ public Sitemap getSitemap(String sitemapName) {
sitemap.getChildren().add(frame);

for(Item item : itemRegistry.getAll()) {
if(item instanceof GroupItem && !item.getName().equals("Things")
&& (item.getGroupNames().isEmpty() || item.getGroupNames().contains("Things"))) {
if(item instanceof GroupItem && !item.getTags().contains("home-group")) {
GroupImpl group = (GroupImpl) SitemapFactory.eINSTANCE.createGroup();
group.setItem(item.getName());
group.setLabel(item.getLabel());
frame.getChildren().add(group);
}
}

return sitemap;
}
return null;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/**
* Copyright (c) 2014-2015 openHAB UG (haftungsbeschraenkt) and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.core.internal.item;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.smarthome.core.common.registry.ProviderChangeListener;
import org.eclipse.smarthome.core.common.registry.RegistryChangeListener;
import org.eclipse.smarthome.core.events.EventPublisher;
import org.eclipse.smarthome.core.items.ActiveItem;
import org.eclipse.smarthome.core.items.GroupItem;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.items.ItemProvider;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.thing.ManagedThingProvider;
import org.eclipse.smarthome.core.types.UnDefType;
import org.osgi.service.cm.ConfigurationException;

/**
* This class dynamically provides items incl. labels from all things of the {@link ManagedThingProvider}.
* All items are hierarchically sorted with a root group item called "Things".
*
* @author Kai Kreuzer
*
*/
public class ThingItemProvider implements ItemProvider, RegistryChangeListener<Item> {

private Set<ProviderChangeListener<Item>> listeners = new HashSet<>();

private ItemRegistry itemRegistry;
private EventPublisher eventPublisher;
private GroupItem rootItem;

private boolean enabled = false;

@Override
public Collection<Item> getAll() {
if(!enabled) return Collections.emptySet();

Set<Item> items = new HashSet<>();
GroupItem all = getRootItem();
for(Item item : itemRegistry.getItemsByTag("thing")) {
ActiveItem aItem = (ActiveItem) item;
aItem.addGroupName(all.getName());
all.addMember(item);
}
items.add(all);
return items;
}

@Override
public void addProviderChangeListener(ProviderChangeListener<Item> listener) {
listeners.add(listener);
for(Item item : getAll()) {
listener.added(this, item);
}
}

@Override
public void removeProviderChangeListener(
ProviderChangeListener<Item> listener) {
listeners.remove(listener);

}

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

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

protected void setEventPublisher(EventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}

protected void unsetEventPublisher(EventPublisher eventPublisher) {
this.eventPublisher = null;
}

private synchronized GroupItem getRootItem() {
if(rootItem==null) {
rootItem = new GroupItem("All");
rootItem.addTag("home-group");
rootItem.setLabel("All");
getAll();
}
return rootItem;
}

protected void activate(Map<String, Object> properties) throws ConfigurationException {
if(properties!=null) {
String enabled = (String) properties.get("enabled");
if("true".equalsIgnoreCase(enabled)) {
this.enabled = true;
for(ProviderChangeListener<Item> listener : listeners) {
for(Item item : getAll()) {
listener.added(this, item);
}
}
} else {
this.enabled = false;
for(ProviderChangeListener<Item> listener : listeners) {
for(Item item : getAll()) {
listener.removed(this, item);
}
}
}
}
}

@Override
public void added(Item element) {
if(!enabled) return;
if(!element.getTags().contains("thing")) return;
rootItem = null;
rootItem = getRootItem();
for(ProviderChangeListener<Item> listener : listeners) {
listener.removed(this, getRootItem());
listener.added(this, getRootItem());
}
}

@Override
public void removed(Item element) {
if(!enabled) return;
if(!element.getTags().contains("thing")) return;
for(ProviderChangeListener<Item> listener : listeners) {
listener.removed(this, getRootItem());
rootItem = null;
listener.added(this, getRootItem());
}
}

@Override
public void updated(Item oldElement, Item element) {
if(!enabled) return;
removed(oldElement);
added(element);
}

public boolean isEnabled() {
return enabled;
}

}
Loading

0 comments on commit 5173ecd

Please sign in to comment.