2 * Copyright (c) 2010-2020 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.MiIoDatabaseWatchService;
23 import org.openhab.binding.miio.internal.cloud.CloudConnector;
24 import org.openhab.binding.miio.internal.handler.MiIoBasicHandler;
25 import org.openhab.binding.miio.internal.handler.MiIoGenericHandler;
26 import org.openhab.binding.miio.internal.handler.MiIoUnsupportedHandler;
27 import org.openhab.binding.miio.internal.handler.MiIoVacuumHandler;
28 import org.openhab.core.common.ThreadPoolManager;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.openhab.core.thing.type.ChannelTypeRegistry;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
40 * The {@link MiIoHandlerFactory} is responsible for creating things and thing
43 * @author Marcel Verpaalen - Initial contribution
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miio")
47 public class MiIoHandlerFactory extends BaseThingHandlerFactory {
48 private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler";
49 protected final ScheduledExecutorService scheduler = ThreadPoolManager
50 .getScheduledPool(THING_HANDLER_THREADPOOL_NAME);
52 private MiIoDatabaseWatchService miIoDatabaseWatchService;
53 private CloudConnector cloudConnector;
54 private ChannelTypeRegistry channelTypeRegistry;
57 public MiIoHandlerFactory(@Reference ChannelTypeRegistry channelTypeRegistry,
58 @Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector,
59 Map<String, Object> properties) {
60 this.miIoDatabaseWatchService = miIoDatabaseWatchService;
61 this.cloudConnector = cloudConnector;
63 String username = (String) properties.get("username");
65 String password = (String) properties.get("password");
67 String country = (String) properties.get("country");
68 cloudConnector.setCredentials(username, password, country);
69 scheduler.submit(() -> cloudConnector.isConnected());
70 this.channelTypeRegistry = channelTypeRegistry;
74 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
75 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
79 protected @Nullable ThingHandler createHandler(Thing thing) {
80 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
81 if (thingTypeUID.equals(THING_TYPE_MIIO)) {
82 return new MiIoGenericHandler(thing, miIoDatabaseWatchService, cloudConnector);
84 if (thingTypeUID.equals(THING_TYPE_BASIC)) {
85 return new MiIoBasicHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry);
87 if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
88 return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry);
90 return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService, cloudConnector);