forked from openhab/openhab-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Openuv] Providing an iconserver (openhab#15191)
* Adding an icon server to OpenUV binding --------- Signed-off-by: clinique <gael@lhopital.org> Signed-off-by: Jørgen Austvik <jaustvik@acm.org>
- Loading branch information
Showing
24 changed files
with
945 additions
and
81 deletions.
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
56 changes: 56 additions & 0 deletions
56
....openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/AlertLevel.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,56 @@ | ||
/** | ||
* Copyright (c) 2010-2023 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.openuv.internal; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.openhab.core.library.types.DecimalType; | ||
import org.openhab.core.types.State; | ||
import org.openhab.core.types.UnDefType; | ||
|
||
/** | ||
* The {@link AlertLevel} enum defines alert level in regard of the UV Index | ||
* | ||
* @author Gaël L'hopital - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
public enum AlertLevel { | ||
GREEN(DecimalType.ZERO, "3a8b2f"), | ||
YELLOW(new DecimalType(1), "f9a825"), | ||
ORANGE(new DecimalType(2), "ef6c00"), | ||
RED(new DecimalType(3), "b71c1c"), | ||
PURPLE(new DecimalType(4), "6a1b9a"), | ||
UNKNOWN(UnDefType.NULL, "b3b3b3"); | ||
|
||
public final State state; | ||
public final String color; | ||
|
||
AlertLevel(State state, String color) { | ||
this.state = state; | ||
this.color = color; | ||
} | ||
|
||
public static AlertLevel fromUVIndex(double uv) { | ||
if (uv >= 11) { | ||
return PURPLE; | ||
} else if (uv >= 8) { | ||
return RED; | ||
} else if (uv >= 6) { | ||
return ORANGE; | ||
} else if (uv >= 3) { | ||
return YELLOW; | ||
} else if (uv > 0) { | ||
return GREEN; | ||
} | ||
return UNKNOWN; | ||
} | ||
} |
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
125 changes: 125 additions & 0 deletions
125
....binding.openuv/src/main/java/org/openhab/binding/openuv/internal/OpenUVIconProvider.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,125 @@ | ||
/** | ||
* Copyright (c) 2010-2023 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.openuv.internal; | ||
|
||
import static org.openhab.binding.openuv.internal.OpenUVBindingConstants.BINDING_ID; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.i18n.TranslationProvider; | ||
import org.openhab.core.ui.icon.IconProvider; | ||
import org.openhab.core.ui.icon.IconSet; | ||
import org.openhab.core.ui.icon.IconSet.Format; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.service.component.annotations.Activate; | ||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link OpenUVIconProvider} is the class providing binding related icons. | ||
* | ||
* @author Gaël L'hopital - Initial contribution | ||
*/ | ||
@Component(service = { IconProvider.class }) | ||
@NonNullByDefault | ||
public class OpenUVIconProvider implements IconProvider { | ||
private static final String UV_ALARM = "uv-alarm"; | ||
private static final String UV_INDEX = "uv-index"; | ||
private static final String DEFAULT_LABEL = "OpenUV Icons"; | ||
private static final String DEFAULT_DESCRIPTION = "Icons illustrating UV conditions provided by OpenUV"; | ||
private static final Set<String> ICONS = Set.of("ozone", UV_INDEX, UV_ALARM); | ||
|
||
private final Logger logger = LoggerFactory.getLogger(OpenUVIconProvider.class); | ||
private final BundleContext context; | ||
private final TranslationProvider i18nProvider; | ||
|
||
@Activate | ||
public OpenUVIconProvider(final BundleContext context, final @Reference TranslationProvider i18nProvider) { | ||
this.context = context; | ||
this.i18nProvider = i18nProvider; | ||
} | ||
|
||
@Override | ||
public Set<IconSet> getIconSets() { | ||
return getIconSets(null); | ||
} | ||
|
||
@Override | ||
public Set<IconSet> getIconSets(@Nullable Locale locale) { | ||
String label = getText("label", DEFAULT_LABEL, locale); | ||
String description = getText("decription", DEFAULT_DESCRIPTION, locale); | ||
|
||
return Set.of(new IconSet(BINDING_ID, label, description, Set.of(Format.SVG))); | ||
} | ||
|
||
private String getText(String entry, String defaultValue, @Nullable Locale locale) { | ||
String text = defaultValue; | ||
if (locale != null) { | ||
text = i18nProvider.getText(context.getBundle(), "iconset." + entry, defaultValue, locale); | ||
text = text == null ? defaultValue : text; | ||
} | ||
return text; | ||
} | ||
|
||
@Override | ||
public @Nullable Integer hasIcon(String category, String iconSetId, Format format) { | ||
return ICONS.contains(category) && iconSetId.equals(BINDING_ID) && format == Format.SVG ? 0 : null; | ||
} | ||
|
||
@Override | ||
public @Nullable InputStream getIcon(String category, String iconSetId, @Nullable String state, Format format) { | ||
String iconName = category; | ||
if (UV_INDEX.equals(category) && state != null) { | ||
try { | ||
Double numeric = Double.valueOf(state); | ||
iconName = "%s-%d".formatted(category, numeric.intValue()); | ||
} catch (NumberFormatException e) { | ||
logger.debug("Unable to parse {} to a numeric value", state); | ||
} | ||
} | ||
String icon = getResource(iconName); | ||
if (UV_ALARM.equals(category) && state != null) { | ||
try { | ||
Integer ordinal = Integer.valueOf(state); | ||
AlertLevel alertLevel = ordinal < AlertLevel.values().length ? AlertLevel.values()[ordinal] | ||
: AlertLevel.UNKNOWN; | ||
icon = icon.replaceAll(AlertLevel.UNKNOWN.color, alertLevel.color); | ||
} catch (NumberFormatException e) { | ||
logger.debug("Unable to parse {} to a numeric value", state); | ||
} | ||
} | ||
return icon.isEmpty() ? null : new ByteArrayInputStream(icon.getBytes()); | ||
} | ||
|
||
private String getResource(String iconName) { | ||
String result = ""; | ||
|
||
URL iconResource = context.getBundle().getEntry("icon/%s.svg".formatted(iconName)); | ||
try (InputStream stream = iconResource.openStream()) { | ||
result = new String(stream.readAllBytes(), StandardCharsets.UTF_8); | ||
} catch (IOException e) { | ||
logger.warn("Unable to load ressource '{}' : {}", iconResource.getPath(), e.getMessage()); | ||
} | ||
return result; | ||
} | ||
} |
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
Oops, something went wrong.