2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nikohomecontrol.internal;
15 import static org.openhab.binding.nikohomecontrol.internal.NikoHomeControlBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
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;
44 * The {@link NikoHomeControlHandlerFactory} is responsible for creating things and thing
47 * @author Mark Herwege - Initial Contribution
50 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.nikohomecontrol")
52 public class NikoHomeControlHandlerFactory extends BaseThingHandlerFactory {
54 private @NonNullByDefault({}) NetworkAddressService networkAddressService;
56 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
59 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
60 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID) || BRIDGE_THING_TYPES_UIDS.contains(thingTypeUID);
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);
70 handler = new NikoHomeControlBridgeHandler1((Bridge) thing);
72 registerNikoHomeControlDiscoveryService(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);
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();
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();
109 protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
110 this.networkAddressService = networkAddressService;
113 protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
114 this.networkAddressService = null;