]> git.basschouten.com Git - openhab-addons.git/blob
5e3581dfecfbdc80d8d0965e3d58fa7021d40f1a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.sncf.internal;
14
15 import static org.openhab.binding.sncf.internal.SncfBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.jetty.client.HttpClient;
20 import org.openhab.binding.sncf.internal.handler.SncfBridgeHandler;
21 import org.openhab.binding.sncf.internal.handler.StationHandler;
22 import org.openhab.core.i18n.LocationProvider;
23 import org.openhab.core.io.net.http.HttpClientFactory;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.thing.binding.ThingHandlerFactory;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.google.gson.FieldNamingPolicy;
37 import com.google.gson.Gson;
38 import com.google.gson.GsonBuilder;
39
40 /**
41  * The {@link SncfHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author GaĆ«l L'hopital - Initial contribution
45  */
46 @NonNullByDefault
47 @Component(configurationPid = "binding.sncf", service = ThingHandlerFactory.class)
48 public class SncfHandlerFactory extends BaseThingHandlerFactory {
49     private final Logger logger = LoggerFactory.getLogger(SncfHandlerFactory.class);
50     private final LocationProvider locationProvider;
51     private final HttpClient httpClient;
52     private final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
53             .create();
54
55     @Activate
56     public SncfHandlerFactory(@Reference LocationProvider locationProvider,
57             final @Reference HttpClientFactory httpClientFactory) {
58         this.locationProvider = locationProvider;
59         this.httpClient = httpClientFactory.getCommonHttpClient();
60     }
61
62     @Override
63     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
64         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
65     }
66
67     @Override
68     protected @Nullable ThingHandler createHandler(Thing thing) {
69         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
70         if (APIBRIDGE_THING_TYPE.equals(thingTypeUID)) {
71             return new SncfBridgeHandler((Bridge) thing, gson, locationProvider, httpClient);
72         } else if (STATION_THING_TYPE.equals(thingTypeUID)) {
73             return new StationHandler(thing, locationProvider);
74         }
75         logger.warn("ThingHandler not found for {}", thing.getThingTypeUID());
76         return null;
77     }
78 }