Skip to content

Commit

Permalink
Added support for dashboard links to external services. (openhab#185)
Browse files Browse the repository at this point in the history
* Added dashboard support for links to external services.

Signed-off-by: Pauli Anttila <pauli.anttila@gmail.com>
GitOrigin-RevId: ef8d0df
  • Loading branch information
paulianttila authored and splatch committed Jul 11, 2023
1 parent 90de577 commit dc1db54
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 2 deletions.
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 @@ -40,7 +41,7 @@
*
* @author Kai Kreuzer - Initial contribution
*/
@Component(service = DashboardService.class, immediate = true)
@Component(service = DashboardService.class, immediate = true, name = "org.openhab.dashboard")
public class DashboardService {

public static final String DASHBOARD_ALIAS = "/start";
Expand All @@ -58,8 +59,12 @@ public class DashboardService {

private BundleContext bundleContext;

private final static String LINK_NAME = "link-name";
private final static String LINK_URL = "link-url";
private final static String LINK_IMAGEURL = "link-imageurl";

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

addTilesForExternalServices(properties);
}

@Deactivate
Expand Down Expand Up @@ -175,4 +182,24 @@ 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 imageUrl = (String) properties.get(linkname + LINK_IMAGEURL);

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

addDashboardTile(new ExternalServiceTile.DashboardTileBuilder().withName(name).withUrl(url)
.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);
}
}
}

0 comments on commit dc1db54

Please sign in to comment.