]> git.basschouten.com Git - openhab-addons.git/blob
7418de70c32eee3ce48732a03d1233d056bbbebe
[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 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;
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     private final HttpClient httpClient;
60
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<>();
63
64     @Activate
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();
70     }
71
72     @Override
73     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
74         Objects.requireNonNull(thingTypeUID, "thingTypeUID cannot be null");
75
76         return NeeoConstants.BINDING_ID.equals(thingTypeUID.getBindingId());
77     }
78
79     @Nullable
80     @Override
81     protected ThingHandler createHandler(Thing thing) {
82         Objects.requireNonNull(thing, "thing cannot be null");
83         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
84
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,
89                     httpClient);
90             registerRoomDiscoveryService(handler);
91             return handler;
92         } else if (thingTypeUID.equals(NeeoConstants.BRIDGE_TYPE_ROOM)) {
93             final NeeoRoomHandler handler = new NeeoRoomHandler((Bridge) thing);
94             registerDeviceDiscoveryService(handler);
95             return handler;
96         } else if (thingTypeUID.equals(NeeoConstants.THING_TYPE_DEVICE)) {
97             return new NeeoDeviceHandler(thing);
98         }
99
100         return null;
101     }
102
103     /**
104      * Helper method to register the room discovery service
105      *
106      * @param handler a non-null brain handler
107      */
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<>()));
113     }
114
115     /**
116      * Helper method to register the device discovery service
117      *
118      * @param handler a non-null room handler
119      */
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<>()));
125     }
126
127     @Override
128     protected void removeHandler(ThingHandler thingHandler) {
129         final ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
130         if (serviceReg != null) {
131             serviceReg.unregister();
132         }
133     }
134 }