]> git.basschouten.com Git - openhab-addons.git/blob
6cb6c0d25fbd2ad89f31829b9ffa2863728e7628
[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.nikohomecontrol.internal;
14
15 import static org.openhab.binding.nikohomecontrol.internal.NikoHomeControlBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.nikohomecontrol.internal.discovery.NikoHomeControlDiscoveryService;
24 import org.openhab.binding.nikohomecontrol.internal.handler.NikoHomeControlActionHandler;
25 import org.openhab.binding.nikohomecontrol.internal.handler.NikoHomeControlBridgeHandler;
26 import org.openhab.binding.nikohomecontrol.internal.handler.NikoHomeControlBridgeHandler1;
27 import org.openhab.binding.nikohomecontrol.internal.handler.NikoHomeControlBridgeHandler2;
28 import org.openhab.binding.nikohomecontrol.internal.handler.NikoHomeControlEnergyMeterHandler;
29 import org.openhab.binding.nikohomecontrol.internal.handler.NikoHomeControlThermostatHandler;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.net.NetworkAddressService;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
37 import org.openhab.core.thing.binding.ThingHandler;
38 import org.openhab.core.thing.binding.ThingHandlerFactory;
39 import org.osgi.framework.ServiceRegistration;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42
43 /**
44  * The {@link NikoHomeControlHandlerFactory} is responsible for creating things and thing
45  * handlers.
46  *
47  * @author Mark Herwege - Initial Contribution
48  */
49
50 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.nikohomecontrol")
51 @NonNullByDefault
52 public class NikoHomeControlHandlerFactory extends BaseThingHandlerFactory {
53
54     private @NonNullByDefault({}) NetworkAddressService networkAddressService;
55
56     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
57
58     @Override
59     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
60         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID);
61     }
62
63     @Override
64     protected @Nullable ThingHandler createHandler(Thing thing) {
65         if (BRIDGE_THING_TYPES_UIDS.contains(thing.getThingTypeUID())) {
66             NikoHomeControlBridgeHandler handler;
67             if (BRIDGEII_THING_TYPE.equals(thing.getThingTypeUID())) {
68                 handler = new NikoHomeControlBridgeHandler2((Bridge) thing, networkAddressService);
69             } else {
70                 handler = new NikoHomeControlBridgeHandler1((Bridge) thing);
71             }
72             registerNikoHomeControlDiscoveryService(handler);
73             return handler;
74         } else if (THING_TYPE_THERMOSTAT.equals(thing.getThingTypeUID())) {
75             return new NikoHomeControlThermostatHandler(thing);
76         } else if (THING_TYPE_ENERGYMETER.equals(thing.getThingTypeUID())) {
77             return new NikoHomeControlEnergyMeterHandler(thing);
78         } else if (ACTION_THING_TYPES_UIDS.contains(thing.getThingTypeUID())) {
79             return new NikoHomeControlActionHandler(thing);
80         }
81
82         return null;
83     }
84
85     private synchronized void registerNikoHomeControlDiscoveryService(NikoHomeControlBridgeHandler bridgeHandler) {
86         NikoHomeControlDiscoveryService nhcDiscoveryService = new NikoHomeControlDiscoveryService(bridgeHandler);
87         discoveryServiceRegs.put(bridgeHandler.getThing().getUID(), bundleContext
88                 .registerService(DiscoveryService.class.getName(), nhcDiscoveryService, new Hashtable<>()));
89         nhcDiscoveryService.activate();
90     }
91
92     @Override
93     protected synchronized void removeHandler(ThingHandler thingHandler) {
94         if (thingHandler instanceof NikoHomeControlBridgeHandler) {
95             ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingHandler.getThing().getUID());
96             if (serviceReg != null) {
97                 // remove discovery service, if bridge handler is removed
98                 NikoHomeControlDiscoveryService service = (NikoHomeControlDiscoveryService) bundleContext
99                         .getService(serviceReg.getReference());
100                 serviceReg.unregister();
101                 if (service != null) {
102                     service.deactivate();
103                 }
104             }
105         }
106     }
107
108     @Reference
109     protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
110         this.networkAddressService = networkAddressService;
111     }
112
113     protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
114         this.networkAddressService = null;
115     }
116 }