]> git.basschouten.com Git - openhab-addons.git/blob
33b51ed42f2ef9a96b9680ebfb2d9dbdb89b6469
[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     private static final int REQUEST_BUFFER_SIZE = 8000;
52
53     private final Logger logger = LoggerFactory.getLogger(LinkyHandlerFactory.class);
54     private final Gson gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class,
55             (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
56                     .parse(json.getAsJsonPrimitive().getAsString(), LINKY_FORMATTER))
57             .create();
58     private final LocaleProvider localeProvider;
59     private final HttpClient httpClient;
60
61     @Activate
62     public LinkyHandlerFactory(final @Reference LocaleProvider localeProvider,
63             final @Reference HttpClientFactory httpClientFactory) {
64         this.localeProvider = localeProvider;
65         this.httpClient = httpClientFactory.createHttpClient(LinkyBindingConstants.BINDING_ID);
66     }
67
68     @Override
69     protected void activate(ComponentContext componentContext) {
70         super.activate(componentContext);
71         httpClient.setFollowRedirects(false);
72         httpClient.setRequestBufferSize(REQUEST_BUFFER_SIZE);
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         return supportsThingType(thing.getThingTypeUID()) ? new LinkyHandler(thing, localeProvider, gson, httpClient)
98                 : null;
99     }
100 }