]> git.basschouten.com Git - openhab-addons.git/blob
058c07310e20d44c9f90bbd17ca978124e19acb3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.miio.internal;
14
15 import static org.openhab.binding.miio.internal.MiIoBindingConstants.*;
16
17 import java.util.Map;
18 import java.util.concurrent.ScheduledExecutorService;
19
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;
38
39 /**
40  * The {@link MiIoHandlerFactory} is responsible for creating things and thing
41  * handlers.
42  *
43  * @author Marcel Verpaalen - Initial contribution
44  */
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miio")
46 @NonNullByDefault
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);
51
52     private MiIoDatabaseWatchService miIoDatabaseWatchService;
53     private CloudConnector cloudConnector;
54     private ChannelTypeRegistry channelTypeRegistry;
55
56     @Activate
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;
62         @Nullable
63         String username = (String) properties.get("username");
64         @Nullable
65         String password = (String) properties.get("password");
66         @Nullable
67         String country = (String) properties.get("country");
68         cloudConnector.setCredentials(username, password, country);
69         scheduler.submit(() -> cloudConnector.isConnected());
70         this.channelTypeRegistry = channelTypeRegistry;
71     }
72
73     @Override
74     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
75         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76     }
77
78     @Override
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);
83         }
84         if (thingTypeUID.equals(THING_TYPE_BASIC)) {
85             return new MiIoBasicHandler(thing, miIoDatabaseWatchService);
86         }
87         if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
88             return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry);
89         }
90         return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService);
91     }
92 }