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

Added support for dashboard links to external services. #185

Merged
merged 8 commits into from Aug 18, 2017
Expand Up @@ -9,7 +9,7 @@
http://www.eclipse.org/legal/epl-v10.html

-->
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="org.openhab.ui.dashboard">
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="org.openhab.dashboard">
<implementation class="org.openhab.ui.dashboard.internal.DashboardService"/>
<reference bind="setHttpService" cardinality="1..1" interface="org.osgi.service.http.HttpService" name="HttpService" policy="static" unbind="unsetHttpService"/>
<reference bind="addDashboardTile" cardinality="0..n" interface="org.openhab.ui.dashboard.DashboardTile" name="DashboardTile" policy="dynamic" unbind="removeDashboardTile"/>
Expand Down
34 changes: 34 additions & 0 deletions bundles/org.openhab.ui.dashboard/README.md
@@ -0,0 +1,34 @@
# Dashboard
Copy link
Member

Choose a reason for hiding this comment

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

@ThomDietrich Any suggestion where this page might best fit in on docs.openhab.org and whether we should automatically pick the page up from here, or simply copy the content over to openhab-docs?

Copy link
Member

Choose a reason for hiding this comment

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

I'll open an issue for this.


openHAB dashboard is landing page for the user where all openHAB UIs can be found. Dashboard support also links to external services. Links can be added to the dashboard by editing the ```conf/services/dashboard.cfg``` configuration file.
Copy link
Member

Choose a reason for hiding this comment

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

Could you put every sentence in a new line?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe change to "The openHAB dashboar is a landing page..."

Copy link
Member

Choose a reason for hiding this comment

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

You can use single backticks when highlighting code inline (no need to have three backticks).


## Link Configuration

| Parameter name | Type | Description |
|-----------------|---------|-----------------------------------------------------------------------------------------|
| X.link-name | String | Name which is shown in the openHAB dashboard. |
| X.link-url | String | URL to external service. |
| X.link-overlay | String | Image overlay icon. Supported values are empty (no icon), "html5", "android" or "apple" |
| X.link-imageurl | String | URL to image which is shown in the dashboard. |

Where X is link unique identifier (see examples).

## Image URL

Browser fetch image from image URL. URL can be direct http link or data URIs according to [RFC2397](https://tools.ietf.org/html/rfc2397). If data URIs are used, browser should support them as well. All five major browsers (Chrome, Firefox, IE, Opera and Safari) support data URIs . See e.g. [https://www.base64-image.de](https://www.base64-image.de) to convert images to base64 coded data.

## Example configuration file
Copy link
Member

Choose a reason for hiding this comment

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

please add an empty line after the header

```
frontail.link-name=openHAB Log Viewer
frontail.link-url=http://<server-adddress>:9001
frontail.link-overlay=
frontail.link-imageurl=../static/image.png

nodered.link-name=Node-RED
nodered.link-url=http://<server-adddress>:1880
nodered.link-overlay=
nodered.link-imageurl=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXwAAACfCAIAAA...QmCC

```

Note: **nodered** image data URL is not valid (it's shorten for the sake of clarity).
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.net.URL;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

Expand Down Expand Up @@ -50,7 +51,13 @@ public class DashboardService {

private BundleContext bundleContext;

protected void activate(ComponentContext componentContext) {
private final static String LINK_NAME = "link-name";
private final static String LINK_URL = "link-url";
private final static String LINK_OVERLAY = "link-overlay";
private final static String LINK_IMAGEURL = "link-imageurl";

protected void activate(ComponentContext componentContext, Map<String, Object> properties) {

try {
bundleContext = componentContext.getBundleContext();
Hashtable<String, String> props = new Hashtable<>();
Expand All @@ -69,6 +76,8 @@ protected void activate(ComponentContext componentContext) {
} catch (NamespaceException | ServletException e) {
logger.error("Error during dashboard startup: {}", e.getMessage());
}

addTilesForExternalServices(properties);
}

protected void deactivate(ComponentContext componentContext) {
Expand Down Expand Up @@ -153,4 +162,25 @@ protected HttpServlet createServlet() {
return new DashboardServlet(configurationAdmin, indexTemplate, entryTemplate, warnTemplate, setupTemplate,
tiles);
}

private void addTilesForExternalServices(Map<String, Object> properties) {
for (String key : properties.keySet()) {
if (key.endsWith(LINK_NAME)) {
if (key.length() > LINK_NAME.length()) {
// get prefix from link name
String linkname = key.substring(0, key.length() - LINK_NAME.length());

String name = (String) properties.get(linkname + LINK_NAME);
String url = (String) properties.get(linkname + LINK_URL);
String overlay = (String) properties.get(linkname + LINK_OVERLAY);
String imageUrl = (String) properties.get(linkname + LINK_IMAGEURL);

logger.debug("Add link: {}", name);

addDashboardTile(new ExternalServiceTile.DashboardTileBuilder().withName(name).withUrl(url)
.withOverlay(overlay).withImageUrl(imageUrl).build());
}
}
}
}
}
@@ -0,0 +1,82 @@
/**
* Copyright (c) 2010-2017 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.ui.dashboard.internal;

import org.openhab.ui.dashboard.DashboardTile;

/**
* The dashboard tile for external services.
*
* @author Pauli Anttila - Initial contribution
*/
public class ExternalServiceTile implements DashboardTile {
private String name;
private String url;
private String overlay;
private String imageUrl;

private ExternalServiceTile(DashboardTileBuilder builder) {
this.name = builder.name;
this.url = builder.url;
this.overlay = builder.overlay;
this.imageUrl = builder.imageUrl;
}

@Override
public String getName() {
return name;
}

@Override
public String getUrl() {
return url;
}

@Override
public String getOverlay() {
return overlay;
}

@Override
public String getImageUrl() {
return imageUrl;
}

public static class DashboardTileBuilder {

private String name;
private String url;
private String overlay;
private String imageUrl;

public DashboardTileBuilder withName(String name) {
this.name = name;
return this;
}

public DashboardTileBuilder withUrl(String url) {
this.url = url;
return this;
}

public DashboardTileBuilder withOverlay(String overlay) {
this.overlay = overlay;
return this;
}

public DashboardTileBuilder withImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
return this;
}

public ExternalServiceTile build() {
return new ExternalServiceTile(this);
}
}
}