2 * Copyright (c) 2010-2021 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.time.ZonedDateTime;
18 import java.time.format.DateTimeFormatter;
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;
38 import com.google.gson.Gson;
39 import com.google.gson.GsonBuilder;
40 import com.google.gson.JsonDeserializer;
43 * The {@link LinkyHandlerFactory} is responsible for creating things handlers.
45 * @author Gaƫl L'hopital - Initial contribution
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");
52 private final Logger logger = LoggerFactory.getLogger(LinkyHandlerFactory.class);
54 private final LocaleProvider localeProvider;
55 private final Gson gson;
56 private final HttpClient httpClient;
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))
66 this.httpClient = httpClientFactory.createHttpClient(LinkyBindingConstants.BINDING_ID);
70 protected void activate(ComponentContext componentContext) {
71 super.activate(componentContext);
72 httpClient.getSslContextFactory().setExcludeCipherSuites(new String[0]);
73 httpClient.setFollowRedirects(false);
76 } catch (Exception e) {
77 logger.warn("Unable to start Jetty HttpClient {}", e.getMessage());
82 protected void deactivate(ComponentContext componentContext) {
83 super.deactivate(componentContext);
86 } catch (Exception e) {
87 logger.warn("Unable to stop Jetty HttpClient {}", e.getMessage());
92 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
93 return THING_TYPE_LINKY.equals(thingTypeUID);
97 protected @Nullable ThingHandler createHandler(Thing thing) {
98 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
100 return supportsThingType(thingTypeUID) ? new LinkyHandler(thing, localeProvider, gson, httpClient) : null;