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.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.openhab.core.thing.type.ChannelTypeRegistry;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Deactivate;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * The {@link MiIoHandlerFactory} is responsible for creating things and thing
49 * @author Marcel Verpaalen - Initial contribution
51 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miio")
53 public class MiIoHandlerFactory extends BaseThingHandlerFactory {
54 private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler";
55 protected final ScheduledExecutorService scheduler = ThreadPoolManager
56 .getScheduledPool(THING_HANDLER_THREADPOOL_NAME);
57 private MiIoDatabaseWatchService miIoDatabaseWatchService;
58 private CloudConnector cloudConnector;
59 private ChannelTypeRegistry channelTypeRegistry;
60 private BasicChannelTypeProvider basicChannelTypeProvider;
61 private @Nullable Future<Boolean> scheduledTask;
62 private final Logger logger = LoggerFactory.getLogger(MiIoHandlerFactory.class);
65 public MiIoHandlerFactory(@Reference ChannelTypeRegistry channelTypeRegistry,
66 @Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector,
67 @Reference BasicChannelTypeProvider basicChannelTypeProvider, Map<String, Object> properties) {
68 this.miIoDatabaseWatchService = miIoDatabaseWatchService;
69 this.channelTypeRegistry = channelTypeRegistry;
70 this.basicChannelTypeProvider = basicChannelTypeProvider;
71 this.cloudConnector = cloudConnector;
73 String username = (String) properties.get("username");
75 String password = (String) properties.get("password");
77 String country = (String) properties.get("country");
78 cloudConnector.setCredentials(username, password, country);
80 if (!scheduler.isShutdown()) {
81 scheduledTask = scheduler.submit(() -> cloudConnector.isConnected());
83 logger.debug("Unexpected: ScheduledExecutorService is shutdown.");
85 } catch (RejectedExecutionException e) {
86 logger.debug("Unexpected: ScheduledExecutorService task rejected.", e);
91 public void dispose() {
92 final Future<Boolean> scheduledTask = this.scheduledTask;
93 if (scheduledTask != null && !scheduledTask.isDone()) {
94 scheduledTask.cancel(true);
99 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
100 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
104 protected @Nullable ThingHandler createHandler(Thing thing) {
105 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
106 if (thingTypeUID.equals(THING_TYPE_MIIO)) {
107 return new MiIoGenericHandler(thing, miIoDatabaseWatchService, cloudConnector);
109 if (thingTypeUID.equals(THING_TYPE_BASIC)) {
110 return new MiIoBasicHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
111 basicChannelTypeProvider);
113 if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
114 return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry);
116 return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService, cloudConnector);