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.OnkyoHandler;
23 import org.openhab.core.audio.AudioHTTPServer;
24 import org.openhab.core.audio.AudioSink;
25 import org.openhab.core.io.transport.upnp.UpnpIOService;
26 import org.openhab.core.net.HttpServiceUtil;
27 import org.openhab.core.net.NetworkAddressService;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerFactory;
33 import org.osgi.framework.ServiceRegistration;
34 import org.osgi.service.component.ComponentContext;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link OnkyoHandlerFactory} is responsible for creating things and thing
44 * @author Paul Frank - Initial contribution
45 * @author Stewart Cossey - added dynamic state descriptor provider functions
47 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.onkyo")
48 public class OnkyoHandlerFactory extends BaseThingHandlerFactory {
50 private final Logger logger = LoggerFactory.getLogger(OnkyoHandlerFactory.class);
52 private final Map<String, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
54 private UpnpIOService upnpIOService;
55 private AudioHTTPServer audioHTTPServer;
56 private NetworkAddressService networkAddressService;
57 private OnkyoStateDescriptionProvider stateDescriptionProvider;
59 // url (scheme+server+port) to use for playing notification sounds
60 private String callbackUrl;
63 protected void activate(ComponentContext componentContext) {
64 super.activate(componentContext);
65 Dictionary<String, Object> properties = componentContext.getProperties();
66 callbackUrl = (String) properties.get("callbackUrl");
70 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
71 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
75 protected ThingHandler createHandler(Thing thing) {
76 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
78 if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
79 String callbackUrl = createCallbackUrl();
80 OnkyoHandler handler = new OnkyoHandler(thing, upnpIOService, audioHTTPServer, callbackUrl,
81 stateDescriptionProvider);
82 if (callbackUrl != null) {
83 @SuppressWarnings("unchecked")
84 ServiceRegistration<AudioSink> reg = (ServiceRegistration<AudioSink>) bundleContext
85 .registerService(AudioSink.class.getName(), handler, new Hashtable<>());
86 audioSinkRegistrations.put(thing.getUID().toString(), reg);
94 private String createCallbackUrl() {
95 if (callbackUrl != null) {
98 final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
99 if (ipAddress == null) {
100 logger.warn("No network interface could be found.");
104 // we do not use SSL as it can cause certificate validation issues.
105 final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
107 logger.warn("Cannot find port of the http service.");
111 return "http://" + ipAddress + ":" + port;
116 public void unregisterHandler(Thing thing) {
117 super.unregisterHandler(thing);
118 ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(thing.getUID().toString());
125 protected void setUpnpIOService(UpnpIOService upnpIOService) {
126 this.upnpIOService = upnpIOService;
129 protected void unsetUpnpIOService(UpnpIOService upnpIOService) {
130 this.upnpIOService = null;
134 protected void setAudioHTTPServer(AudioHTTPServer audioHTTPServer) {
135 this.audioHTTPServer = audioHTTPServer;
138 protected void unsetAudioHTTPServer(AudioHTTPServer audioHTTPServer) {
139 this.audioHTTPServer = null;
143 protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
144 this.networkAddressService = networkAddressService;
147 protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
148 this.networkAddressService = null;
152 protected void setDynamicStateDescriptionProvider(OnkyoStateDescriptionProvider provider) {
153 this.stateDescriptionProvider = provider;
156 protected void unsetDynamicStateDescriptionProvider(OnkyoStateDescriptionProvider provider) {
157 this.stateDescriptionProvider = null;