]> git.basschouten.com Git - openhab-addons.git/blob
129ce0a6b82778843f1abf794cadb24739bf9b0a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.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;
39
40 /**
41  * The {@link MiIoHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author Marcel Verpaalen - Initial contribution
45  */
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miio")
47 @NonNullByDefault
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);
52
53     private MiIoDatabaseWatchService miIoDatabaseWatchService;
54     private CloudConnector cloudConnector;
55     private ChannelTypeRegistry channelTypeRegistry;
56     private BasicChannelTypeProvider basicChannelTypeProvider;
57
58     @Activate
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;
64         @Nullable
65         String username = (String) properties.get("username");
66         @Nullable
67         String password = (String) properties.get("password");
68         @Nullable
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;
74     }
75
76     @Override
77     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
78         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
79     }
80
81     @Override
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);
86         }
87         if (thingTypeUID.equals(THING_TYPE_BASIC)) {
88             return new MiIoBasicHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
89                     basicChannelTypeProvider);
90         }
91         if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
92             return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry);
93         }
94         return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService, cloudConnector);
95     }
96 }