]> git.basschouten.com Git - openhab-addons.git/blob
2da48f935377458fc85222c7062f0d96053ebb4c
[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.linky.internal;
14
15 import static org.openhab.binding.linky.internal.LinkyBindingConstants.THING_TYPE_LINKY;
16
17 import java.security.KeyManagementException;
18 import java.security.NoSuchAlgorithmException;
19 import java.time.ZonedDateTime;
20 import java.time.format.DateTimeFormatter;
21
22 import javax.net.ssl.SSLContext;
23 import javax.net.ssl.TrustManager;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.eclipse.jetty.client.HttpClient;
28 import org.openhab.binding.linky.internal.handler.LinkyHandler;
29 import org.openhab.core.i18n.LocaleProvider;
30 import org.openhab.core.io.net.http.HttpClientFactory;
31 import org.openhab.core.io.net.http.TrustAllTrustManager;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.service.component.ComponentContext;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.google.gson.Gson;
45 import com.google.gson.GsonBuilder;
46 import com.google.gson.JsonDeserializer;
47
48 /**
49  * The {@link LinkyHandlerFactory} is responsible for creating things handlers.
50  *
51  * @author GaĆ«l L'hopital - Initial contribution
52  */
53 @NonNullByDefault
54 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.linky")
55 public class LinkyHandlerFactory extends BaseThingHandlerFactory {
56     private static final DateTimeFormatter LINKY_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");
57     private static final int REQUEST_BUFFER_SIZE = 8000;
58
59     private final Logger logger = LoggerFactory.getLogger(LinkyHandlerFactory.class);
60     private final Gson gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class,
61             (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
62                     .parse(json.getAsJsonPrimitive().getAsString(), LINKY_FORMATTER))
63             .create();
64     private final LocaleProvider localeProvider;
65     private final HttpClient httpClient;
66
67     @Activate
68     public LinkyHandlerFactory(final @Reference LocaleProvider localeProvider,
69             final @Reference HttpClientFactory httpClientFactory) {
70         this.localeProvider = localeProvider;
71         this.httpClient = httpClientFactory.createHttpClient(LinkyBindingConstants.BINDING_ID);
72     }
73
74     @Override
75     protected void activate(ComponentContext componentContext) {
76         super.activate(componentContext);
77         httpClient.setFollowRedirects(false);
78         httpClient.setRequestBufferSize(REQUEST_BUFFER_SIZE);
79
80         try {
81             SSLContext sslContext = SSLContext.getInstance("SSL");
82             sslContext.init(null, new TrustManager[] { TrustAllTrustManager.getInstance() }, null);
83             httpClient.getSslContextFactory().setSslContext(sslContext);
84             httpClient.start();
85         } catch (NoSuchAlgorithmException e) {
86             logger.warn("An exception occurred while requesting the SSL encryption algorithm : '{}'", e.getMessage(),
87                     e);
88         } catch (KeyManagementException e) {
89             logger.warn("An exception occurred while initialising the SSL context : '{}'", e.getMessage(), e);
90         } catch (Exception e) {
91             logger.warn("Unable to start Jetty HttpClient {}", e.getMessage());
92         }
93     }
94
95     @Override
96     protected void deactivate(ComponentContext componentContext) {
97         super.deactivate(componentContext);
98         try {
99             httpClient.stop();
100         } catch (Exception e) {
101             logger.warn("Unable to stop Jetty HttpClient {}", e.getMessage());
102         }
103     }
104
105     @Override
106     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
107         return THING_TYPE_LINKY.equals(thingTypeUID);
108     }
109
110     @Override
111     protected @Nullable ThingHandler createHandler(Thing thing) {
112         return supportsThingType(thing.getThingTypeUID()) ? new LinkyHandler(thing, localeProvider, gson, httpClient)
113                 : null;
114     }
115 }