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