2 * Copyright (c) 2010-2023 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.sncf.internal;
15 import static org.openhab.binding.sncf.internal.SncfBindingConstants.*;
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;
36 import com.google.gson.FieldNamingPolicy;
37 import com.google.gson.Gson;
38 import com.google.gson.GsonBuilder;
41 * The {@link SncfHandlerFactory} is responsible for creating things and thing
44 * @author Gaƫl L'hopital - Initial contribution
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)
56 public SncfHandlerFactory(@Reference LocationProvider locationProvider,
57 final @Reference HttpClientFactory httpClientFactory) {
58 this.locationProvider = locationProvider;
59 this.httpClient = httpClientFactory.getCommonHttpClient();
63 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
64 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
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);
75 logger.warn("ThingHandler not found for {}", thing.getThingTypeUID());