]> git.basschouten.com Git - openhab-addons.git/blob
0ffaf3fb52dba9aba4ddc8acb5c578df65c8cc13
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.nest.internal.wwn.test;
14
15 import java.util.HashMap;
16 import java.util.Hashtable;
17 import java.util.Map;
18
19 import javax.ws.rs.client.ClientBuilder;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.nest.internal.wwn.discovery.WWNDiscoveryService;
24 import org.openhab.binding.nest.internal.wwn.handler.WWNAccountHandler;
25 import org.openhab.core.config.discovery.DiscoveryService;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.framework.ServiceRegistration;
34 import org.osgi.service.component.ComponentContext;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Modified;
37 import org.osgi.service.component.annotations.Reference;
38 import org.osgi.service.jaxrs.client.SseEventSourceFactory;
39
40 /**
41  * The {@link WWNTestHandlerFactory} is responsible for creating test things and thing handlers.
42  *
43  * @author Wouter Born - Initial contribution
44  */
45 @NonNullByDefault
46 public class WWNTestHandlerFactory extends BaseThingHandlerFactory implements ThingHandlerFactory {
47
48     public static final String REDIRECT_URL_CONFIG_PROPERTY = "redirect.url";
49
50     private final ClientBuilder clientBuilder;
51     private final SseEventSourceFactory eventSourceFactory;
52     private final Map<ThingUID, ServiceRegistration<?>> discoveryService = new HashMap<>();
53
54     private String redirectUrl = "http://localhost";
55
56     @Activate
57     public WWNTestHandlerFactory(@Reference ClientBuilder clientBuilder,
58             @Reference SseEventSourceFactory eventSourceFactory) {
59         this.clientBuilder = clientBuilder;
60         this.eventSourceFactory = eventSourceFactory;
61     }
62
63     @Override
64     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65         return WWNTestAccountHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID);
66     }
67
68     @Activate
69     public void activate(ComponentContext componentContext, Map<String, Object> config) {
70         super.activate(componentContext);
71         modified(config);
72     }
73
74     @Modified
75     public void modified(Map<String, Object> config) {
76         String url = (String) config.get(REDIRECT_URL_CONFIG_PROPERTY);
77         if (url != null) {
78             this.redirectUrl = url;
79         }
80     }
81
82     @Override
83     protected @Nullable ThingHandler createHandler(Thing thing) {
84         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
85         if (thingTypeUID.equals(WWNTestAccountHandler.THING_TYPE_TEST_BRIDGE)) {
86             WWNTestAccountHandler handler = new WWNTestAccountHandler((Bridge) thing, clientBuilder, eventSourceFactory,
87                     redirectUrl);
88             WWNDiscoveryService service = new WWNDiscoveryService();
89             service.setThingHandler(handler);
90             // Register the discovery service.
91             discoveryService.put(handler.getThing().getUID(),
92                     bundleContext.registerService(DiscoveryService.class.getName(), service, new Hashtable<>()));
93
94             return handler;
95         }
96         return null;
97     }
98
99     /**
100      * Removes the handler for the specific thing. This also handles disabling the discovery
101      * service when the bridge is removed.
102      */
103     @Override
104     protected void removeHandler(ThingHandler thingHandler) {
105         if (thingHandler instanceof WWNAccountHandler) {
106             ServiceRegistration<?> registration = discoveryService.get(thingHandler.getThing().getUID());
107             if (registration != null) {
108                 // Unregister the discovery service.
109                 WWNDiscoveryService service = (WWNDiscoveryService) bundleContext
110                         .getService(registration.getReference());
111                 service.deactivate();
112                 registration.unregister();
113                 discoveryService.remove(thingHandler.getThing().getUID());
114             }
115         }
116         super.removeHandler(thingHandler);
117     }
118 }