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.tr064.internal;
15 import static org.openhab.binding.tr064.internal.Tr064BindingConstants.*;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.eclipse.jetty.util.ssl.SslContextFactory;
25 import org.openhab.binding.tr064.internal.phonebook.PhonebookProfileFactory;
26 import org.openhab.core.io.net.http.HttpClientFactory;
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.Deactivate;
36 import org.osgi.service.component.annotations.Reference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link Tr064HandlerFactory} is responsible for creating things and thing
44 * @author Jan N. Klug - Initial contribution
47 @Component(service = { ThingHandlerFactory.class }, configurationPid = "binding.tr064")
48 public class Tr064HandlerFactory extends BaseThingHandlerFactory {
49 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
50 .of(Tr064RootHandler.SUPPORTED_THING_TYPES, Tr064SubHandler.SUPPORTED_THING_TYPES).flatMap(Set::stream)
51 .collect(Collectors.toSet());
53 private final Logger logger = LoggerFactory.getLogger(Tr064HandlerFactory.class);
54 private final HttpClient httpClient;
55 private final PhonebookProfileFactory phonebookProfileFactory;
57 // the Tr064ChannelTypeProvider is needed for creating the channels and
58 // referenced here to make sure it is available before things are
60 @SuppressWarnings("unused")
61 private final Tr064ChannelTypeProvider channelTypeProvider;
64 public Tr064HandlerFactory(@Reference Tr064ChannelTypeProvider channelTypeProvider,
65 @Reference PhonebookProfileFactory phonebookProfileFactory,
66 final @Reference HttpClientFactory httpClientFactory) {
67 this.channelTypeProvider = channelTypeProvider;
68 this.phonebookProfileFactory = phonebookProfileFactory;
69 // use an insecure client (i.e. without verifying the certificate)
70 this.httpClient = httpClientFactory.createHttpClient(BINDING_ID, new SslContextFactory.Client(true));
72 this.httpClient.start();
73 } catch (Exception e) {
74 // catching exception is necessary due to the signature of HttpClient.start()
75 logger.warn("Failed to start http client: {}", e.getMessage());
76 throw new IllegalStateException("Could not create HttpClient instance.", e);
81 public void deactivate() {
84 } catch (Exception e) {
85 // catching exception is necessary due to the signature of HttpClient.stop()
86 logger.warn("Failed to stop http client: {}", e.getMessage());
91 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
92 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
96 protected @Nullable ThingHandler createHandler(Thing thing) {
97 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
99 if (Tr064RootHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
100 Tr064RootHandler handler = new Tr064RootHandler((Bridge) thing, httpClient);
101 if (THING_TYPE_FRITZBOX.equals(thingTypeUID)) {
102 phonebookProfileFactory.registerPhonebookProvider(handler);
105 } else if (Tr064SubHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
106 return new Tr064SubHandler(thing);
113 protected void removeHandler(ThingHandler thingHandler) {
114 if (thingHandler instanceof Tr064RootHandler) {
115 phonebookProfileFactory.unregisterPhonebookProvider((Tr064RootHandler) thingHandler);