2 * Copyright (c) 2010-2023 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.nest.internal.wwn.test;
15 import java.util.HashMap;
16 import java.util.Hashtable;
19 import javax.ws.rs.client.ClientBuilder;
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;
41 * The {@link WWNTestHandlerFactory} is responsible for creating test things and thing handlers.
43 * @author Wouter Born - Initial contribution
46 public class WWNTestHandlerFactory extends BaseThingHandlerFactory implements ThingHandlerFactory {
48 public static final String REDIRECT_URL_CONFIG_PROPERTY = "redirect.url";
50 private final ClientBuilder clientBuilder;
51 private final SseEventSourceFactory eventSourceFactory;
52 private final Map<ThingUID, ServiceRegistration<?>> discoveryService = new HashMap<>();
54 private String redirectUrl = "http://localhost";
57 public WWNTestHandlerFactory(@Reference ClientBuilder clientBuilder,
58 @Reference SseEventSourceFactory eventSourceFactory) {
59 this.clientBuilder = clientBuilder;
60 this.eventSourceFactory = eventSourceFactory;
64 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
65 return WWNTestAccountHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID);
69 public void activate(ComponentContext componentContext, Map<String, Object> config) {
70 super.activate(componentContext);
75 public void modified(Map<String, Object> config) {
76 String url = (String) config.get(REDIRECT_URL_CONFIG_PROPERTY);
78 this.redirectUrl = url;
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,
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<>()));
100 * Removes the handler for the specific thing. This also handles disabling the discovery
101 * service when the bridge is removed.
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());
116 super.removeHandler(thingHandler);