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.onebusaway.internal;
15 import static org.openhab.binding.onebusaway.internal.OneBusAwayBindingConstants.*;
17 import java.util.Collections;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.binding.onebusaway.internal.handler.ApiHandler;
26 import org.openhab.binding.onebusaway.internal.handler.RouteHandler;
27 import org.openhab.binding.onebusaway.internal.handler.StopHandler;
28 import org.openhab.core.io.net.http.HttpClientFactory;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerFactory;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
40 * The {@link OneBusAwayHandlerFactory} is responsible for creating things and thing
43 * @author Shawn Wilsher - Initial contribution
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.onebusaway")
47 public class OneBusAwayHandlerFactory extends BaseThingHandlerFactory {
49 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.unmodifiableSet(Stream
50 .of(ApiHandler.SUPPORTED_THING_TYPE, RouteHandler.SUPPORTED_THING_TYPE, StopHandler.SUPPORTED_THING_TYPE)
51 .collect(Collectors.toSet()));
53 private final HttpClient httpClient;
56 public OneBusAwayHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
57 this.httpClient = httpClientFactory.getCommonHttpClient();
61 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
66 protected @Nullable ThingHandler createHandler(Thing thing) {
67 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
69 if (thingTypeUID.equals(THING_TYPE_API)) {
70 return new ApiHandler((Bridge) thing);
71 } else if (thingTypeUID.equals(THING_TYPE_ROUTE)) {
72 return new RouteHandler(thing);
73 } else if (thingTypeUID.equals(THING_TYPE_STOP)) {
74 return new StopHandler((Bridge) thing, httpClient);