2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.bticinosmarther.internal.factory;
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;
39 * The {@code SmartherHandlerFactory} class is responsible for creating things and thing handlers.
41 * @author Fabio Possieri - Initial contribution
43 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bticinosmarther")
45 public class SmartherHandlerFactory extends BaseThingHandlerFactory {
47 private final Logger logger = LoggerFactory.getLogger(SmartherHandlerFactory.class);
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;
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;
67 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
68 return SmartherBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
72 protected @Nullable ThingHandler createHandler(Thing thing) {
73 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
75 if (SmartherBindingConstants.THING_TYPE_BRIDGE.equals(thingTypeUID)) {
76 final SmartherBridgeHandler handler = new SmartherBridgeHandler((Bridge) thing, oAuthFactory, httpClient);
77 this.authService.addSmartherAccountHandler(handler);
79 } else if (SmartherBindingConstants.THING_TYPE_MODULE.equals(thingTypeUID)) {
80 return new SmartherModuleHandler(thing, cronScheduler, dynamicStateDescriptionProvider);
82 logger.debug("Unsupported thing {}", thing.getThingTypeUID());
88 protected synchronized void removeHandler(ThingHandler thingHandler) {
89 if (thingHandler instanceof SmartherBridgeHandler) {
90 authService.removeSmartherAccountHandler((SmartherBridgeHandler) thingHandler);