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.*;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.Hashtable;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.groheondus.internal.AccountsServlet;
26 import org.openhab.binding.groheondus.internal.discovery.GroheOndusDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.storage.StorageService;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerFactory;
36 import org.osgi.framework.FrameworkUtil;
37 import org.osgi.framework.ServiceRegistration;
38 import org.osgi.framework.wiring.BundleWiring;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42 import org.osgi.service.http.HttpService;
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 HttpService httpService;
54 private StorageService storageService;
55 private AccountsServlet accountsServlet;
58 public GroheOndusHandlerFactory(@Reference HttpService httpService, @Reference StorageService storageService,
59 @Reference AccountsServlet accountsServlet) {
60 this.httpService = httpService;
61 this.storageService = storageService;
62 this.accountsServlet = accountsServlet;
65 private static final Collection<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Arrays.asList(THING_TYPE_SENSEGUARD,
66 THING_TYPE_SENSE, THING_TYPE_BRIDGE_ACCOUNT);
69 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
70 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
74 protected @Nullable ThingHandler createHandler(Thing thing) {
75 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
77 if (THING_TYPE_BRIDGE_ACCOUNT.equals(thingTypeUID)) {
78 GroheOndusAccountHandler handler = new GroheOndusAccountHandler((Bridge) thing, httpService,
79 storageService.getStorage(thing.getUID().toString(),
80 FrameworkUtil.getBundle(getClass()).adapt(BundleWiring.class).getClassLoader()));
81 onAccountCreated(thing, handler);
83 } else if (THING_TYPE_SENSEGUARD.equals(thingTypeUID)) {
84 return new GroheOndusSenseGuardHandler(thing);
85 } else if (THING_TYPE_SENSE.equals(thingTypeUID)) {
86 return new GroheOndusSenseHandler(thing);
92 private void onAccountCreated(Thing thing, GroheOndusAccountHandler handler) {
93 registerDeviceDiscoveryService(handler);
94 if (this.accountsServlet != null) {
95 this.accountsServlet.addAccount(thing);
100 protected synchronized void removeHandler(ThingHandler thingHandler) {
101 if (thingHandler instanceof GroheOndusAccountHandler) {
102 ServiceRegistration<?> serviceReg = discoveryServiceRegs.remove(thingHandler.getThing().getUID());
103 if (serviceReg != null) {
104 serviceReg.unregister();
109 private synchronized void registerDeviceDiscoveryService(GroheOndusAccountHandler handler) {
110 GroheOndusDiscoveryService discoveryService = new GroheOndusDiscoveryService(handler);
111 discoveryServiceRegs.put(handler.getThing().getUID(),
112 bundleContext.registerService(DiscoveryService.class.getName(), discoveryService, new Hashtable<>()));