]> git.basschouten.com Git - openhab-addons.git/blob
9e85981c8f7300ab24c5a46fd978b0f3b5fa6756
[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.heos.internal;
14
15 import static org.openhab.binding.heos.internal.HeosBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.heos.internal.api.HeosAudioSink;
25 import org.openhab.binding.heos.internal.discovery.HeosPlayerDiscovery;
26 import org.openhab.binding.heos.internal.handler.HeosBridgeHandler;
27 import org.openhab.binding.heos.internal.handler.HeosDynamicStateDescriptionProvider;
28 import org.openhab.binding.heos.internal.handler.HeosGroupHandler;
29 import org.openhab.binding.heos.internal.handler.HeosPlayerHandler;
30 import org.openhab.binding.heos.internal.handler.HeosThingBaseHandler;
31 import org.openhab.core.audio.AudioHTTPServer;
32 import org.openhab.core.audio.AudioSink;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.net.HttpServiceUtil;
35 import org.openhab.core.net.NetworkAddressService;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.Thing;
38 import org.openhab.core.thing.ThingTypeUID;
39 import org.openhab.core.thing.ThingUID;
40 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.thing.binding.ThingHandlerFactory;
43 import org.osgi.framework.ServiceRegistration;
44 import org.osgi.service.component.ComponentContext;
45 import org.osgi.service.component.annotations.Activate;
46 import org.osgi.service.component.annotations.Component;
47 import org.osgi.service.component.annotations.Reference;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * The {@link HeosHandlerFactory} is responsible for creating things and thing
53  * handlers.
54  *
55  * @author Johannes Einig - Initial contribution
56  */
57 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.heos")
58 @NonNullByDefault
59 public class HeosHandlerFactory extends BaseThingHandlerFactory {
60     private final Logger logger = LoggerFactory.getLogger(HeosHandlerFactory.class);
61
62     private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
63     private final Map<String, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
64
65     private @NonNullByDefault({}) AudioHTTPServer audioHTTPServer;
66     private @NonNullByDefault({}) NetworkAddressService networkAddressService;
67     private @NonNullByDefault({}) HeosDynamicStateDescriptionProvider heosDynamicStateDescriptionProvider;
68
69     @Override
70     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
71         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
72     }
73
74     @Override
75     @Activate
76     protected void activate(ComponentContext componentContext) {
77         super.activate(componentContext);
78     }
79
80     @Override
81     protected @Nullable ThingHandler createHandler(Thing thing) {
82         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
83
84         if (THING_TYPE_BRIDGE.equals(thingTypeUID)) {
85             HeosBridgeHandler bridgeHandler = new HeosBridgeHandler((Bridge) thing,
86                     heosDynamicStateDescriptionProvider);
87             HeosPlayerDiscovery playerDiscovery = new HeosPlayerDiscovery(bridgeHandler);
88             discoveryServiceRegs.put(bridgeHandler.getThing().getUID(), bundleContext
89                     .registerService(DiscoveryService.class.getName(), playerDiscovery, new Hashtable<>()));
90             logger.debug("Register discovery service for HEOS player and HEOS groups by bridge '{}'",
91                     bridgeHandler.getThing().getUID().getId());
92             return bridgeHandler;
93         }
94         if (THING_TYPE_PLAYER.equals(thingTypeUID)) {
95             HeosPlayerHandler playerHandler = new HeosPlayerHandler(thing, heosDynamicStateDescriptionProvider);
96             registerAudioSink(thing, playerHandler);
97             return playerHandler;
98         }
99         if (THING_TYPE_GROUP.equals(thingTypeUID)) {
100             HeosGroupHandler groupHandler = new HeosGroupHandler(thing, heosDynamicStateDescriptionProvider);
101             registerAudioSink(thing, groupHandler);
102             return groupHandler;
103         }
104         return null;
105     }
106
107     private void registerAudioSink(Thing thing, HeosThingBaseHandler thingBaseHandler) {
108         HeosAudioSink audioSink = new HeosAudioSink(thingBaseHandler, audioHTTPServer, createCallbackUrl());
109         @SuppressWarnings("unchecked")
110         ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) bundleContext
111                 .registerService(AudioSink.class.getName(), audioSink, new Hashtable<>());
112         audioSinkRegistrations.put(thing.getUID().toString(), reg);
113     }
114
115     @Override
116     public void unregisterHandler(Thing thing) {
117         if (thing.getThingTypeUID().equals(THING_TYPE_BRIDGE)) {
118             super.unregisterHandler(thing);
119             ServiceRegistration<?> serviceRegistration = discoveryServiceRegs.get(thing.getUID());
120             if (serviceRegistration != null) {
121                 serviceRegistration.unregister();
122                 discoveryServiceRegs.remove(thing.getUID());
123                 logger.debug("Unregister discovery service for HEOS player and HEOS groups by bridge '{}'",
124                         thing.getUID().getId());
125             }
126         }
127         if (THING_TYPE_PLAYER.equals(thing.getThingTypeUID()) || THING_TYPE_GROUP.equals(thing.getThingTypeUID())) {
128             super.unregisterHandler(thing);
129             ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(thing.getUID().toString());
130             if (reg != null) {
131                 reg.unregister();
132             }
133         }
134     }
135
136     @Reference
137     protected void setAudioHTTPServer(AudioHTTPServer audioHTTPServer) {
138         this.audioHTTPServer = audioHTTPServer;
139     }
140
141     protected void unsetAudioHTTPServer(AudioHTTPServer audioHTTPServer) {
142         this.audioHTTPServer = null;
143     }
144
145     @Reference
146     protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
147         this.networkAddressService = networkAddressService;
148     }
149
150     protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
151         this.networkAddressService = null;
152     }
153
154     @Reference
155     protected void setDynamicStateDescriptionProvider(HeosDynamicStateDescriptionProvider provider) {
156         this.heosDynamicStateDescriptionProvider = provider;
157     }
158
159     protected void unsetDynamicStateDescriptionProvider(HeosDynamicStateDescriptionProvider provider) {
160         this.heosDynamicStateDescriptionProvider = null;
161     }
162
163     private @Nullable String createCallbackUrl() {
164         final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
165         if (ipAddress == null) {
166             logger.warn("No network interface could be found.");
167             return null;
168         }
169         // we do not use SSL as it can cause certificate validation issues.
170         final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
171         if (port == -1) {
172             logger.warn("Cannot find port of the http service.");
173             return null;
174         }
175         return "http://" + ipAddress + ":" + port;
176     }
177 }