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.onkyo.internal;
15 import static org.openhab.binding.onkyo.internal.OnkyoBindingConstants.SUPPORTED_THING_TYPES_UIDS;
17 import java.util.Dictionary;
18 import java.util.Hashtable;
20 import java.util.concurrent.ConcurrentHashMap;
22 import org.openhab.binding.onkyo.internal.handler.OnkyoAudioSink;
23 import org.openhab.binding.onkyo.internal.handler.OnkyoHandler;
24 import org.openhab.core.audio.AudioHTTPServer;
25 import org.openhab.core.audio.AudioSink;
26 import org.openhab.core.io.transport.upnp.UpnpIOService;
27 import org.openhab.core.net.HttpServiceUtil;
28 import org.openhab.core.net.NetworkAddressService;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerFactory;
34 import org.osgi.framework.ServiceRegistration;
35 import org.osgi.service.component.ComponentContext;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link OnkyoHandlerFactory} is responsible for creating things and thing
45 * @author Paul Frank - Initial contribution
46 * @author Stewart Cossey - added dynamic state descriptor provider functions
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.onkyo")
49 public class OnkyoHandlerFactory extends BaseThingHandlerFactory {
51 private final Logger logger = LoggerFactory.getLogger(OnkyoHandlerFactory.class);
53 private final Map<String, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
55 private UpnpIOService upnpIOService;
56 private AudioHTTPServer audioHTTPServer;
57 private NetworkAddressService networkAddressService;
58 private OnkyoStateDescriptionProvider stateDescriptionProvider;
60 // url (scheme+server+port) to use for playing notification sounds
61 private String callbackUrl;
64 protected void activate(ComponentContext componentContext) {
65 super.activate(componentContext);
66 Dictionary<String, Object> properties = componentContext.getProperties();
67 callbackUrl = (String) properties.get("callbackUrl");
71 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
76 protected ThingHandler createHandler(Thing thing) {
77 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
79 if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
80 String callbackUrl = createCallbackUrl();
81 OnkyoHandler handler = new OnkyoHandler(thing, upnpIOService, stateDescriptionProvider);
82 OnkyoAudioSink audioSink = new OnkyoAudioSink(handler, audioHTTPServer, callbackUrl);
83 @SuppressWarnings("unchecked")
84 ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) bundleContext
85 .registerService(AudioSink.class.getName(), audioSink, new Hashtable<>());
86 audioSinkRegistrations.put(thing.getUID().toString(), reg);
93 private String createCallbackUrl() {
94 if (callbackUrl != null) {
97 final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
98 if (ipAddress == null) {
99 logger.warn("No network interface could be found.");
103 // we do not use SSL as it can cause certificate validation issues.
104 final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
106 logger.warn("Cannot find port of the http service.");
110 return "http://" + ipAddress + ":" + port;
115 public void unregisterHandler(Thing thing) {
116 super.unregisterHandler(thing);
117 ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(thing.getUID().toString());
124 protected void setUpnpIOService(UpnpIOService upnpIOService) {
125 this.upnpIOService = upnpIOService;
128 protected void unsetUpnpIOService(UpnpIOService upnpIOService) {
129 this.upnpIOService = null;
133 protected void setAudioHTTPServer(AudioHTTPServer audioHTTPServer) {
134 this.audioHTTPServer = audioHTTPServer;
137 protected void unsetAudioHTTPServer(AudioHTTPServer audioHTTPServer) {
138 this.audioHTTPServer = null;
142 protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
143 this.networkAddressService = networkAddressService;
146 protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
147 this.networkAddressService = null;
151 protected void setDynamicStateDescriptionProvider(OnkyoStateDescriptionProvider provider) {
152 this.stateDescriptionProvider = provider;
155 protected void unsetDynamicStateDescriptionProvider(OnkyoStateDescriptionProvider provider) {
156 this.stateDescriptionProvider = null;