2 * Copyright (c) 2010-2022 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.i18n.LocaleProvider;
33 import org.openhab.core.i18n.TranslationProvider;
34 import org.openhab.core.io.net.http.HttpClientFactory;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerFactory;
40 import org.openhab.core.thing.type.ChannelTypeRegistry;
41 import org.osgi.service.component.annotations.Activate;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Deactivate;
44 import org.osgi.service.component.annotations.Reference;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * The {@link MiIoHandlerFactory} is responsible for creating things and thing
52 * @author Marcel Verpaalen - Initial contribution
54 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miio")
56 public class MiIoHandlerFactory extends BaseThingHandlerFactory {
57 private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler";
58 protected final ScheduledExecutorService scheduler = ThreadPoolManager
59 .getScheduledPool(THING_HANDLER_THREADPOOL_NAME);
60 private final HttpClientFactory httpClientFactory;
61 private MiIoDatabaseWatchService miIoDatabaseWatchService;
62 private CloudConnector cloudConnector;
63 private ChannelTypeRegistry channelTypeRegistry;
64 private BasicChannelTypeProvider basicChannelTypeProvider;
65 private final TranslationProvider i18nProvider;
66 private final LocaleProvider localeProvider;
67 private @Nullable Future<Boolean> scheduledTask;
68 private final Logger logger = LoggerFactory.getLogger(MiIoHandlerFactory.class);
71 public MiIoHandlerFactory(@Reference HttpClientFactory httpClientFactory,
72 @Reference ChannelTypeRegistry channelTypeRegistry,
73 @Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector,
74 @Reference BasicChannelTypeProvider basicChannelTypeProvider, @Reference TranslationProvider i18nProvider,
75 @Reference LocaleProvider localeProvider, Map<String, Object> properties) {
76 this.httpClientFactory = httpClientFactory;
77 this.miIoDatabaseWatchService = miIoDatabaseWatchService;
78 this.channelTypeRegistry = channelTypeRegistry;
79 this.basicChannelTypeProvider = basicChannelTypeProvider;
80 this.i18nProvider = i18nProvider;
81 this.localeProvider = localeProvider;
82 this.cloudConnector = cloudConnector;
84 String username = (String) properties.get("username");
86 String password = (String) properties.get("password");
88 String country = (String) properties.get("country");
89 cloudConnector.setCredentials(username, password, country);
91 if (!scheduler.isShutdown()) {
92 scheduledTask = scheduler.submit(() -> cloudConnector.isConnected(true));
94 logger.debug("Unexpected: ScheduledExecutorService is shutdown.");
96 } catch (RejectedExecutionException e) {
97 logger.debug("Unexpected: ScheduledExecutorService task rejected.", e);
102 public void dispose() {
103 final Future<Boolean> scheduledTask = this.scheduledTask;
104 if (scheduledTask != null && !scheduledTask.isDone()) {
105 scheduledTask.cancel(true);
110 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
111 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
115 protected @Nullable ThingHandler createHandler(Thing thing) {
116 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
117 if (thingTypeUID.equals(THING_TYPE_MIIO)) {
118 return new MiIoGenericHandler(thing, miIoDatabaseWatchService, cloudConnector, i18nProvider,
121 if (thingTypeUID.equals(THING_TYPE_BASIC)) {
122 return new MiIoBasicHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
123 basicChannelTypeProvider, i18nProvider, localeProvider);
125 if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
126 return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
127 i18nProvider, localeProvider);
129 return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService, cloudConnector,
130 httpClientFactory.getCommonHttpClient(), i18nProvider, localeProvider);