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.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.eclipse.jetty.client.HttpClient;
23 import org.openhab.binding.neeo.internal.NeeoConstants;
24 import org.openhab.binding.neeo.internal.discovery.NeeoDeviceDiscoveryService;
25 import org.openhab.binding.neeo.internal.discovery.NeeoRoomDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.io.net.http.HttpClientFactory;
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 private final HttpClient httpClient;
61 /** The discovery services created by this class (one per room and one for each device) */
62 private final ConcurrentMap<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new ConcurrentHashMap<>();
65 public NeeoHandlerFactory(@Reference HttpService httpService,
66 @Reference NetworkAddressService networkAddressService, @Reference HttpClientFactory httpClientFactory) {
67 this.httpService = httpService;
68 this.networkAddressService = networkAddressService;
69 this.httpClient = httpClientFactory.getCommonHttpClient();
73 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
74 Objects.requireNonNull(thingTypeUID, "thingTypeUID cannot be null");
76 return NeeoConstants.BINDING_ID.equals(thingTypeUID.getBindingId());
81 protected ThingHandler createHandler(Thing thing) {
82 Objects.requireNonNull(thing, "thing cannot be null");
83 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
85 if (thingTypeUID.equals(NeeoConstants.BRIDGE_TYPE_BRAIN)) {
86 final int port = HttpServiceUtil.getHttpServicePort(this.bundleContext);
87 final NeeoBrainHandler handler = new NeeoBrainHandler((Bridge) thing,
88 port < 0 ? NeeoConstants.DEFAULT_BRAIN_HTTP_PORT : port, httpService, networkAddressService,
90 registerRoomDiscoveryService(handler);
92 } else if (thingTypeUID.equals(NeeoConstants.BRIDGE_TYPE_ROOM)) {
93 final NeeoRoomHandler handler = new NeeoRoomHandler((Bridge) thing);
94 registerDeviceDiscoveryService(handler);
96 } else if (thingTypeUID.equals(NeeoConstants.THING_TYPE_DEVICE)) {
97 return new NeeoDeviceHandler(thing);
104 * Helper method to register the room discovery service
106 * @param handler a non-null brain handler
108 private void registerRoomDiscoveryService(NeeoBrainHandler handler) {
109 Objects.requireNonNull(handler, "handler cannot be null");
110 final NeeoRoomDiscoveryService discoveryService = new NeeoRoomDiscoveryService(handler);
111 this.discoveryServiceRegs.put(handler.getThing().getUID(),
112 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
116 * Helper method to register the device discovery service
118 * @param handler a non-null room handler
120 private void registerDeviceDiscoveryService(NeeoRoomHandler handler) {
121 Objects.requireNonNull(handler, "handler cannot be null");
122 final NeeoDeviceDiscoveryService discoveryService = new NeeoDeviceDiscoveryService(handler);
123 this.discoveryServiceRegs.put(handler.getThing().getUID(),
124 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
128 protected void removeHandler(ThingHandler thingHandler) {
129 final ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
130 if (serviceReg != null) {
131 serviceReg.unregister();