[miio] fix bundle restart error (#10190)

Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
This commit is contained in:
Marcel 2021-02-26 00:53:19 +01:00 committed by GitHub
parent 23ff9a0221
commit 8fde1c3988
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,6 +15,8 @@ package org.openhab.binding.miio.internal;
import static org.openhab.binding.miio.internal.MiIoBindingConstants.*; import static org.openhab.binding.miio.internal.MiIoBindingConstants.*;
import java.util.Map; import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
@ -35,7 +37,10 @@ import org.openhab.core.thing.binding.ThingHandlerFactory;
import org.openhab.core.thing.type.ChannelTypeRegistry; import org.openhab.core.thing.type.ChannelTypeRegistry;
import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component; 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.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* The {@link MiIoHandlerFactory} is responsible for creating things and thing * The {@link MiIoHandlerFactory} is responsible for creating things and thing
@ -49,17 +54,20 @@ public class MiIoHandlerFactory extends BaseThingHandlerFactory {
private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler"; private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler";
protected final ScheduledExecutorService scheduler = ThreadPoolManager protected final ScheduledExecutorService scheduler = ThreadPoolManager
.getScheduledPool(THING_HANDLER_THREADPOOL_NAME); .getScheduledPool(THING_HANDLER_THREADPOOL_NAME);
private MiIoDatabaseWatchService miIoDatabaseWatchService; private MiIoDatabaseWatchService miIoDatabaseWatchService;
private CloudConnector cloudConnector; private CloudConnector cloudConnector;
private ChannelTypeRegistry channelTypeRegistry; private ChannelTypeRegistry channelTypeRegistry;
private BasicChannelTypeProvider basicChannelTypeProvider; private BasicChannelTypeProvider basicChannelTypeProvider;
private @Nullable Future<Boolean> scheduledTask;
private final Logger logger = LoggerFactory.getLogger(MiIoHandlerFactory.class);
@Activate @Activate
public MiIoHandlerFactory(@Reference ChannelTypeRegistry channelTypeRegistry, public MiIoHandlerFactory(@Reference ChannelTypeRegistry channelTypeRegistry,
@Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector, @Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector,
@Reference BasicChannelTypeProvider basicChannelTypeProvider, Map<String, Object> properties) { @Reference BasicChannelTypeProvider basicChannelTypeProvider, Map<String, Object> properties) {
this.miIoDatabaseWatchService = miIoDatabaseWatchService; this.miIoDatabaseWatchService = miIoDatabaseWatchService;
this.channelTypeRegistry = channelTypeRegistry;
this.basicChannelTypeProvider = basicChannelTypeProvider;
this.cloudConnector = cloudConnector; this.cloudConnector = cloudConnector;
@Nullable @Nullable
String username = (String) properties.get("username"); String username = (String) properties.get("username");
@ -68,9 +76,23 @@ public class MiIoHandlerFactory extends BaseThingHandlerFactory {
@Nullable @Nullable
String country = (String) properties.get("country"); String country = (String) properties.get("country");
cloudConnector.setCredentials(username, password, country); cloudConnector.setCredentials(username, password, country);
scheduler.submit(() -> cloudConnector.isConnected()); try {
this.channelTypeRegistry = channelTypeRegistry; if (!scheduler.isShutdown()) {
this.basicChannelTypeProvider = basicChannelTypeProvider; scheduledTask = scheduler.submit(() -> cloudConnector.isConnected());
} else {
logger.debug("Unexpected: ScheduledExecutorService is shutdown.");
}
} catch (RejectedExecutionException e) {
logger.debug("Unexpected: ScheduledExecutorService task rejected.", e);
}
}
@Deactivate
private void dispose() {
final Future<Boolean> scheduledTask = this.scheduledTask;
if (scheduledTask != null && !scheduledTask.isDone()) {
scheduledTask.cancel(true);
}
} }
@Override @Override