]> git.basschouten.com Git - openhab-addons.git/blob
6366fc1a6032c2309394721caa116c338da5222b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.*;
16
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.Hashtable;
21 import java.util.Map;
22
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;
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 HttpService httpService;
54     private StorageService storageService;
55     private AccountsServlet accountsServlet;
56
57     @Activate
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;
63     }
64
65     private static final Collection<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Arrays.asList(THING_TYPE_SENSEGUARD,
66             THING_TYPE_SENSE, THING_TYPE_BRIDGE_ACCOUNT);
67
68     @Override
69     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
70         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
71     }
72
73     @Override
74     protected @Nullable ThingHandler createHandler(Thing thing) {
75         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
76
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);
82             return 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);
87         }
88
89         return null;
90     }
91
92     private void onAccountCreated(Thing thing, GroheOndusAccountHandler handler) {
93         registerDeviceDiscoveryService(handler);
94         if (this.accountsServlet != null) {
95             this.accountsServlet.addAccount(thing);
96         }
97     }
98
99     @Override
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();
105             }
106         }
107     }
108
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<>()));
113     }
114 }