2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.miio.internal;
15 import static org.openhab.binding.miio.internal.MiIoBindingConstants.*;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.RejectedExecutionException;
20 import java.util.concurrent.ScheduledExecutorService;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.miio.internal.basic.BasicChannelTypeProvider;
25 import org.openhab.binding.miio.internal.basic.MiIoDatabaseWatchService;
26 import org.openhab.binding.miio.internal.cloud.CloudConnector;
27 import org.openhab.binding.miio.internal.handler.MiIoBasicHandler;
28 import org.openhab.binding.miio.internal.handler.MiIoGenericHandler;
29 import org.openhab.binding.miio.internal.handler.MiIoUnsupportedHandler;
30 import org.openhab.binding.miio.internal.handler.MiIoVacuumHandler;
31 import org.openhab.core.common.ThreadPoolManager;
32 import org.openhab.core.io.net.http.HttpClientFactory;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.openhab.core.thing.type.ChannelTypeRegistry;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Deactivate;
42 import org.osgi.service.component.annotations.Reference;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * The {@link MiIoHandlerFactory} is responsible for creating things and thing
50 * @author Marcel Verpaalen - Initial contribution
52 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miio")
54 public class MiIoHandlerFactory extends BaseThingHandlerFactory {
55 private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler";
56 protected final ScheduledExecutorService scheduler = ThreadPoolManager
57 .getScheduledPool(THING_HANDLER_THREADPOOL_NAME);
58 private final HttpClientFactory httpClientFactory;
59 private MiIoDatabaseWatchService miIoDatabaseWatchService;
60 private CloudConnector cloudConnector;
61 private ChannelTypeRegistry channelTypeRegistry;
62 private BasicChannelTypeProvider basicChannelTypeProvider;
63 private @Nullable Future<Boolean> scheduledTask;
64 private final Logger logger = LoggerFactory.getLogger(MiIoHandlerFactory.class);
67 public MiIoHandlerFactory(@Reference HttpClientFactory httpClientFactory,
68 @Reference ChannelTypeRegistry channelTypeRegistry,
69 @Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector,
70 @Reference BasicChannelTypeProvider basicChannelTypeProvider, Map<String, Object> properties) {
71 this.httpClientFactory = httpClientFactory;
72 this.miIoDatabaseWatchService = miIoDatabaseWatchService;
73 this.channelTypeRegistry = channelTypeRegistry;
74 this.basicChannelTypeProvider = basicChannelTypeProvider;
75 this.cloudConnector = cloudConnector;
77 String username = (String) properties.get("username");
79 String password = (String) properties.get("password");
81 String country = (String) properties.get("country");
82 cloudConnector.setCredentials(username, password, country);
84 if (!scheduler.isShutdown()) {
85 scheduledTask = scheduler.submit(() -> cloudConnector.isConnected(true));
87 logger.debug("Unexpected: ScheduledExecutorService is shutdown.");
89 } catch (RejectedExecutionException e) {
90 logger.debug("Unexpected: ScheduledExecutorService task rejected.", e);
95 public void dispose() {
96 final Future<Boolean> scheduledTask = this.scheduledTask;
97 if (scheduledTask != null && !scheduledTask.isDone()) {
98 scheduledTask.cancel(true);
103 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
104 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
108 protected @Nullable ThingHandler createHandler(Thing thing) {
109 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
110 if (thingTypeUID.equals(THING_TYPE_MIIO)) {
111 return new MiIoGenericHandler(thing, miIoDatabaseWatchService, cloudConnector);
113 if (thingTypeUID.equals(THING_TYPE_BASIC)) {
114 return new MiIoBasicHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
115 basicChannelTypeProvider);
117 if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
118 return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry);
120 return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService, cloudConnector,
121 httpClientFactory.getCommonHttpClient());