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.ScheduledExecutorService;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.miio.internal.basic.BasicChannelTypeProvider;
23 import org.openhab.binding.miio.internal.basic.MiIoDatabaseWatchService;
24 import org.openhab.binding.miio.internal.cloud.CloudConnector;
25 import org.openhab.binding.miio.internal.handler.MiIoBasicHandler;
26 import org.openhab.binding.miio.internal.handler.MiIoGenericHandler;
27 import org.openhab.binding.miio.internal.handler.MiIoUnsupportedHandler;
28 import org.openhab.binding.miio.internal.handler.MiIoVacuumHandler;
29 import org.openhab.core.common.ThreadPoolManager;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.openhab.core.thing.type.ChannelTypeRegistry;
36 import org.osgi.service.component.annotations.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
41 * The {@link MiIoHandlerFactory} is responsible for creating things and thing
44 * @author Marcel Verpaalen - Initial contribution
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miio")
48 public class MiIoHandlerFactory extends BaseThingHandlerFactory {
49 private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler";
50 protected final ScheduledExecutorService scheduler = ThreadPoolManager
51 .getScheduledPool(THING_HANDLER_THREADPOOL_NAME);
53 private MiIoDatabaseWatchService miIoDatabaseWatchService;
54 private CloudConnector cloudConnector;
55 private ChannelTypeRegistry channelTypeRegistry;
56 private BasicChannelTypeProvider basicChannelTypeProvider;
59 public MiIoHandlerFactory(@Reference ChannelTypeRegistry channelTypeRegistry,
60 @Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector,
61 @Reference BasicChannelTypeProvider basicChannelTypeProvider, Map<String, Object> properties) {
62 this.miIoDatabaseWatchService = miIoDatabaseWatchService;
63 this.cloudConnector = cloudConnector;
65 String username = (String) properties.get("username");
67 String password = (String) properties.get("password");
69 String country = (String) properties.get("country");
70 cloudConnector.setCredentials(username, password, country);
71 scheduler.submit(() -> cloudConnector.isConnected());
72 this.channelTypeRegistry = channelTypeRegistry;
73 this.basicChannelTypeProvider = basicChannelTypeProvider;
77 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
78 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
82 protected @Nullable ThingHandler createHandler(Thing thing) {
83 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
84 if (thingTypeUID.equals(THING_TYPE_MIIO)) {
85 return new MiIoGenericHandler(thing, miIoDatabaseWatchService, cloudConnector);
87 if (thingTypeUID.equals(THING_TYPE_BASIC)) {
88 return new MiIoBasicHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
89 basicChannelTypeProvider);
91 if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
92 return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry);
94 return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService, cloudConnector);