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