]> git.basschouten.com Git - openhab-addons.git/blob
37efc0abdb747b78f443030a19f2fd8fbe2b1dfc
[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.groheondus.internal.handler;
14
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;
18
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.Hashtable;
23 import java.util.Map;
24
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;
43
44 /**
45  * @author Florian Schmidt and Arne Wohlert - Initial contribution
46  */
47 @NonNullByDefault
48 @Component(configurationPid = "binding.groheondus", service = ThingHandlerFactory.class)
49 public class GroheOndusHandlerFactory extends BaseThingHandlerFactory {
50
51     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
52
53     private StorageService storageService;
54     private int thingCounter = 0;
55
56     @Activate
57     public GroheOndusHandlerFactory(@Reference StorageService storageService) {
58         this.storageService = storageService;
59     }
60
61     private static final Collection<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Arrays.asList(THING_TYPE_SENSEGUARD,
62             THING_TYPE_SENSE, THING_TYPE_BRIDGE_ACCOUNT);
63
64     @Override
65     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
67     }
68
69     @Override
70     protected @Nullable ThingHandler createHandler(Thing thing) {
71         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
72
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);
78             return 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++);
83         }
84
85         return null;
86     }
87
88     private void onAccountCreated(Thing thing, GroheOndusAccountHandler handler) {
89         registerDeviceDiscoveryService(handler);
90     }
91
92     @Override
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();
98             }
99         }
100     }
101
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<>()));
106     }
107 }