2 * Copyright (c) 2010-2022 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.neeo.internal.handler;
15 import java.util.Hashtable;
16 import java.util.Objects;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
20 import javax.ws.rs.client.ClientBuilder;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.neeo.internal.NeeoConstants;
25 import org.openhab.binding.neeo.internal.discovery.NeeoDeviceDiscoveryService;
26 import org.openhab.binding.neeo.internal.discovery.NeeoRoomDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.net.HttpServiceUtil;
29 import org.openhab.core.net.NetworkAddressService;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerFactory;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41 import org.osgi.service.http.HttpService;
44 * The {@link NeeoHandlerFactory} is responsible for creating things and thing
47 * @author Tim Roberts - Initial contribution
50 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.neeo")
51 public class NeeoHandlerFactory extends BaseThingHandlerFactory {
53 /** The {@link HttpService} used to register callbacks */
54 private final HttpService httpService;
56 /** The {@link NetworkAddressService} used for ip lookup */
57 private final NetworkAddressService networkAddressService;
59 /** The {@link ClientBuilder} used in HttpRequest */
60 private final ClientBuilder clientBuilder;
62 /** The discovery services created by this class (one per room and one for each device) */
63 private final ConcurrentMap<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new ConcurrentHashMap<>();
66 public NeeoHandlerFactory(@Reference HttpService httpService,
67 @Reference NetworkAddressService networkAddressService, @Reference ClientBuilder clientBuilder) {
68 this.httpService = httpService;
69 this.networkAddressService = networkAddressService;
70 this.clientBuilder = clientBuilder;
74 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
75 Objects.requireNonNull(thingTypeUID, "thingTypeUID cannot be null");
77 return NeeoConstants.BINDING_ID.equals(thingTypeUID.getBindingId());
82 protected ThingHandler createHandler(Thing thing) {
83 Objects.requireNonNull(thing, "thing cannot be null");
84 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
86 if (thingTypeUID.equals(NeeoConstants.BRIDGE_TYPE_BRAIN)) {
87 final int port = HttpServiceUtil.getHttpServicePort(this.bundleContext);
88 final NeeoBrainHandler handler = new NeeoBrainHandler((Bridge) thing,
89 port < 0 ? NeeoConstants.DEFAULT_BRAIN_HTTP_PORT : port, httpService, networkAddressService,
91 registerRoomDiscoveryService(handler);
93 } else if (thingTypeUID.equals(NeeoConstants.BRIDGE_TYPE_ROOM)) {
94 final NeeoRoomHandler handler = new NeeoRoomHandler((Bridge) thing);
95 registerDeviceDiscoveryService(handler);
97 } else if (thingTypeUID.equals(NeeoConstants.THING_TYPE_DEVICE)) {
98 return new NeeoDeviceHandler(thing);
105 * Helper method to register the room discovery service
107 * @param handler a non-null brain handler
109 private void registerRoomDiscoveryService(NeeoBrainHandler handler) {
110 Objects.requireNonNull(handler, "handler cannot be null");
111 final NeeoRoomDiscoveryService discoveryService = new NeeoRoomDiscoveryService(handler);
112 this.discoveryServiceRegs.put(handler.getThing().getUID(),
113 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
117 * Helper method to register the device discovery service
119 * @param handler a non-null room handler
121 private void registerDeviceDiscoveryService(NeeoRoomHandler handler) {
122 Objects.requireNonNull(handler, "handler cannot be null");
123 final NeeoDeviceDiscoveryService discoveryService = new NeeoDeviceDiscoveryService(handler);
124 this.discoveryServiceRegs.put(handler.getThing().getUID(),
125 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
129 protected void removeHandler(ThingHandler thingHandler) {
130 final ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
131 if (serviceReg != null) {
132 serviceReg.unregister();