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.THING_TYPE_FRITZBOX;
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.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.Deactivate;
35 import org.osgi.service.component.annotations.Reference;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link Tr064HandlerFactory} is responsible for creating things and thing
43 * @author Jan N. Klug - Initial contribution
46 @Component(service = { ThingHandlerFactory.class }, configurationPid = "binding.tr064")
47 public class Tr064HandlerFactory extends BaseThingHandlerFactory {
48 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
49 .of(Tr064RootHandler.SUPPORTED_THING_TYPES, Tr064SubHandler.SUPPORTED_THING_TYPES).flatMap(Set::stream)
50 .collect(Collectors.toSet());
52 private final Logger logger = LoggerFactory.getLogger(Tr064HandlerFactory.class);
53 private final HttpClient httpClient;
54 private final PhonebookProfileFactory phonebookProfileFactory;
56 // the Tr064ChannelTypeProvider is needed for creating the channels and
57 // referenced here to make sure it is available before things are
59 @SuppressWarnings("unused")
60 private final Tr064ChannelTypeProvider channelTypeProvider;
63 public Tr064HandlerFactory(@Reference Tr064ChannelTypeProvider channelTypeProvider,
64 @Reference PhonebookProfileFactory phonebookProfileFactory) {
65 this.channelTypeProvider = channelTypeProvider;
66 this.phonebookProfileFactory = phonebookProfileFactory;
67 // use an insecure client (i.e. without verifying the certificate)
68 this.httpClient = new HttpClient(new SslContextFactory.Client(true));
70 this.httpClient.start();
71 } catch (Exception e) {
72 // catching exception is necessary due to the signature of HttpClient.start()
73 logger.warn("Failed to start http client: {}", e.getMessage());
74 throw new IllegalStateException("Could not create HttpClient instance.", e);
79 public void deactivate() {
82 } catch (Exception e) {
83 // catching exception is necessary due to the signature of HttpClient.stop()
84 logger.warn("Failed to stop http client: {}", e.getMessage());
89 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
90 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
94 protected @Nullable ThingHandler createHandler(Thing thing) {
95 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
97 if (Tr064RootHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
98 Tr064RootHandler handler = new Tr064RootHandler((Bridge) thing, httpClient);
99 if (thingTypeUID.equals(THING_TYPE_FRITZBOX)) {
100 phonebookProfileFactory.registerPhonebookProvider(handler);
103 } else if (Tr064SubHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
104 return new Tr064SubHandler(thing);
111 protected void removeHandler(ThingHandler thingHandler) {
112 if (thingHandler instanceof Tr064RootHandler) {
113 phonebookProfileFactory.unregisterPhonebookProvider((Tr064RootHandler) thingHandler);