2 * Copyright (c) 2010-2020 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 org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.neeo.internal.NeeoConstants;
23 import org.openhab.binding.neeo.internal.discovery.NeeoDeviceDiscoveryService;
24 import org.openhab.binding.neeo.internal.discovery.NeeoRoomDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryService;
26 import org.openhab.core.net.HttpServiceUtil;
27 import org.openhab.core.net.NetworkAddressService;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
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.framework.ServiceRegistration;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38 import org.osgi.service.http.HttpService;
41 * The {@link NeeoHandlerFactory} is responsible for creating things and thing
44 * @author Tim Roberts - Initial contribution
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.neeo")
48 public class NeeoHandlerFactory extends BaseThingHandlerFactory {
50 /** The {@link HttpService} used to register callbacks */
52 private HttpService httpService;
54 /** The {@link NetworkAddressService} used for ip lookup */
56 private NetworkAddressService networkAddressService;
58 /** The discovery services created by this class (one per room and one for each device) */
59 private final ConcurrentMap<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new ConcurrentHashMap<>();
62 * Sets the {@link HttpService}.
64 * @param httpService the non-null {@link HttpService} to use
67 protected void setHttpService(HttpService httpService) {
68 Objects.requireNonNull(httpService, "httpService cannot be null");
69 this.httpService = httpService;
73 * Unsets the {@link HttpService}
75 * @param httpService the {@link HttpService} (not used in this implementation)
77 protected void unsetHttpService(HttpService httpService) {
78 this.httpService = null;
82 * Sets the {@link NetworkAddressService}.
84 * @param networkAddressService the non-null {@link NetworkAddressService} to use
87 protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
88 Objects.requireNonNull(networkAddressService, "networkAddressService cannot be null");
89 this.networkAddressService = networkAddressService;
93 * Unsets the {@link NetworkAddressService}
95 * @param networkAddressService the {@link NetworkAddressService} (not used in this implementation)
97 protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
98 this.networkAddressService = null;
102 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
103 Objects.requireNonNull(thingTypeUID, "thingTypeUID cannot be null");
105 return NeeoConstants.BINDING_ID.equals(thingTypeUID.getBindingId());
110 protected ThingHandler createHandler(Thing thing) {
111 Objects.requireNonNull(thing, "thing cannot be null");
112 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
114 if (thingTypeUID.equals(NeeoConstants.BRIDGE_TYPE_BRAIN)) {
115 final HttpService localHttpService = httpService;
116 final NetworkAddressService localNetworkAddressService = networkAddressService;
118 Objects.requireNonNull(localHttpService, "HttpService cannot be null");
119 Objects.requireNonNull(localNetworkAddressService, "networkAddressService cannot be null");
121 final int port = HttpServiceUtil.getHttpServicePort(this.bundleContext);
123 final NeeoBrainHandler handler = new NeeoBrainHandler((Bridge) thing,
124 port < 0 ? NeeoConstants.DEFAULT_BRAIN_HTTP_PORT : port, localHttpService,
125 localNetworkAddressService);
126 registerRoomDiscoveryService(handler);
128 } else if (thingTypeUID.equals(NeeoConstants.BRIDGE_TYPE_ROOM)) {
129 final NeeoRoomHandler handler = new NeeoRoomHandler((Bridge) thing);
130 registerDeviceDiscoveryService(handler);
132 } else if (thingTypeUID.equals(NeeoConstants.THING_TYPE_DEVICE)) {
133 return new NeeoDeviceHandler(thing);
140 * Helper method to register the room discovery service
142 * @param handler a non-null brain handler
144 private void registerRoomDiscoveryService(NeeoBrainHandler handler) {
145 Objects.requireNonNull(handler, "handler cannot be null");
146 final NeeoRoomDiscoveryService discoveryService = new NeeoRoomDiscoveryService(handler);
147 this.discoveryServiceRegs.put(handler.getThing().getUID(),
148 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
152 * Helper method to register the device discovery service
154 * @param handler a non-null room handler
156 private void registerDeviceDiscoveryService(NeeoRoomHandler handler) {
157 Objects.requireNonNull(handler, "handler cannot be null");
158 final NeeoDeviceDiscoveryService discoveryService = new NeeoDeviceDiscoveryService(handler);
159 this.discoveryServiceRegs.put(handler.getThing().getUID(),
160 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
164 protected void removeHandler(ThingHandler thingHandler) {
165 final ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
166 if (serviceReg != null) {
167 serviceReg.unregister();