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