]> git.basschouten.com Git - openhab-addons.git/blob
5ceaeb7b34aac9014b7a74fcdfe03bb4f2b01f06
[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.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;
44
45 import com.google.gson.Gson;
46 import com.google.gson.GsonBuilder;
47 import com.google.gson.JsonDeserializer;
48
49 /**
50  * The {@link LinkyHandlerFactory} is responsible for creating things handlers.
51  *
52  * @author GaĆ«l L'hopital - Initial contribution
53  */
54 @NonNullByDefault
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;
59
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))
64             .create();
65     private final LocaleProvider localeProvider;
66     private final HttpClient httpClient;
67
68     @Activate
69     public LinkyHandlerFactory(final @Reference LocaleProvider localeProvider,
70             final @Reference HttpClientFactory httpClientFactory) {
71         this.localeProvider = localeProvider;
72         SslContextFactory sslContextFactory = new SslContextFactory.Client();
73         try {
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(),
79                     e);
80         } catch (KeyManagementException e) {
81             logger.warn("An exception occurred while initialising the SSL context : '{}'", e.getMessage(), e);
82         }
83         this.httpClient = httpClientFactory.createHttpClient(LinkyBindingConstants.BINDING_ID, sslContextFactory);
84         httpClient.setFollowRedirects(false);
85         httpClient.setRequestBufferSize(REQUEST_BUFFER_SIZE);
86     }
87
88     @Override
89     protected void activate(ComponentContext componentContext) {
90         super.activate(componentContext);
91         try {
92             httpClient.start();
93         } catch (Exception e) {
94             logger.warn("Unable to start Jetty HttpClient {}", e.getMessage());
95         }
96     }
97
98     @Override
99     protected void deactivate(ComponentContext componentContext) {
100         super.deactivate(componentContext);
101         try {
102             httpClient.stop();
103         } catch (Exception e) {
104             logger.warn("Unable to stop Jetty HttpClient {}", e.getMessage());
105         }
106     }
107
108     @Override
109     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
110         return THING_TYPE_LINKY.equals(thingTypeUID);
111     }
112
113     @Override
114     protected @Nullable ThingHandler createHandler(Thing thing) {
115         return supportsThingType(thing.getThingTypeUID()) ? new LinkyHandler(thing, localeProvider, gson, httpClient)
116                 : null;
117     }
118 }