]> git.basschouten.com Git - openhab-addons.git/blob
7b2731642fd3f2b2d9819f3a1b001cb41f66676f
[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.onebusaway.internal;
14
15 import static org.openhab.binding.onebusaway.internal.OneBusAwayBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
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;
38
39 /**
40  * The {@link OneBusAwayHandlerFactory} is responsible for creating things and thing
41  * handlers.
42  *
43  * @author Shawn Wilsher - Initial contribution
44  */
45 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.onebusaway")
46 @NonNullByDefault
47 public class OneBusAwayHandlerFactory extends BaseThingHandlerFactory {
48
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()));
52
53     private final HttpClient httpClient;
54
55     @Activate
56     public OneBusAwayHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
57         this.httpClient = httpClientFactory.getCommonHttpClient();
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
66     protected @Nullable ThingHandler createHandler(Thing thing) {
67         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
68
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);
75         }
76
77         return null;
78     }
79 }