mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-10 13:21:53 +01:00
Improve marketplace remote handling
Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
parent
af35487155
commit
6d0a3b330c
@ -15,7 +15,6 @@ package org.openhab.core.addon.marketplace;
|
|||||||
import static org.openhab.core.common.ThreadPoolManager.THREAD_POOL_NAME_COMMON;
|
import static org.openhab.core.common.ThreadPoolManager.THREAD_POOL_NAME_COMMON;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
@ -95,7 +94,7 @@ public abstract class AbstractRemoteAddonService implements AddonService {
|
|||||||
this::getRemoteAddons);
|
this::getRemoteAddons);
|
||||||
protected final AddonInfoRegistry addonInfoRegistry;
|
protected final AddonInfoRegistry addonInfoRegistry;
|
||||||
protected List<Addon> cachedAddons = List.of();
|
protected List<Addon> cachedAddons = List.of();
|
||||||
protected List<String> installedAddons = List.of();
|
protected List<String> installedAddonIds = List.of();
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(AbstractRemoteAddonService.class);
|
private final Logger logger = LoggerFactory.getLogger(AbstractRemoteAddonService.class);
|
||||||
private final ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool(THREAD_POOL_NAME_COMMON);
|
private final ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool(THREAD_POOL_NAME_COMMON);
|
||||||
@ -137,7 +136,10 @@ public abstract class AbstractRemoteAddonService implements AddonService {
|
|||||||
// this is safe, because the {@link AddonHandler}s only report ready when they installed everything from the
|
// this is safe, because the {@link AddonHandler}s only report ready when they installed everything from the
|
||||||
// cache
|
// cache
|
||||||
try {
|
try {
|
||||||
installedAddonStorage.stream().map(this::convertFromStorage).peek(this::setInstalled).forEach(addons::add);
|
installedAddonStorage.stream().map(this::convertFromStorage).forEach(addon -> {
|
||||||
|
setInstalled(addon);
|
||||||
|
addons.add(addon);
|
||||||
|
});
|
||||||
} catch (JsonSyntaxException e) {
|
} catch (JsonSyntaxException e) {
|
||||||
List.copyOf(installedAddonStorage.getKeys()).forEach(installedAddonStorage::remove);
|
List.copyOf(installedAddonStorage.getKeys()).forEach(installedAddonStorage::remove);
|
||||||
logger.error(
|
logger.error(
|
||||||
@ -152,13 +154,15 @@ public abstract class AbstractRemoteAddonService implements AddonService {
|
|||||||
addons.removeIf(addon -> missingAddons.contains(addon.getUid()));
|
addons.removeIf(addon -> missingAddons.contains(addon.getUid()));
|
||||||
|
|
||||||
// create lookup list to make sure installed addons take precedence
|
// create lookup list to make sure installed addons take precedence
|
||||||
List<String> installedAddons = addons.stream().map(Addon::getUid).toList();
|
List<String> currentAddonIds = addons.stream().map(Addon::getUid).toList();
|
||||||
|
|
||||||
// get the remote addons
|
// get the remote addons
|
||||||
if (remoteEnabled()) {
|
if (remoteEnabled()) {
|
||||||
List<Addon> remoteAddons = Objects.requireNonNullElse(cachedRemoteAddons.getValue(), List.of());
|
List<Addon> remoteAddons = Objects.requireNonNullElse(cachedRemoteAddons.getValue(), List.of());
|
||||||
remoteAddons.stream().filter(a -> !installedAddons.contains(a.getUid())).peek(this::setInstalled)
|
remoteAddons.stream().filter(a -> !currentAddonIds.contains(a.getUid())).forEach(addon -> {
|
||||||
.forEach(addons::add);
|
setInstalled(addon);
|
||||||
|
addons.add(addon);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove incompatible add-ons if not enabled
|
// remove incompatible add-ons if not enabled
|
||||||
@ -175,7 +179,7 @@ public abstract class AbstractRemoteAddonService implements AddonService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cachedAddons = addons;
|
cachedAddons = addons;
|
||||||
this.installedAddons = installedAddons;
|
this.installedAddonIds = currentAddonIds;
|
||||||
|
|
||||||
if (!missingAddons.isEmpty()) {
|
if (!missingAddons.isEmpty()) {
|
||||||
logger.info("Re-installing missing add-ons from remote repository: {}", missingAddons);
|
logger.info("Re-installing missing add-ons from remote repository: {}", missingAddons);
|
||||||
@ -222,9 +226,6 @@ public abstract class AbstractRemoteAddonService implements AddonService {
|
|||||||
return cachedAddons;
|
return cachedAddons;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public abstract @Nullable Addon getAddon(String id, @Nullable Locale locale);
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<AddonType> getTypes(@Nullable Locale locale) {
|
public List<AddonType> getTypes(@Nullable Locale locale) {
|
||||||
return AddonType.DEFAULT_TYPES;
|
return AddonType.DEFAULT_TYPES;
|
||||||
@ -244,6 +245,7 @@ public abstract class AbstractRemoteAddonService implements AddonService {
|
|||||||
handler.install(addon);
|
handler.install(addon);
|
||||||
addon.setInstalled(true);
|
addon.setInstalled(true);
|
||||||
installedAddonStorage.put(id, gson.toJson(addon));
|
installedAddonStorage.put(id, gson.toJson(addon));
|
||||||
|
cachedRemoteAddons.invalidateValue();
|
||||||
refreshSource();
|
refreshSource();
|
||||||
postInstalledEvent(addon.getUid());
|
postInstalledEvent(addon.getUid());
|
||||||
} catch (MarketplaceHandlerException e) {
|
} catch (MarketplaceHandlerException e) {
|
||||||
@ -271,6 +273,7 @@ public abstract class AbstractRemoteAddonService implements AddonService {
|
|||||||
try {
|
try {
|
||||||
handler.uninstall(addon);
|
handler.uninstall(addon);
|
||||||
installedAddonStorage.remove(id);
|
installedAddonStorage.remove(id);
|
||||||
|
cachedRemoteAddons.invalidateValue();
|
||||||
refreshSource();
|
refreshSource();
|
||||||
postUninstalledEvent(addon.getUid());
|
postUninstalledEvent(addon.getUid());
|
||||||
} catch (MarketplaceHandlerException e) {
|
} catch (MarketplaceHandlerException e) {
|
||||||
@ -286,9 +289,6 @@ public abstract class AbstractRemoteAddonService implements AddonService {
|
|||||||
postFailureEvent(id, "Add-on can't be uninstalled because there is no handler for it.");
|
postFailureEvent(id, "Add-on can't be uninstalled because there is no handler for it.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public abstract @Nullable String getAddonId(URI addonURI);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* check if remote services are enabled
|
* check if remote services are enabled
|
||||||
*
|
*
|
||||||
|
@ -205,7 +205,7 @@ public class CommunityMarketplaceAddonService extends AbstractRemoteAddonService
|
|||||||
// check if it is an installed add-on (cachedAddons also contains possibly incomplete results from the remote
|
// check if it is an installed add-on (cachedAddons also contains possibly incomplete results from the remote
|
||||||
// side, we need to retrieve them from Discourse)
|
// side, we need to retrieve them from Discourse)
|
||||||
|
|
||||||
if (installedAddons.contains(queryId)) {
|
if (installedAddonIds.contains(queryId)) {
|
||||||
return cachedAddons.stream().filter(e -> queryId.equals(e.getUid())).findAny().orElse(null);
|
return cachedAddons.stream().filter(e -> queryId.equals(e.getUid())).findAny().orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user