-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
lo92fr
wants to merge
7
commits into
openhab:main
Choose a base branch
from
lo92fr:tidal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[tidal] Initial contribution #17819
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
000eee6
Start a new tidal binding (using spotify binding as base code)
lo92fr a82c99f
add tindal to pom.xml
lo92fr 6779657
fix typo
lo92fr 1bb278c
Start implementing oauth process
lo92fr d08965c
spotless:apply
lo92fr 4b098f5
things code violations
lo92fr 6ed7f86
spotless:apply
lo92fr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
</parent> | ||
|
||
<artifactId>org.openhab.binding.tidal</artifactId> | ||
|
||
<name>openHAB Add-ons :: Bundles :: Tidal Binding</name> | ||
|
||
</project> |
9 changes: 9 additions & 0 deletions
9
bundles/org.openhab.binding.tidal/src/main/feature/feature.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
84 changes: 84 additions & 0 deletions
84
...b.binding.tidal/src/main/java/org/openhab/binding/tidal/internal/TidalAccountHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
180 changes: 180 additions & 0 deletions
180
...nhab.binding.tidal/src/main/java/org/openhab/binding/tidal/internal/TidalAuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.