]> git.basschouten.com Git - openhab-addons.git/blob
a519373a211cc0ebdb8fe15846ce7e53249af87c
[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.surepetcare.internal;
14
15 import static org.openhab.binding.surepetcare.internal.SurePetcareConstants.*;
16
17 import java.util.Collection;
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.surepetcare.internal.handler.SurePetcareBridgeHandler;
25 import org.openhab.binding.surepetcare.internal.handler.SurePetcareDeviceHandler;
26 import org.openhab.binding.surepetcare.internal.handler.SurePetcareHouseholdHandler;
27 import org.openhab.binding.surepetcare.internal.handler.SurePetcarePetHandler;
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.Component;
36 import org.osgi.service.component.annotations.Reference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * The {@link SurePetcareHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author Rene Scherer - Initial contribution
45  *
46  */
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.surepetcare")
48 @NonNullByDefault
49 public class SurePetcareHandlerFactory extends BaseThingHandlerFactory {
50
51     private final Logger logger = LoggerFactory.getLogger(SurePetcareHandlerFactory.class);
52
53     private SurePetcareAPIHelper petcareAPI = new SurePetcareAPIHelper();
54
55     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Stream
56             .of(BRIDGE_THING_TYPES_UIDS, SurePetcareConstants.SUPPORTED_THING_TYPES_UIDS).flatMap(Collection::stream)
57             .collect(Collectors.toSet());
58
59     /**
60      * Returns true if the factory supports the given thing type.
61      */
62     @Override
63     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
64         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
65     }
66
67     /**
68      * Returns a newly created thing handler for the given thing.
69      */
70     @Override
71     protected @Nullable ThingHandler createHandler(Thing thing) {
72         logger.debug("createHandler - create handler for {}", thing);
73         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
74
75         if (thingTypeUID.equals(THING_TYPE_HOUSEHOLD)) {
76             return new SurePetcareHouseholdHandler(thing, petcareAPI);
77         } else if (thingTypeUID.equals(THING_TYPE_HUB_DEVICE)) {
78             return new SurePetcareDeviceHandler(thing, petcareAPI);
79         } else if (thingTypeUID.equals(THING_TYPE_FLAP_DEVICE)) {
80             return new SurePetcareDeviceHandler(thing, petcareAPI);
81         } else if (thingTypeUID.equals(THING_TYPE_FEEDER_DEVICE)) {
82             return new SurePetcareDeviceHandler(thing, petcareAPI);
83         } else if (thingTypeUID.equals(THING_TYPE_PET)) {
84             return new SurePetcarePetHandler(thing, petcareAPI);
85         } else if (thingTypeUID.equals(THING_TYPE_BRIDGE)) {
86             return new SurePetcareBridgeHandler((Bridge) thing, petcareAPI);
87         }
88         return null;
89     }
90
91     @Reference
92     protected void setHttpClientFactory(HttpClientFactory httpClientFactory) {
93         petcareAPI.setHttpClient(httpClientFactory.getCommonHttpClient());
94     }
95
96     protected void unsetHttpClientFactory(HttpClientFactory httpClientFactory) {
97         petcareAPI.setHttpClient(null);
98     }
99 }