2 * Copyright (c) 2010-2022 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.amplipi.internal;
15 import static org.openhab.binding.amplipi.internal.AmpliPiBindingConstants.*;
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;
38 * The {@link AmpliPiHandlerFactory} is responsible for creating things and thing
41 * @author Kai Kreuzer - Initial contribution
44 @Component(configurationPid = "binding.amplipi", service = ThingHandlerFactory.class)
45 public class AmpliPiHandlerFactory extends BaseThingHandlerFactory {
47 private final Logger logger = LoggerFactory.getLogger(AmpliPiHandlerFactory.class);
49 private HttpClient httpClient;
50 private AudioHTTPServer audioHttpServer;
51 private final NetworkAddressService networkAddressService;
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;
61 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_CONTROLLER, THING_TYPE_ZONE,
65 public boolean supportsThingType(ThingTypeUID thingTypeUID) {
66 return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
70 protected @Nullable ThingHandler createHandler(Thing thing) {
71 ThingTypeUID thingTypeUID = thing.getThingTypeUID();
73 if (THING_TYPE_CONTROLLER.equals(thingTypeUID)) {
74 String callbackUrl = createCallbackUrl();
75 return new AmpliPiHandler(thing, httpClient, audioHttpServer, callbackUrl);
77 if (THING_TYPE_ZONE.equals(thingTypeUID)) {
78 return new AmpliPiZoneHandler(thing, httpClient);
80 if (THING_TYPE_GROUP.equals(thingTypeUID)) {
81 return new AmpliPiGroupHandler(thing, httpClient);
87 private @Nullable String createCallbackUrl() {
88 final String ipAddress = networkAddressService.getPrimaryIpv4HostAddress();
89 if (ipAddress == null) {
90 logger.warn("No network interface could be found.");
94 // we do not use SSL as it can cause certificate validation issues.
95 final int port = HttpServiceUtil.getHttpServicePort(bundleContext);
97 logger.warn("Cannot find port of the http service.");
101 return "http://" + ipAddress + ":" + port;