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.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;
40 * The {@code SmartherHandlerFactory} class is responsible for creating things and thing handlers.
42 * @author Fabio Possieri - Initial contribution
44 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.bticinosmarther")
46 public class SmartherHandlerFactory extends BaseThingHandlerFactory {
48 private final Logger logger = LoggerFactory.getLogger(SmartherHandlerFactory.class);
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;
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;
71 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72 return SmartherBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76 protected @Nullable ThingHandler createHandler(Thing thing) {
77 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79 if (SmartherBindingConstants.THING_TYPE_BRIDGE.equals(thingTypeUID)) {
80 final SmartherBridgeHandler handler = new SmartherBridgeHandler((Bridge) thing, oAuthFactory, httpClient);
81 this.authService.addSmartherAccountHandler(handler);
83 } else if (SmartherBindingConstants.THING_TYPE_MODULE.equals(thingTypeUID)) {
84 return new SmartherModuleHandler(thing, cronScheduler, dynamicStateDescriptionProvider, timeZoneProvider);
86 logger.debug("Unsupported thing {}", thing.getThingTypeUID());
92 protected synchronized void removeHandler(ThingHandler thingHandler) {
93 if (thingHandler instanceof SmartherBridgeHandler) {
94 authService.removeSmartherAccountHandler((SmartherBridgeHandler) thingHandler);