]> git.basschouten.com Git - openhab-addons.git/blob
83ff54b41f45ac6e5621e8070f401b611f575cc4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 static final DateTimeFormatter LINKY_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");
51
52     private final Logger logger = LoggerFactory.getLogger(LinkyHandlerFactory.class);
53
54     private final LocaleProvider localeProvider;
55     private final Gson gson;
56     private final HttpClient httpClient;
57
58     @Activate
59     public LinkyHandlerFactory(final @Reference LocaleProvider localeProvider,
60             final @Reference HttpClientFactory httpClientFactory) {
61         this.localeProvider = localeProvider;
62         this.gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class,
63                 (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
64                         .parse(json.getAsJsonPrimitive().getAsString(), LINKY_FORMATTER))
65                 .create();
66         this.httpClient = httpClientFactory.createHttpClient(LinkyBindingConstants.BINDING_ID);
67     }
68
69     @Override
70     protected void activate(ComponentContext componentContext) {
71         super.activate(componentContext);
72         httpClient.getSslContextFactory().setExcludeCipherSuites(new String[0]);
73         httpClient.setFollowRedirects(false);
74         try {
75             httpClient.start();
76         } catch (Exception e) {
77             logger.warn("Unable to start Jetty HttpClient {}", e.getMessage());
78         }
79     }
80
81     @Override
82     protected void deactivate(ComponentContext componentContext) {
83         super.deactivate(componentContext);
84         try {
85             httpClient.stop();
86         } catch (Exception e) {
87             logger.warn("Unable to stop Jetty HttpClient {}", e.getMessage());
88         }
89     }
90
91     @Override
92     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
93         return THING_TYPE_LINKY.equals(thingTypeUID);
94     }
95
96     @Override
97     protected @Nullable ThingHandler createHandler(Thing thing) {
98         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
99
100         return supportsThingType(thingTypeUID) ? new LinkyHandler(thing, localeProvider, gson, httpClient) : null;
101     }
102 }