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