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.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;
44 import com.google.gson.Gson;
45 import com.google.gson.GsonBuilder;
46 import com.google.gson.JsonDeserializer;
49 * The {@link LinkyHandlerFactory} is responsible for creating things handlers.
51 * @author Gaƫl L'hopital - Initial contribution
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;
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))
64 private final LocaleProvider localeProvider;
65 private final HttpClient httpClient;
68 public LinkyHandlerFactory(final @Reference LocaleProvider localeProvider,
69 final @Reference HttpClientFactory httpClientFactory) {
70 this.localeProvider = localeProvider;
71 this.httpClient = httpClientFactory.createHttpClient(LinkyBindingConstants.BINDING_ID);
75 protected void activate(ComponentContext componentContext) {
76 super.activate(componentContext);
77 httpClient.setFollowRedirects(false);
78 httpClient.setRequestBufferSize(REQUEST_BUFFER_SIZE);
81 SSLContext sslContext = SSLContext.getInstance("SSL");
82 sslContext.init(null, new TrustManager[] { TrustAllTrustManager.getInstance() }, null);
83 httpClient.getSslContextFactory().setSslContext(sslContext);
85 } catch (NoSuchAlgorithmException e) {
86 logger.warn("An exception occurred while requesting the SSL encryption algorithm : '{}'", e.getMessage(),
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());
96 protected void deactivate(ComponentContext componentContext) {
97 super.deactivate(componentContext);
100 } catch (Exception e) {
101 logger.warn("Unable to stop Jetty HttpClient {}", e.getMessage());
106 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
107 return THING_TYPE_LINKY.equals(thingTypeUID);
111 protected @Nullable ThingHandler createHandler(Thing thing) {
112 return supportsThingType(thing.getThingTypeUID()) ? new LinkyHandler(thing, localeProvider, gson, httpClient)