Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tidal] Initial contribution #17819

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bom/openhab-addons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,11 @@
<artifactId>org.openhab.binding.tibber</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.tidal</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.binding.tivo</artifactId>
Expand Down
13 changes: 13 additions & 0 deletions bundles/org.openhab.binding.tidal/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This content is produced and maintained by the openHAB project.

* Project home: https://www.openhab.org

== Declared Project Licenses

This program and the accompanying materials are made available under the terms
of the Eclipse Public License 2.0 which is available at
https://www.eclipse.org/legal/epl-2.0/.

== Source Code

https://github.com/openhab/openhab-addons
293 changes: 293 additions & 0 deletions bundles/org.openhab.binding.tidal/README.md

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions bundles/org.openhab.binding.tidal/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>4.3.0-SNAPSHOT</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<version>4.3.0-SNAPSHOT</version>
<version>5.0.0-SNAPSHOT</version>

</parent>

<artifactId>org.openhab.binding.tidal</artifactId>

<name>openHAB Add-ons :: Bundles :: Tidal Binding</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.binding.tidal-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>

<feature name="openhab-binding-tidal" description="Tidal Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.binding.tidal/${project.version}</bundle>
</feature>
</features>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.tidal.internal;

import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.tidal.internal.api.model.Device;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandler;

/**
* Interface to decouple Tidal Bridge Handler implementation from other code.
*
* @author Hilbrand Bouwkamp - Initial contribution
*/
@NonNullByDefault
public interface TidalAccountHandler extends ThingHandler {

/**
* @return The {@link ThingUID} associated with this Tidal Account Handler
*/
ThingUID getUID();

/**
* @return The label of the Tidal Bridge associated with this Tidal Account Handler
*/
String getLabel();

/**
* @return The Tidal user name associated with this Tidal Account Handler
*/
String getUser();

/**
* @return Returns true if the Tidal Bridge is authorized.
*/
boolean isAuthorized();

/**
* @return List of Tidal devices associated with this Tidal Account Handler
*/
List<Device> listDevices();

/**
* @return Returns true if the device is online
*/
boolean isOnline();

/**
* Calls Tidal Api to obtain refresh and access tokens and persist data with Thing.
*
* @param redirectUrl The redirect url Tidal calls back to
* @param reqCode The unique code passed by Tidal to obtain the refresh and access tokens
* @return returns the name of the Tidal user that is authorized
*/
String authorize(String redirectUrl, String reqCode);

/**
* Returns true if the given Thing UID relates to this {@link TidalAccountHandler} instance.
*
* @param thingUID The Thing UID to check
* @return true if it relates to the given Thing UID
*/
boolean equalsThingUID(String thingUID);

/**
* Formats the Url to use to call Tidal to authorize the application.
*
* @param redirectUri The uri Tidal will redirect back to
* @return the formatted url that should be used to call Tidal Web Api with
*/
String formatAuthorizationUrl(String redirectUri);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/**
* Copyright (c) 2010-2024 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.tidal.internal;

import static org.openhab.binding.tidal.internal.TidalBindingConstants.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.tidal.internal.api.exception.TidalException;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link TidalAuthService} class to manage the servlets and bind authorization servlet to bridges.
*
* @author Andreas Stenlund - Initial contribution
* @author Hilbrand Bouwkamp - Made this the service class instead of only interface. Added templates
*/
@Component(service = TidalAuthService.class, configurationPid = "binding.tidal.authService")
@NonNullByDefault
public class TidalAuthService {

private static final String TEMPLATE_PATH = "templates/";
private static final String TEMPLATE_PLAYER = TEMPLATE_PATH + "player.html";
private static final String TEMPLATE_INDEX = TEMPLATE_PATH + "index.html";
private static final String ERROR_UKNOWN_BRIDGE = "Returned 'state' by doesn't match any Bridges. Has the bridge been removed?";

private final Logger logger = LoggerFactory.getLogger(TidalAuthService.class);

private final List<TidalAccountHandler> handlers = new ArrayList<>();

private @NonNullByDefault({}) HttpService httpService;
private @NonNullByDefault({}) BundleContext bundleContext;

@Activate
protected void activate(ComponentContext componentContext, Map<String, Object> properties) {
try {
bundleContext = componentContext.getBundleContext();
httpService.registerServlet(TIDAL_ALIAS, createServlet(), new Hashtable<>(),
httpService.createDefaultHttpContext());
httpService.registerResources(TIDAL_ALIAS + TIDAL_IMG_ALIAS, "web", null);
} catch (NamespaceException | ServletException | IOException e) {
logger.warn("Error during tidal servlet startup", e);
}
}

@Deactivate
protected void deactivate(ComponentContext componentContext) {
httpService.unregister(TIDAL_ALIAS);
httpService.unregister(TIDAL_ALIAS + TIDAL_IMG_ALIAS);
}

/**
* Creates a new {@link TidalAuthServlet}.
*
* @return the newly created servlet
* @throws IOException thrown when an HTML template could not be read
*/
private HttpServlet createServlet() throws IOException {
return new TidalAuthServlet(this, readTemplate(TEMPLATE_INDEX), readTemplate(TEMPLATE_PLAYER));
}

/**
* Reads a template from file and returns the content as String.
*
* @param templateName name of the template file to read
* @return The content of the template file
* @throws IOException thrown when an HTML template could not be read
*/
private String readTemplate(String templateName) throws IOException {
final URL index = bundleContext.getBundle().getEntry(templateName);

if (index == null) {
throw new FileNotFoundException(
String.format("Cannot find '{}' - failed to initialize Tidal servlet", templateName));
} else {
try (InputStream inputStream = index.openStream()) {
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}
}
}

/**
* Call with Tidal redirect uri returned State and Code values to get the refresh and access tokens and persist
* these values
*
* @param servletBaseURL the servlet base, which will be the Tidal redirect url
* @param state The Tidal returned state value
* @param code The Tidal returned code value
* @return returns the name of the Tidal user that is authorized
*/
public String authorize(String servletBaseURL, String state, String code) {
final TidalAccountHandler listener = getTidalAuthListener(state);

if (listener == null) {
logger.debug(
"Tidal redirected with state '{}' but no matching bridge was found. Possible bridge has been removed.",
state);
throw new TidalException(ERROR_UKNOWN_BRIDGE);
} else {
return listener.authorize(servletBaseURL, code);
}
}

/**
* @param listener Adds the given handler
*/
public void addTidalAccountHandler(TidalAccountHandler listener) {
if (!handlers.contains(listener)) {
handlers.add(listener);
}
}

/**
* @param handler Removes the given handler
*/
public void removeTidalAccountHandler(TidalAccountHandler handler) {
handlers.remove(handler);
}

/**
* @return Returns all {@link TidalAccountHandler}s.
*/
public List<TidalAccountHandler> getTidalAccountHandlers() {
return handlers;
}

/**
* Get the {@link TidalAccountHandler} that matches the given thing UID.
*
* @param thingUID UID of the thing to match the handler with
* @return the {@link TidalAccountHandler} matching the thing UID or null
*/
private @Nullable TidalAccountHandler getTidalAuthListener(String thingUID) {
final Optional<TidalAccountHandler> maybeListener = handlers.stream().filter(l -> l.equalsThingUID(thingUID))
.findFirst();
return maybeListener.isPresent() ? maybeListener.get() : null;
}

@Reference
protected void setHttpService(HttpService httpService) {
this.httpService = httpService;
}

protected void unsetHttpService(HttpService httpService) {
this.httpService = null;
}
}
Loading