2 * Copyright (c) 2010-2022 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.groheondus.internal.handler;
15 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.THING_TYPE_BRIDGE_ACCOUNT;
16 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.THING_TYPE_SENSE;
17 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.THING_TYPE_SENSEGUARD;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.Hashtable;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.groheondus.internal.discovery.GroheOndusDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.storage.StorageService;
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.FrameworkUtil;
38 import org.osgi.framework.ServiceRegistration;
39 import org.osgi.framework.wiring.BundleWiring;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Reference;
45 * @author Florian Schmidt and Arne Wohlert - Initial contribution
48 @Component(configurationPid = "binding.groheondus", service = ThingHandlerFactory.class)
49 public class GroheOndusHandlerFactory extends BaseThingHandlerFactory {
51 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
53 private StorageService storageService;
54 private int thingCounter = 0;
57 public GroheOndusHandlerFactory(@Reference StorageService storageService) {
58 this.storageService = storageService;
61 private static final Collection<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Arrays.asList(THING_TYPE_SENSEGUARD,
62 THING_TYPE_SENSE, THING_TYPE_BRIDGE_ACCOUNT);
65 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70 protected @Nullable ThingHandler createHandler(Thing thing) {
71 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
73 if (THING_TYPE_BRIDGE_ACCOUNT.equals(thingTypeUID)) {
74 GroheOndusAccountHandler handler = new GroheOndusAccountHandler((Bridge) thing,
75 storageService.getStorage(thing.getUID().toString(),
76 FrameworkUtil.getBundle(getClass()).adapt(BundleWiring.class).getClassLoader()));
77 onAccountCreated(thing, handler);
79 } else if (THING_TYPE_SENSEGUARD.equals(thingTypeUID)) {
80 return new GroheOndusSenseGuardHandler(thing, thingCounter++);
81 } else if (THING_TYPE_SENSE.equals(thingTypeUID)) {
82 return new GroheOndusSenseHandler(thing, thingCounter++);
88 private void onAccountCreated(Thing thing, GroheOndusAccountHandler handler) {
89 registerDeviceDiscoveryService(handler);
93 protected synchronized void removeHandler(ThingHandler thingHandler) {
94 if (thingHandler instanceof GroheOndusAccountHandler) {
95 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingHandler.getThing().getUID());
96 if (serviceReg != null) {
97 serviceReg.unregister();
102 private synchronized void registerDeviceDiscoveryService(GroheOndusAccountHandler handler) {
103 GroheOndusDiscoveryService discoveryService = new GroheOndusDiscoveryService(handler);
104 discoveryServiceRegs.put(handler.getThing().getUID(),
105 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));