]> git.basschouten.com Git - openhab-addons.git/blob
afb8073b11228e3305f44351b2892948087b4787
[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.Future;
19 import java.util.concurrent.RejectedExecutionException;
20 import java.util.concurrent.ScheduledExecutorService;
21
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.MiIoGenericHandler;
29 import org.openhab.binding.miio.internal.handler.MiIoUnsupportedHandler;
30 import org.openhab.binding.miio.internal.handler.MiIoVacuumHandler;
31 import org.openhab.core.common.ThreadPoolManager;
32 import org.openhab.core.io.net.http.HttpClientFactory;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerFactory;
38 import org.openhab.core.thing.type.ChannelTypeRegistry;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Deactivate;
42 import org.osgi.service.component.annotations.Reference;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * The {@link MiIoHandlerFactory} is responsible for creating things and thing
48  * handlers.
49  *
50  * @author Marcel Verpaalen - Initial contribution
51  */
52 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.miio")
53 @NonNullByDefault
54 public class MiIoHandlerFactory extends BaseThingHandlerFactory {
55     private static final String THING_HANDLER_THREADPOOL_NAME = "thingHandler";
56     protected final ScheduledExecutorService scheduler = ThreadPoolManager
57             .getScheduledPool(THING_HANDLER_THREADPOOL_NAME);
58     private final HttpClientFactory httpClientFactory;
59     private MiIoDatabaseWatchService miIoDatabaseWatchService;
60     private CloudConnector cloudConnector;
61     private ChannelTypeRegistry channelTypeRegistry;
62     private BasicChannelTypeProvider basicChannelTypeProvider;
63     private @Nullable Future<Boolean> scheduledTask;
64     private final Logger logger = LoggerFactory.getLogger(MiIoHandlerFactory.class);
65
66     @Activate
67     public MiIoHandlerFactory(@Reference HttpClientFactory httpClientFactory,
68             @Reference ChannelTypeRegistry channelTypeRegistry,
69             @Reference MiIoDatabaseWatchService miIoDatabaseWatchService, @Reference CloudConnector cloudConnector,
70             @Reference BasicChannelTypeProvider basicChannelTypeProvider, Map<String, Object> properties) {
71         this.httpClientFactory = httpClientFactory;
72         this.miIoDatabaseWatchService = miIoDatabaseWatchService;
73         this.channelTypeRegistry = channelTypeRegistry;
74         this.basicChannelTypeProvider = basicChannelTypeProvider;
75         this.cloudConnector = cloudConnector;
76         @Nullable
77         String username = (String) properties.get("username");
78         @Nullable
79         String password = (String) properties.get("password");
80         @Nullable
81         String country = (String) properties.get("country");
82         cloudConnector.setCredentials(username, password, country);
83         try {
84             if (!scheduler.isShutdown()) {
85                 scheduledTask = scheduler.submit(() -> cloudConnector.isConnected(true));
86             } else {
87                 logger.debug("Unexpected: ScheduledExecutorService is shutdown.");
88             }
89         } catch (RejectedExecutionException e) {
90             logger.debug("Unexpected: ScheduledExecutorService task rejected.", e);
91         }
92     }
93
94     @Deactivate
95     public void dispose() {
96         final Future<Boolean> scheduledTask = this.scheduledTask;
97         if (scheduledTask != null && !scheduledTask.isDone()) {
98             scheduledTask.cancel(true);
99         }
100     }
101
102     @Override
103     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
104         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
105     }
106
107     @Override
108     protected @Nullable ThingHandler createHandler(Thing thing) {
109         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
110         if (thingTypeUID.equals(THING_TYPE_MIIO)) {
111             return new MiIoGenericHandler(thing, miIoDatabaseWatchService, cloudConnector);
112         }
113         if (thingTypeUID.equals(THING_TYPE_BASIC)) {
114             return new MiIoBasicHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry,
115                     basicChannelTypeProvider);
116         }
117         if (thingTypeUID.equals(THING_TYPE_VACUUM)) {
118             return new MiIoVacuumHandler(thing, miIoDatabaseWatchService, cloudConnector, channelTypeRegistry);
119         }
120         return new MiIoUnsupportedHandler(thing, miIoDatabaseWatchService, cloudConnector,
121                 httpClientFactory.getCommonHttpClient());
122     }
123 }