]> git.basschouten.com Git - openhab-addons.git/blob
80db07ce224b18fe26469ce4589c6942940d05ce
[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.amplipi.internal;
14
15 import static org.openhab.binding.amplipi.internal.AmpliPiBindingConstants.*;
16
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.openhab.core.audio.AudioHTTPServer;
23 import org.openhab.core.io.net.http.HttpClientFactory;
24 import org.openhab.core.net.HttpServiceUtil;
25 import org.openhab.core.net.NetworkAddressService;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.binding.BaseThingHandlerFactory;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerFactory;
31 import org.osgi.service.component.annotations.Activate;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.Reference;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link AmpliPiHandlerFactory} is responsible for creating things and thing
39  * handlers.
40  *
41  * @author Kai Kreuzer - Initial contribution
42  */
43 @NonNullByDefault
44 @Component(configurationPid = "binding.amplipi", service = ThingHandlerFactory.class)
45 public class AmpliPiHandlerFactory extends BaseThingHandlerFactory {
46
47     private final Logger logger = LoggerFactory.getLogger(AmpliPiHandlerFactory.class);
48
49     private HttpClient httpClient;
50     private AudioHTTPServer audioHttpServer;
51     private final NetworkAddressService networkAddressService;
52
53     @Activate
54     public AmpliPiHandlerFactory(@Reference HttpClientFactory httpClientFactory,
55             @Reference AudioHTTPServer audioHttpServer, @Reference NetworkAddressService networkAddressService) {
56         this.httpClient = httpClientFactory.getCommonHttpClient();
57         this.audioHttpServer = audioHttpServer;
58         this.networkAddressService = networkAddressService;
59     }
60
61     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_CONTROLLER, THING_TYPE_ZONE,
62             THING_TYPE_GROUP);
63
64     @Override
65     public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66         return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
67     }
68
69     @Override
70     protected @Nullable ThingHandler createHandler(Thing thing) {
71         ThingTypeUID thingTypeUID = thing.getThingTypeUID();
72
73         if (THING_TYPE_CONTROLLER.equals(thingTypeUID)) {
74             String callbackUrl = createCallbackUrl();
75             return new AmpliPiHandler(thing, httpClient, audioHttpServer, callbackUrl);
76         }
77         if (THING_TYPE_ZONE.equals(thingTypeUID)) {
78             return new AmpliPiZoneHandler(thing, httpClient);
79         }
80         if (THING_TYPE_GROUP.equals(thingTypeUID)) {
81             return new AmpliPiGroupHandler(thing, httpClient);
82         }
83
84         return null;
85     }
86
87     private @Nullable String createCallbackUrl() {
88         final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
89         if (ipAddress == null) {
90             logger.warn("No network interface could be found.");
91             return null;
92         }
93
94         // we do not use SSL as it can cause certificate validation issues.
95         final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
96         if (port == -1) {
97             logger.warn("Cannot find port of the http service.");
98             return null;
99         }
100
101         return "http://" + ipAddress + ":" + port;
102     }
103 }