Skip to content

Commit

Permalink
extend to all installed AddonServices
Browse files Browse the repository at this point in the history
Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
  • Loading branch information
holgerfriedrich committed Jan 4, 2024
1 parent f7ac05d commit 80646fa
Showing 1 changed file with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand All @@ -53,10 +54,11 @@
import org.openhab.core.config.discovery.addon.AddonFinder;
import org.openhab.core.config.discovery.addon.BaseAddonFinder;
import org.openhab.core.net.NetUtil;
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;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -152,14 +154,12 @@ public class IpAddonFinder extends BaseAddonFinder {
private final Logger logger = LoggerFactory.getLogger(IpAddonFinder.class);
private final ScheduledExecutorService scheduler = ThreadPoolManager
.getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);
private final AddonService addonService;
private final Set<AddonService> addonServices = new CopyOnWriteArraySet<>();
private @Nullable Future<?> scanJob = null;
Set<AddonInfo> suggestions = new HashSet<>();

@Activate
public IpAddonFinder(@Reference AddonService as) {
public IpAddonFinder() {
logger.trace("IpAddonFinder::IpAddonFinder");
this.addonService = as;
// start of scan will be triggered by setAddonCandidates to ensure addonCandidates are available
}

Expand All @@ -176,6 +176,15 @@ public void setAddonCandidates(List<AddonInfo> candidates) {
startScan();
}

@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void addAddonService(AddonService featureService) {
this.addonServices.add(featureService);
}

protected void removeAddonService(AddonService featureService) {
this.addonServices.remove(featureService);
}

private void startScan() {
// The setAddonCandidates() method is called for each info provider.
// In order to do the scan only once, but on the full set of candidates, we have to delay the execution.
Expand Down Expand Up @@ -205,8 +214,7 @@ private void scan() {
logger.trace("Checking candidate: {}", candidate.getUID());

// skip scanning if already installed
Addon addon = addonService.getAddon(candidate.getUID(), null);
if (addon != null && addon.isInstalled()) {
if (isAddonInstalled(candidate.getUID())) {
logger.trace("Skipping {}, already installed", candidate.getUID());
continue;
}
Expand Down Expand Up @@ -356,4 +364,14 @@ public Set<AddonInfo> getSuggestedAddons() {
public String getServiceName() {
return SERVICE_NAME;
}

private boolean isAddonInstalled(String addonId) {
for (AddonService addonService : addonServices) {
Addon addon = addonService.getAddon(addonId, null);
if (addon != null && addon.isInstalled()) {
return true;
}
}
return false;
}
}

0 comments on commit 80646fa

Please sign in to comment.