]> git.basschouten.com Git - openhab-addons.git/blob
636e0d318dab892cea54eb307f996137fc1df6e4
[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.bticinosmarther.internal.factory;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.eclipse.jetty.client.HttpClient;
18 import org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants;
19 import org.openhab.binding.bticinosmarther.internal.account.SmartherAccountService;
20 import org.openhab.binding.bticinosmarther.internal.handler.SmartherBridgeHandler;
21 import org.openhab.binding.bticinosmarther.internal.handler.SmartherDynamicStateDescriptionProvider;
22 import org.openhab.binding.bticinosmarther.internal.handler.SmartherModuleHandler;
23 import org.openhab.core.auth.client.oauth2.OAuthFactory;
24 import org.openhab.core.io.net.http.HttpClientFactory;
25 import org.openhab.core.scheduler.CronScheduler;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerFactory;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@code SmartherHandlerFactory} class is responsible for creating things and thing handlers.
40  *
41  * @author Fabio Possieri - Initial contribution
42  */
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bticinosmarther")
44 @NonNullByDefault
45 public class SmartherHandlerFactory extends BaseThingHandlerFactory {
46
47     private final Logger logger = LoggerFactory.getLogger(SmartherHandlerFactory.class);
48
49     private final OAuthFactory oAuthFactory;
50     private final SmartherAccountService authService;
51     private final HttpClient httpClient;
52     private final CronScheduler cronScheduler;
53     private final SmartherDynamicStateDescriptionProvider dynamicStateDescriptionProvider;
54
55     @Activate
56     public SmartherHandlerFactory(@Reference OAuthFactory oAuthFactory, @Reference SmartherAccountService authService,
57             @Reference HttpClientFactory httpClientFactory, @Reference CronScheduler cronScheduler,
58             @Reference SmartherDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
59         this.oAuthFactory = oAuthFactory;
60         this.authService = authService;
61         this.httpClient = httpClientFactory.getCommonHttpClient();
62         this.cronScheduler = cronScheduler;
63         this.dynamicStateDescriptionProvider = dynamicStateDescriptionProvider;
64     }
65
66     @Override
67     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68         return SmartherBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
69     }
70
71     @Override
72     protected @Nullable ThingHandler createHandler(Thing thing) {
73         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
74
75         if (SmartherBindingConstants.THING_TYPE_BRIDGE.equals(thingTypeUID)) {
76             final SmartherBridgeHandler handler = new SmartherBridgeHandler((Bridge) thing, oAuthFactory, httpClient);
77             this.authService.addSmartherAccountHandler(handler);
78             return handler;
79         } else if (SmartherBindingConstants.THING_TYPE_MODULE.equals(thingTypeUID)) {
80             return new SmartherModuleHandler(thing, cronScheduler, dynamicStateDescriptionProvider);
81         } else {
82             logger.debug("Unsupported thing {}", thing.getThingTypeUID());
83             return null;
84         }
85     }
86
87     @Override
88     protected synchronized void removeHandler(ThingHandler thingHandler) {
89         if (thingHandler instanceof SmartherBridgeHandler) {
90             authService.removeSmartherAccountHandler((SmartherBridgeHandler) thingHandler);
91         }
92     }
93 }