]> git.basschouten.com Git - openhab-addons.git/blob
39a63cc47785fc757bd2a396891eb96758ce9afa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.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;
39
40 /**
41  * The {@link NeeoHandlerFactory} is responsible for creating things and thing
42  * handlers.
43  *
44  * @author Tim Roberts - Initial contribution
45  */
46 @NonNullByDefault
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.neeo")
48 public class NeeoHandlerFactory extends BaseThingHandlerFactory {
49
50     /** The {@link HttpService} used to register callbacks */
51     @NonNullByDefault({})
52     private HttpService httpService;
53
54     /** The {@link NetworkAddressService} used for ip lookup */
55     @NonNullByDefault({})
56     private NetworkAddressService networkAddressService;
57
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<>();
60
61     /**
62      * Sets the {@link HttpService}.
63      *
64      * @param httpService the non-null {@link HttpService} to use
65      */
66     @Reference
67     protected void setHttpService(HttpService httpService) {
68         Objects.requireNonNull(httpService, "httpService cannot be null");
69         this.httpService = httpService;
70     }
71
72     /**
73      * Unsets the {@link HttpService}
74      *
75      * @param httpService the {@link HttpService} (not used in this implementation)
76      */
77     protected void unsetHttpService(HttpService httpService) {
78         this.httpService = null;
79     }
80
81     /**
82      * Sets the {@link NetworkAddressService}.
83      *
84      * @param networkAddressService the non-null {@link NetworkAddressService} to use
85      */
86     @Reference
87     protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
88         Objects.requireNonNull(networkAddressService, "networkAddressService cannot be null");
89         this.networkAddressService = networkAddressService;
90     }
91
92     /**
93      * Unsets the {@link NetworkAddressService}
94      *
95      * @param networkAddressService the {@link NetworkAddressService} (not used in this implementation)
96      */
97     protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
98         this.networkAddressService = null;
99     }
100
101     @Override
102     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
103         Objects.requireNonNull(thingTypeUID, "thingTypeUID cannot be null");
104
105         return NeeoConstants.BINDING_ID.equals(thingTypeUID.getBindingId());
106     }
107
108     @Nullable
109     @Override
110     protected ThingHandler createHandler(Thing thing) {
111         Objects.requireNonNull(thing, "thing cannot be null");
112         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
113
114         if (thingTypeUID.equals(NeeoConstants.BRIDGE_TYPE_BRAIN)) {
115             final HttpService localHttpService = httpService;
116             final NetworkAddressService localNetworkAddressService = networkAddressService;
117
118             Objects.requireNonNull(localHttpService, "HttpService cannot be null");
119             Objects.requireNonNull(localNetworkAddressService, "networkAddressService cannot be null");
120
121             final int port = HttpServiceUtil.getHttpServicePort(this.bundleContext);
122
123             final NeeoBrainHandler handler = new NeeoBrainHandler((Bridge) thing,
124                     port < 0 ? NeeoConstants.DEFAULT_BRAIN_HTTP_PORT : port, localHttpService,
125                     localNetworkAddressService);
126             registerRoomDiscoveryService(handler);
127             return handler;
128         } else if (thingTypeUID.equals(NeeoConstants.BRIDGE_TYPE_ROOM)) {
129             final NeeoRoomHandler handler = new NeeoRoomHandler((Bridge) thing);
130             registerDeviceDiscoveryService(handler);
131             return handler;
132         } else if (thingTypeUID.equals(NeeoConstants.THING_TYPE_DEVICE)) {
133             return new NeeoDeviceHandler(thing);
134         }
135
136         return null;
137     }
138
139     /**
140      * Helper method to register the room discovery service
141      *
142      * @param handler a non-null brain handler
143      */
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<>()));
149     }
150
151     /**
152      * Helper method to register the device discovery service
153      *
154      * @param handler a non-null room handler
155      */
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<>()));
161     }
162
163     @Override
164     protected void removeHandler(ThingHandler thingHandler) {
165         final ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.remove(thingHandler.getThing().getUID());
166         if (serviceReg != null) {
167             serviceReg.unregister();
168         }
169     }
170 }