2 * Copyright (c) 2010-2023 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.MiIoGatewayHandler;
29 import org.openhab.binding.miio.internal.handler.MiIoGenericHandler;
30 import org.openhab.binding.miio.internal.handler.MiIoLumiHandler;
31 import org.openhab.binding.miio.internal.handler.MiIoUnsupportedHandler;
32 import org.openhab.binding.miio.internal.handler.MiIoVacuumHandler;
33 import org.openhab.core.common.ThreadPoolManager;
34 import org.openhab.core.i18n.LocaleProvider;
35 import org.openhab.core.i18n.TranslationProvider;
36 import org.openhab.core.io.net.http.HttpClientFactory;
37 import org.openhab.core.thing.Bridge;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingTypeUID;
40 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.thing.binding.ThingHandlerFactory;
43 import org.openhab.core.thing.type.ChannelTypeRegistry;
44 import org.osgi.service.component.annotations.Activate;
45 import org.osgi.service.component.annotations.Component;
46 import org.osgi.service.component.annotations.Deactivate;
47 import org.osgi.service.component.annotations.Reference;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * The {@link MiIoHandlerFactory} is responsible for creating things and thing
55 * @author Marcel Verpaalen - Initial contribution
57 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miio")
59 public class MiIoHandlerFactory extends BaseThingHandlerFactory {
60 private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler";
61 protected final ScheduledExecutorService scheduler = ThreadPoolManager
62 .getScheduledPool(THING_HANDLER_THREADPOOL_NAME);
63 private final HttpClientFactory httpClientFactory;
64 private MiIoDatabaseWatchService miIoDatabaseWatchService;
65 private CloudConnector cloudConnector;
66 private ChannelTypeRegistry channelTypeRegistry;
67 private BasicChannelTypeProvider basicChannelTypeProvider;
68 private final TranslationProvider i18nProvider;
69 private final LocaleProvider localeProvider;
70 private @Nullable Future<Boolean> scheduledTask;
71 private final Logger logger = LoggerFactory.getLogger(MiIoHandlerFactory.class);
74 public MiIoHandlerFactory(@Reference HttpClientFactory httpClientFactory,
75 @Reference ChannelTypeRegistry channelTypeRegistry,
76 @Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector,
77 @Reference BasicChannelTypeProvider basicChannelTypeProvider, @Reference TranslationProvider i18nProvider,
78 @Reference LocaleProvider localeProvider, Map<String, Object> properties) {
79 this.httpClientFactory = httpClientFactory;
80 this.miIoDatabaseWatchService = miIoDatabaseWatchService;
81 this.channelTypeRegistry = channelTypeRegistry;
82 this.basicChannelTypeProvider = basicChannelTypeProvider;
83 this.i18nProvider = i18nProvider;
84 this.localeProvider = localeProvider;
85 this.cloudConnector = cloudConnector;
87 String username = (String) properties.get("username");
89 String password = (String) properties.get("password");
91 String country = (String) properties.get("country");
92 cloudConnector.setCredentials(username, password, country);
94 if (!scheduler.isShutdown()) {
95 scheduledTask = scheduler.submit(() -> cloudConnector.isConnected(true));
97 logger.debug("Unexpected: ScheduledExecutorService is shutdown.");
99 } catch (RejectedExecutionException e) {
100 logger.debug("Unexpected: ScheduledExecutorService task rejected.", e);
105 public void dispose() {
106 final Future<Boolean> scheduledTask = this.scheduledTask;
107 if (scheduledTask != null && !scheduledTask.isDone()) {
108 scheduledTask.cancel(true);
113 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
114 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
118 protected @Nullable ThingHandler createHandler(Thing thing) {
119 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
120 if (thingTypeUID.equals(THING_TYPE_MIIO)) {
121 return new MiIoGenericHandler(thing, miIoDatabaseWatchService, cloudConnector, i18nProvider,
124 if (thingTypeUID.equals(THING_TYPE_BASIC)) {
125 return new MiIoBasicHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
126 basicChannelTypeProvider, i18nProvider, localeProvider);
128 if (thingTypeUID.equals(THING_TYPE_LUMI)) {
129 return new MiIoLumiHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
130 basicChannelTypeProvider, i18nProvider, localeProvider);
132 if (thingTypeUID.equals(THING_TYPE_GATEWAY)) {
133 return new MiIoGatewayHandler((Bridge) thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
134 basicChannelTypeProvider, i18nProvider, localeProvider);
136 if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
137 return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
138 i18nProvider, localeProvider);
140 return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService, cloudConnector,
141 httpClientFactory.getCommonHttpClient(), i18nProvider, localeProvider);