]> git.basschouten.com Git - openhab-addons.git/blob
c0de792df7eb90043ffff40211f39cf162a30687
[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.foobot.internal;
14
15 import static org.openhab.binding.foobot.internal.FoobotBindingConstants.*;
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.openhab.binding.foobot.internal.handler.FoobotAccountHandler;
25 import org.openhab.binding.foobot.internal.handler.FoobotDeviceHandler;
26 import org.openhab.core.io.net.http.HttpClientFactory;
27 import org.openhab.core.thing.Bridge;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36
37 /**
38  * The {@link FoobotHandlerFactory} is responsible for creating things and thing handlers.
39  *
40  * @author Divya Chauhan - Initial contribution
41  * @author George Katsis - Add Bridge thing type
42  * @author Hilbrand Bouwkamp - Completed implementation
43  */
44 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.foobot")
45 @NonNullByDefault
46 public class FoobotHandlerFactory extends BaseThingHandlerFactory {
47
48     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Collections
49             .unmodifiableSet(Stream.of(BRIDGE_TYPE_FOOBOTACCOUNT, THING_TYPE_FOOBOT).collect(Collectors.toSet()));
50
51     public static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPE_UIDS = Set.of(THING_TYPE_FOOBOT);
52
53     private final FoobotApiConnector connector = new FoobotApiConnector();
54
55     @Activate
56     public FoobotHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
57         connector.setHttpClient(httpClientFactory.getCommonHttpClient());
58     }
59
60     @Override
61     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
62         return SUPPORTED_THING_TYPE_UIDS.contains(thingTypeUID);
63     }
64
65     @Override
66     protected @Nullable ThingHandler createHandler(Thing thing) {
67         final ThingTypeUID thingTypeUID = thing.getThingTypeUID();
68
69         if (thingTypeUID.equals(THING_TYPE_FOOBOT)) {
70             return new FoobotDeviceHandler(thing, connector);
71         } else if (thingTypeUID.equals(BRIDGE_TYPE_FOOBOTACCOUNT)) {
72             return new FoobotAccountHandler((Bridge) thing, connector);
73         }
74         return null;
75     }
76 }