]> git.basschouten.com Git - openhab-addons.git/blob
b3bedba0c2e1e816649bfebea5b33e77de6c1fb2
[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.neeo.internal.handler;
14
15 import java.util.Hashtable;
16 import java.util.Objects;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19
20 import javax.ws.rs.client.ClientBuilder;
21
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;
42
43 /**
44  * The {@link NeeoHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author Tim Roberts - Initial contribution
48  */
49 @NonNullByDefault
50 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.neeo")
51 public class NeeoHandlerFactory extends BaseThingHandlerFactory {
52
53     /** The {@link HttpService} used to register callbacks */
54     private final HttpService httpService;
55
56     /** The {@link NetworkAddressService} used for ip lookup */
57     private final NetworkAddressService networkAddressService;
58
59     /** The {@link ClientBuilder} used in HttpRequest */
60     private final ClientBuilder clientBuilder;
61
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<>();
64
65     @Activate
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;
71     }
72
73     @Override
74     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
75         Objects.requireNonNull(thingTypeUID, "thingTypeUID cannot be null");
76
77         return NeeoConstants.BINDING_ID.equals(thingTypeUID.getBindingId());
78     }
79
80     @Nullable
81     @Override
82     protected ThingHandler createHandler(Thing thing) {
83         Objects.requireNonNull(thing, "thing cannot be null");
84         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
85
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,
90                     clientBuilder);
91             registerRoomDiscoveryService(handler);
92             return handler;
93         } else if (thingTypeUID.equals(NeeoConstants.BRIDGE_TYPE_ROOM)) {
94             final NeeoRoomHandler handler = new NeeoRoomHandler((Bridge) thing);
95             registerDeviceDiscoveryService(handler);
96             return handler;
97         } else if (thingTypeUID.equals(NeeoConstants.THING_TYPE_DEVICE)) {
98             return new NeeoDeviceHandler(thing);
99         }
100
101         return null;
102     }
103
104     /**
105      * Helper method to register the room discovery service
106      *
107      * @param handler a non-null brain handler
108      */
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<>()));
114     }
115
116     /**
117      * Helper method to register the device discovery service
118      *
119      * @param handler a non-null room handler
120      */
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<>()));
126     }
127
128     @Override
129     protected void removeHandler(ThingHandler thingHandler) {
130         final ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
131         if (serviceReg != null) {
132             serviceReg.unregister();
133         }
134     }
135 }