]> git.basschouten.com Git - openhab-addons.git/blob
4af163ede5aadb85131eefa7efab007bdc3f593d
[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.plex.internal;
14
15 import static org.openhab.binding.plex.internal.PlexBindingConstants.*;
16
17 import java.util.Hashtable;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.plex.internal.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;
36
37 /**
38  * The {@link PlexHandlerFactory} is responsible for creating things and thing
39  * handlers.
40  *
41  * @author Brian Homeyer - Initial contribution
42  * @author Aron Beurskens - Binding development
43  */
44 @NonNullByDefault
45 @Component(configurationPid = "binding.plex", service = ThingHandlerFactory.class)
46 public class PlexHandlerFactory extends BaseThingHandlerFactory {
47     private final HttpClientFactory httpClientFactory;
48     private @Nullable ServiceRegistration<?> plexDiscoveryServiceRegistration;
49
50     @Activate
51     public PlexHandlerFactory(final @Reference HttpClientFactory httpClientFactory) {
52         this.httpClientFactory = httpClientFactory;
53     }
54
55     @Override
56     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
57         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
58     }
59
60     @Override
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);
66             return handler;
67         } else if (SUPPORTED_PLAYER_THING_TYPES_UIDS.contains(thingTypeUID)) {
68             return new PlexPlayerHandler(thing);
69         }
70         return null;
71     }
72
73     @Override
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();
80             }
81         }
82     }
83
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<>());
89         }
90     }
91 }