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