]> git.basschouten.com Git - openhab-addons.git/blob
0e3f1fb52649071e20770f26b0474a17b60cefa2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.evohome.internal;
14
15 import java.util.HashMap;
16 import java.util.Hashtable;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.openhab.binding.evohome.internal.discovery.EvohomeDiscoveryService;
23 import org.openhab.binding.evohome.internal.handler.EvohomeAccountBridgeHandler;
24 import org.openhab.binding.evohome.internal.handler.EvohomeHeatingZoneHandler;
25 import org.openhab.binding.evohome.internal.handler.EvohomeTemperatureControlSystemHandler;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.io.net.http.HttpClientFactory;
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.Activate;
37 import org.osgi.service.component.annotations.Component;
38 import org.osgi.service.component.annotations.Reference;
39
40 /**
41  * Provides the thing factory for this binding
42  *
43  * @author Jasper van Zuijlen - Initial contribution
44  *
45  */
46 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.evohome")
47 @NonNullByDefault
48 public class EvohomeHandlerFactory extends BaseThingHandlerFactory {
49
50     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
51
52     private final HttpClient httpClient;
53
54     @Activate
55     public EvohomeHandlerFactory(@Reference final HttpClientFactory httpClientFactory) {
56         this.httpClient = httpClientFactory.getCommonHttpClient();
57     }
58
59     @Override
60     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
61         return EvohomeBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
62     }
63
64     @Override
65     protected @Nullable ThingHandler createHandler(Thing thing) {
66         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
67         if (thingTypeUID.equals(EvohomeBindingConstants.THING_TYPE_EVOHOME_ACCOUNT)) {
68             EvohomeAccountBridgeHandler bridge = new EvohomeAccountBridgeHandler((Bridge) thing, httpClient);
69             registerEvohomeDiscoveryService(bridge);
70             return bridge;
71         } else if (thingTypeUID.equals(EvohomeBindingConstants.THING_TYPE_EVOHOME_DISPLAY)) {
72             return new EvohomeTemperatureControlSystemHandler(thing);
73         } else if (thingTypeUID.equals(EvohomeBindingConstants.THING_TYPE_EVOHOME_HEATING_ZONE)) {
74             return new EvohomeHeatingZoneHandler(thing);
75         }
76
77         return null;
78     }
79
80     private void registerEvohomeDiscoveryService(EvohomeAccountBridgeHandler evohomeBridgeHandler) {
81         EvohomeDiscoveryService discoveryService = new EvohomeDiscoveryService(evohomeBridgeHandler);
82
83         this.discoveryServiceRegs.put(evohomeBridgeHandler.getThing().getUID(),
84                 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));
85     }
86
87     @Override
88     public ThingHandler registerHandler(Thing thing) {
89         return super.registerHandler(thing);
90     }
91
92     @Override
93     protected void removeHandler(ThingHandler thingHandler) {
94         if (thingHandler instanceof EvohomeAccountBridgeHandler) {
95             ServiceRegistration<?> serviceReg = this.discoveryServiceRegs.get(thingHandler.getThing().getUID());
96             if (serviceReg != null) {
97                 EvohomeDiscoveryService service = (EvohomeDiscoveryService) bundleContext
98                         .getService(serviceReg.getReference());
99                 if (service != null) {
100                     service.deactivate();
101                 }
102                 serviceReg.unregister();
103                 discoveryServiceRegs.remove(thingHandler.getThing().getUID());
104             }
105         }
106     }
107 }