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.heos.internal;
15 import static org.openhab.binding.heos.internal.HeosBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.Hashtable;
20 import java.util.concurrent.ConcurrentHashMap;
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;
52 * The {@link HeosHandlerFactory} is responsible for creating things and thing
55 * @author Johannes Einig - Initial contribution
57 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.heos")
59 public class HeosHandlerFactory extends BaseThingHandlerFactory {
60 private final Logger logger = LoggerFactory.getLogger(HeosHandlerFactory.class);
62 private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
63 private final Map<String, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
65 private @NonNullByDefault({}) AudioHTTPServer audioHTTPServer;
66 private @NonNullByDefault({}) NetworkAddressService networkAddressService;
67 private @NonNullByDefault({}) HeosDynamicStateDescriptionProvider heosDynamicStateDescriptionProvider;
70 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
71 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76 protected void activate(ComponentContext componentContext) {
77 super.activate(componentContext);
81 protected @Nullable ThingHandler createHandler(Thing thing) {
82 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
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());
94 if (THING_TYPE_PLAYER.equals(thingTypeUID)) {
95 HeosPlayerHandler playerHandler = new HeosPlayerHandler(thing, heosDynamicStateDescriptionProvider);
96 registerAudioSink(thing, playerHandler);
99 if (THING_TYPE_GROUP.equals(thingTypeUID)) {
100 HeosGroupHandler groupHandler = new HeosGroupHandler(thing, heosDynamicStateDescriptionProvider);
101 registerAudioSink(thing, groupHandler);
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);
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());
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());
137 protected void setAudioHTTPServer(AudioHTTPServer audioHTTPServer) {
138 this.audioHTTPServer = audioHTTPServer;
141 protected void unsetAudioHTTPServer(AudioHTTPServer audioHTTPServer) {
142 this.audioHTTPServer = null;
146 protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
147 this.networkAddressService = networkAddressService;
150 protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
151 this.networkAddressService = null;
155 protected void setDynamicStateDescriptionProvider(HeosDynamicStateDescriptionProvider provider) {
156 this.heosDynamicStateDescriptionProvider = provider;
159 protected void unsetDynamicStateDescriptionProvider(HeosDynamicStateDescriptionProvider provider) {
160 this.heosDynamicStateDescriptionProvider = null;
163 private @Nullable String createCallbackUrl() {
164 final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
165 if (ipAddress == null) {
166 logger.warn("No network interface could be found.");
169 // we do not use SSL as it can cause certificate validation issues.
170 final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
172 logger.warn("Cannot find port of the http service.");
175 return "http://" + ipAddress + ":" + port;