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.surepetcare.internal;
15 import static org.openhab.binding.surepetcare.internal.SurePetcareConstants.*;
17 import java.util.Collection;
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.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;
41 * The {@link SurePetcareHandlerFactory} is responsible for creating things and thing
44 * @author Rene Scherer - Initial contribution
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.surepetcare")
49 public class SurePetcareHandlerFactory extends BaseThingHandlerFactory {
51 private final Logger logger = LoggerFactory.getLogger(SurePetcareHandlerFactory.class);
53 private SurePetcareAPIHelper petcareAPI = new SurePetcareAPIHelper();
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());
60 * Returns true if the factory supports the given thing type.
63 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
64 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
68 * Returns a newly created thing handler for the given thing.
71 protected @Nullable ThingHandler createHandler(Thing thing) {
72 logger.debug("createHandler - create handler for {}", thing);
73 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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);
92 protected void setHttpClientFactory(HttpClientFactory httpClientFactory) {
93 petcareAPI.setHttpClient(httpClientFactory.getCommonHttpClient());
96 protected void unsetHttpClientFactory(HttpClientFactory httpClientFactory) {
97 petcareAPI.setHttpClient(null);