Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapted AutoUpdateDelegate to the new ESH auto update infrastructure #390

Merged
merged 3 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bundles/org.openhab.core.compat1x/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ Import-Package:
org.eclipse.jdt.annotation;resolution:=optional,
org.eclipse.smarthome.config.core,
org.eclipse.smarthome.core.audio,
org.eclipse.smarthome.core.autoupdate,
org.eclipse.smarthome.core.common.registry,
org.eclipse.smarthome.core.events,
org.eclipse.smarthome.core.items,
Expand Down
1 change: 1 addition & 0 deletions bundles/org.openhab.core.compat1x/OSGI-INF/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*.xml

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

21 changes: 0 additions & 21 deletions bundles/org.openhab.core.compat1x/OSGI-INF/eventbridge.xml

This file was deleted.

This file was deleted.

19 changes: 0 additions & 19 deletions bundles/org.openhab.core.compat1x/OSGI-INF/itemuiregistry.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,28 @@
*/
package org.openhab.core.autoupdate.internal;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import org.eclipse.smarthome.core.autoupdate.AutoUpdateBindingConfigProvider;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.common.registry.ProviderChangeListener;
import org.eclipse.smarthome.core.common.registry.RegistryChangeListener;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.items.ItemRegistry;
import org.eclipse.smarthome.core.items.Metadata;
import org.eclipse.smarthome.core.items.MetadataKey;
import org.eclipse.smarthome.core.items.MetadataProvider;
import org.openhab.core.autoupdate.AutoUpdateBindingProvider;
import org.openhab.core.binding.BindingChangeListener;
import org.openhab.core.binding.BindingProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;

/**
* This class serves as a mapping from the "old" org.openhab namespace to the new org.eclipse.smarthome
Expand All @@ -20,26 +38,185 @@
*
* @author Kai Kreuzer - Initial contribution and API
*/
public class AutoUpdateProviderDelegate implements AutoUpdateBindingConfigProvider {
@NonNullByDefault
@Component(service = MetadataProvider.class)
public class AutoUpdateProviderDelegate
implements MetadataProvider, RegistryChangeListener<Item>, BindingChangeListener {

private Set<org.openhab.core.autoupdate.AutoUpdateBindingProvider> providers = new CopyOnWriteArraySet<>();
private static final String AUTOUPDATE_KEY = "autoupdate";

public void addAutoUpdateBindingProvider(org.openhab.core.autoupdate.AutoUpdateBindingProvider provider) {
private Set<AutoUpdateBindingProvider> providers = new CopyOnWriteArraySet<>();
private Set<ProviderChangeListener<Metadata>> listeners = new CopyOnWriteArraySet<>();
private Set<String> itemUpdateVetos = new HashSet<>();
private boolean started = false;

private @NonNullByDefault({}) ItemRegistry itemRegistry;

@Activate
protected void activate() {
refreshItemUpdateVetos();
started = true;
itemRegistry.addRegistryChangeListener(this);
for (AutoUpdateBindingProvider provider : providers) {
provider.addBindingChangeListener(this);
}
}

@Deactivate
protected void deactivate() {
for (AutoUpdateBindingProvider provider : providers) {
provider.removeBindingChangeListener(this);
}
itemRegistry.removeRegistryChangeListener(this);
started = false;
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE)
public void addAutoUpdateBindingProvider(AutoUpdateBindingProvider provider) {
providers.add(provider);
if (started) {
refreshItemUpdateVetos();
provider.addBindingChangeListener(this);
}
}

public void removeAutoUpdateBindingProvider(org.openhab.core.autoupdate.AutoUpdateBindingProvider provider) {
public void removeAutoUpdateBindingProvider(AutoUpdateBindingProvider provider) {
providers.remove(provider);
if (started) {
refreshItemUpdateVetos();
provider.removeBindingChangeListener(this);
}
}

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

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

@Override
public void addProviderChangeListener(ProviderChangeListener<Metadata> listener) {
this.listeners.add(listener);
}

@Override
public Collection<Metadata> getAll() {
Set<Metadata> metadataSet = new HashSet<>();
for (Item item : itemRegistry.getAll()) {
synchronized (itemUpdateVetos) {
if (itemUpdateVetos.contains(item.getName())) {
Metadata metadata = getMetadata(item.getName());
metadataSet.add(metadata);
}
}
}
return metadataSet;
}

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

private void refreshItemUpdateVetos() {
Set<String> newVetos = new HashSet<>();
synchronized (itemUpdateVetos) {
itemUpdateVetos.clear();
for (Item item : itemRegistry.getAll()) {
for (AutoUpdateBindingProvider provider : providers) {
Boolean autoUpdate = provider.autoUpdate(item.getName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it really return a Boolean with potential null's?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. A provider returns null if it does not want to do any statement.

if (Boolean.FALSE.equals(autoUpdate)) {
newVetos.add(item.getName());
}
}
}

// find the removed ones
Set<String> removedVetos = new HashSet<>(itemUpdateVetos);
removedVetos.removeAll(newVetos);
for (String itemName : removedVetos) {
if (itemUpdateVetos.contains(itemName)) {
Metadata md = getMetadata(itemName);
for (ProviderChangeListener<Metadata> listener : listeners) {
listener.removed(this, md);
}
}
}

// find the added ones
Set<String> addedVetos = new HashSet<>(newVetos);
addedVetos.removeAll(itemUpdateVetos);
for (String itemName : addedVetos) {
notifyAboutAddedMetadata(itemName);
}
itemUpdateVetos = newVetos;
}
}

private void notifyAboutAddedMetadata(String itemName) {
if (itemUpdateVetos.contains(itemName)) {
Metadata md = getMetadata(itemName);
for (ProviderChangeListener<Metadata> listener : listeners) {
listener.added(this, md);
}
}
}

private void notifyAboutRemovedMetadata(String itemName) {
for (ProviderChangeListener<Metadata> listener : listeners) {
listener.removed(this, getMetadata(itemName));
}
}

private Metadata getMetadata(String itemName) {
return new Metadata(new MetadataKey(AUTOUPDATE_KEY, itemName), "false", null);
}

@Override
public Boolean autoUpdate(String itemName) {
for (org.openhab.core.autoupdate.AutoUpdateBindingProvider provider : providers) {
Boolean autoUpdate = provider.autoUpdate(itemName);
if (autoUpdate != null) {
return autoUpdate;
public void added(Item element) {
String itemName = element.getName();
refreshVetoForItem(itemName);
}

private void refreshVetoForItem(String itemName) {
synchronized (itemUpdateVetos) {
boolean removed = itemUpdateVetos.remove(itemName);
for (AutoUpdateBindingProvider provider : providers) {
Boolean autoUpdate = provider.autoUpdate(itemName);
if (Boolean.FALSE.equals(autoUpdate)) {
itemUpdateVetos.add(itemName);
notifyAboutAddedMetadata(itemName);
return;
}
}
if (removed) {
notifyAboutRemovedMetadata(itemName);
}
}
}

@Override
public void removed(Item element) {
itemUpdateVetos.remove(element.getName());
}

@Override
public void updated(Item oldElement, Item element) {
refreshVetoForItem(element.getName());
}

@Override
public void bindingChanged(@Nullable BindingProvider provider, @Nullable String itemName) {
if (itemName != null) {
refreshVetoForItem(itemName);
}
return null;
}

@Override
public void allBindingsChanged(@Nullable BindingProvider provider) {
refreshItemUpdateVetos();
}
}
Loading