2 * Copyright (c) 2010-2020 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.openhab.binding.tr064.internal.phonebook.PhonebookProfileFactory;
25 import org.openhab.core.io.net.http.HttpClientFactory;
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;
37 * The {@link Tr064HandlerFactory} is responsible for creating things and thing
40 * @author Jan N. Klug - Initial contribution
43 @Component(service = { ThingHandlerFactory.class }, configurationPid = "binding.tr064")
44 public class Tr064HandlerFactory extends BaseThingHandlerFactory {
45 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
46 .of(Tr064RootHandler.SUPPORTED_THING_TYPES, Tr064SubHandler.SUPPORTED_THING_TYPES).flatMap(Set::stream)
47 .collect(Collectors.toSet());
49 private final HttpClient httpClient;
50 private final PhonebookProfileFactory phonebookProfileFactory;
52 // the Tr064ChannelTypeProvider is needed for creating the channels and
53 // referenced here to make sure it is available before things are
55 @SuppressWarnings("unused")
56 private final Tr064ChannelTypeProvider channelTypeProvider;
59 public Tr064HandlerFactory(@Reference HttpClientFactory httpClientFactory,
60 @Reference Tr064ChannelTypeProvider channelTypeProvider,
61 @Reference PhonebookProfileFactory phonebookProfileFactory) {
62 httpClient = httpClientFactory.getCommonHttpClient();
63 this.channelTypeProvider = channelTypeProvider;
64 this.phonebookProfileFactory = phonebookProfileFactory;
68 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73 protected @Nullable ThingHandler createHandler(Thing thing) {
74 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
76 if (Tr064RootHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
77 Tr064RootHandler handler = new Tr064RootHandler((Bridge) thing, httpClient);
78 if (thingTypeUID.equals(THING_TYPE_FRITZBOX)) {
79 phonebookProfileFactory.registerPhonebookProvider(handler);
82 } else if (Tr064SubHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
83 return new Tr064SubHandler(thing);
90 protected void removeHandler(ThingHandler thingHandler) {
91 if (thingHandler instanceof Tr064RootHandler) {
92 phonebookProfileFactory.unregisterPhonebookProvider((Tr064RootHandler) thingHandler);