]> git.basschouten.com Git - openhab-addons.git/blob
59c7fbaf434a0528031f4767f12d302966b77596
[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.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
34
35 import com.google.gson.Gson;
36 import com.google.gson.GsonBuilder;
37 import com.google.gson.JsonDeserializer;
38
39 /**
40  * The {@link LinkyHandlerFactory} is responsible for creating things handlers.
41  *
42  * @author GaĆ«l L'hopital - Initial contribution
43  */
44 @NonNullByDefault
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.linky")
46 public class LinkyHandlerFactory extends BaseThingHandlerFactory {
47     private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX");
48     private final LocaleProvider localeProvider;
49     private final Gson gson;
50     private final HttpClient httpClient;
51
52     @Activate
53     public LinkyHandlerFactory(final @Reference LocaleProvider localeProvider,
54             final @Reference HttpClientFactory httpClientFactory) {
55         this.localeProvider = localeProvider;
56         this.httpClient = httpClientFactory.createHttpClient(LinkyBindingConstants.BINDING_ID);
57         this.gson = new GsonBuilder().registerTypeAdapter(ZonedDateTime.class,
58                 (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
59                         .parse(json.getAsJsonPrimitive().getAsString(), formatter))
60                 .create();
61     }
62
63     @Override
64     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65         return THING_TYPE_LINKY.equals(thingTypeUID);
66     }
67
68     @Override
69     protected @Nullable ThingHandler createHandler(Thing thing) {
70         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
71
72         if (supportsThingType(thingTypeUID)) {
73             return new LinkyHandler(thing, localeProvider, gson, httpClient);
74         }
75
76         return null;
77     }
78 }