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