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