]> git.basschouten.com Git - openhab-addons.git/blob
54278af4c9273a067726e859cbe94abf6d5d75df
[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.onkyo.internal;
14
15 import static org.openhab.binding.onkyo.internal.OnkyoBindingConstants.SUPPORTED_THING_TYPES_UIDS;
16
17 import java.util.Dictionary;
18 import java.util.Hashtable;
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
21
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;
40
41 /**
42  * The {@link OnkyoHandlerFactory} is responsible for creating things and thing
43  * handlers.
44  *
45  * @author Paul Frank - Initial contribution
46  * @author Stewart Cossey - added dynamic state descriptor provider functions
47  */
48 @Component(service = ThingHandlerFactory.class, configurationPid = "binding.onkyo")
49 public class OnkyoHandlerFactory extends BaseThingHandlerFactory {
50
51     private final Logger logger = LoggerFactory.getLogger(OnkyoHandlerFactory.class);
52
53     private final Map<String, ServiceRegistration<AudioSink>> audioSinkRegistrations = new ConcurrentHashMap<>();
54
55     private UpnpIOService upnpIOService;
56     private AudioHTTPServer audioHTTPServer;
57     private NetworkAddressService networkAddressService;
58     private OnkyoStateDescriptionProvider stateDescriptionProvider;
59
60     // url (scheme+server+port) to use for playing notification sounds
61     private String callbackUrl;
62
63     @Override
64     protected void activate(ComponentContext componentContext) {
65         super.activate(componentContext);
66         Dictionary<String, Object> properties = componentContext.getProperties();
67         callbackUrl = (String) properties.get("callbackUrl");
68     }
69
70     @Override
71     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
72         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
73     }
74
75     @Override
76     protected ThingHandler createHandler(Thing thing) {
77         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
78
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);
87             return handler;
88         }
89
90         return null;
91     }
92
93     private String createCallbackUrl() {
94         if (callbackUrl != null) {
95             return callbackUrl;
96         } else {
97             final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
98             if (ipAddress == null) {
99                 logger.warn("No network interface could be found.");
100                 return null;
101             }
102
103             // we do not use SSL as it can cause certificate validation issues.
104             final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
105             if (port == -1) {
106                 logger.warn("Cannot find port of the http service.");
107                 return null;
108             }
109
110             return "http://" + ipAddress + ":" + port;
111         }
112     }
113
114     @Override
115     public void unregisterHandler(Thing thing) {
116         super.unregisterHandler(thing);
117         ServiceRegistration<AudioSink> reg = audioSinkRegistrations.get(thing.getUID().toString());
118         if (reg != null) {
119             reg.unregister();
120         }
121     }
122
123     @Reference
124     protected void setUpnpIOService(UpnpIOService upnpIOService) {
125         this.upnpIOService = upnpIOService;
126     }
127
128     protected void unsetUpnpIOService(UpnpIOService upnpIOService) {
129         this.upnpIOService = null;
130     }
131
132     @Reference
133     protected void setAudioHTTPServer(AudioHTTPServer audioHTTPServer) {
134         this.audioHTTPServer = audioHTTPServer;
135     }
136
137     protected void unsetAudioHTTPServer(AudioHTTPServer audioHTTPServer) {
138         this.audioHTTPServer = null;
139     }
140
141     @Reference
142     protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
143         this.networkAddressService = networkAddressService;
144     }
145
146     protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
147         this.networkAddressService = null;
148     }
149
150     @Reference
151     protected void setDynamicStateDescriptionProvider(OnkyoStateDescriptionProvider provider) {
152         this.stateDescriptionProvider = provider;
153     }
154
155     protected void unsetDynamicStateDescriptionProvider(OnkyoStateDescriptionProvider provider) {
156         this.stateDescriptionProvider = null;
157     }
158 }