]> git.basschouten.com Git - openhab-addons.git/blob
2d23cde6890c0e74364e11d2771b10fba067a6a0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Set;
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.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;
38
39 /**
40  * The {@link Tr064HandlerFactory} is responsible for creating things and thing
41  * handlers.
42  *
43  * @author Jan N. Klug - Initial contribution
44  */
45 @NonNullByDefault
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());
51
52     private final Logger logger = LoggerFactory.getLogger(Tr064HandlerFactory.class);
53     private final HttpClient httpClient;
54     private final PhonebookProfileFactory phonebookProfileFactory;
55
56     // the Tr064ChannelTypeProvider is needed for creating the channels and
57     // referenced here to make sure it is available before things are
58     // initialized
59     @SuppressWarnings("unused")
60     private final Tr064ChannelTypeProvider channelTypeProvider;
61
62     @Activate
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));
69         try {
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);
75         }
76     }
77
78     @Deactivate
79     public void deactivate() {
80         try {
81             httpClient.stop();
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());
85         }
86     }
87
88     @Override
89     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
90         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
91     }
92
93     @Override
94     protected @Nullable ThingHandler createHandler(Thing thing) {
95         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
96
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);
101             }
102             return handler;
103         } else if (Tr064SubHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
104             return new Tr064SubHandler(thing);
105         }
106
107         return null;
108     }
109
110     @Override
111     protected void removeHandler(ThingHandler thingHandler) {
112         if (thingHandler instanceof Tr064RootHandler) {
113             phonebookProfileFactory.unregisterPhonebookProvider((Tr064RootHandler) thingHandler);
114         }
115     }
116 }