]> git.basschouten.com Git - openhab-addons.git/blob
c40dd503de6c19e76900a2f97f386239e1424c41
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tr064.internal;
14
15 import static org.openhab.binding.tr064.internal.Tr064BindingConstants.THING_TYPE_FRITZBOX;
16
17 import java.util.*;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
20
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;
35
36 /**
37  * The {@link Tr064HandlerFactory} is responsible for creating things and thing
38  * handlers.
39  *
40  * @author Jan N. Klug - Initial contribution
41  */
42 @NonNullByDefault
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());
48
49     private final HttpClient httpClient;
50     private final PhonebookProfileFactory phonebookProfileFactory;
51
52     // the Tr064ChannelTypeProvider is needed for creating the channels and
53     // referenced here to make sure it is available before things are
54     // initialized
55     @SuppressWarnings("unused")
56     private final Tr064ChannelTypeProvider channelTypeProvider;
57
58     @Activate
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;
65     }
66
67     @Override
68     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
69         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70     }
71
72     @Override
73     protected @Nullable ThingHandler createHandler(Thing thing) {
74         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
75
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);
80             }
81             return handler;
82         } else if (Tr064SubHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
83             return new Tr064SubHandler(thing);
84         }
85
86         return null;
87     }
88
89     @Override
90     protected void removeHandler(ThingHandler thingHandler) {
91         if (thingHandler instanceof Tr064RootHandler) {
92             phonebookProfileFactory.unregisterPhonebookProvider((Tr064RootHandler) thingHandler);
93         }
94     }
95 }