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.plex.internal;
15 import static org.openhab.binding.plex.internal.PlexBindingConstants.*;
17 import java.util.Hashtable;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.plex.discovery.PlexDiscoveryService;
22 import org.openhab.binding.plex.internal.handler.PlexPlayerHandler;
23 import org.openhab.binding.plex.internal.handler.PlexServerHandler;
24 import org.openhab.core.config.discovery.DiscoveryService;
25 import org.openhab.core.io.net.http.HttpClientFactory;
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.binding.BaseThingHandlerFactory;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerFactory;
32 import org.osgi.framework.ServiceRegistration;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
38 * The {@link PlexHandlerFactory} is responsible for creating things and thing
41 * @author Brian Homeyer - Initial contribution
42 * @author Aron Beurskens - Binding development
45 @Component(configurationPid = "binding.plex", service = ThingHandlerFactory.class)
46 public class PlexHandlerFactory extends BaseThingHandlerFactory {
47 private final HttpClientFactory httpClientFactory;
48 private @Nullable ServiceRegistration<?> plexDiscoveryServiceRegistration;
51 public PlexHandlerFactory(final @Reference HttpClientFactory httpClientFactory) {
52 this.httpClientFactory = httpClientFactory;
56 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
57 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
61 protected @Nullable ThingHandler createHandler(Thing thing) {
62 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
63 if (SUPPORTED_SERVER_THING_TYPES_UIDS.contains(thingTypeUID)) {
64 PlexServerHandler handler = new PlexServerHandler((Bridge) thing, httpClientFactory);
65 registerPlexDiscoveryService(handler);
67 } else if (SUPPORTED_PLAYER_THING_TYPES_UIDS.contains(thingTypeUID)) {
68 return new PlexPlayerHandler(thing);
74 protected synchronized void removeHandler(ThingHandler thingHandler) {
75 if (thingHandler instanceof PlexServerHandler) {
76 ServiceRegistration<?> plexDiscoveryServiceRegistration = this.plexDiscoveryServiceRegistration;
77 if (plexDiscoveryServiceRegistration != null) {
78 // remove discovery service, if bridge handler is removed
79 plexDiscoveryServiceRegistration.unregister();
84 private void registerPlexDiscoveryService(PlexServerHandler handler) {
85 PlexDiscoveryService discoveryService = new PlexDiscoveryService(handler);
86 if (bundleContext != null) {
87 this.plexDiscoveryServiceRegistration = bundleContext.registerService(DiscoveryService.class.getName(),
88 discoveryService, new Hashtable<>());