2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.openuv.internal;
15 import static org.openhab.binding.openuv.internal.OpenUVBindingConstants.BINDING_ID;
17 import java.io.ByteArrayInputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Locale;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.core.i18n.TranslationProvider;
28 import org.openhab.core.ui.icon.IconProvider;
29 import org.openhab.core.ui.icon.IconSet;
30 import org.openhab.core.ui.icon.IconSet.Format;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.service.component.annotations.Activate;
33 import org.osgi.service.component.annotations.Component;
34 import org.osgi.service.component.annotations.Reference;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link OpenUVIconProvider} is the class providing binding related icons.
41 * @author Gaƫl L'hopital - Initial contribution
43 @Component(service = { IconProvider.class })
45 public class OpenUVIconProvider implements IconProvider {
46 private static final String UV_ALARM = "uv-alarm";
47 private static final String UV_INDEX = "uv-index";
48 private static final String DEFAULT_LABEL = "OpenUV Icons";
49 private static final String DEFAULT_DESCRIPTION = "Icons illustrating UV conditions provided by OpenUV";
50 private static final Set<String> ICONS = Set.of("ozone", UV_INDEX, UV_ALARM);
52 private final Logger logger = LoggerFactory.getLogger(OpenUVIconProvider.class);
53 private final BundleContext context;
54 private final TranslationProvider i18nProvider;
57 public OpenUVIconProvider(final BundleContext context, final @Reference TranslationProvider i18nProvider) {
58 this.context = context;
59 this.i18nProvider = i18nProvider;
63 public Set<IconSet> getIconSets() {
64 return getIconSets(null);
68 public Set<IconSet> getIconSets(@Nullable Locale locale) {
69 String label = getText("label", DEFAULT_LABEL, locale);
70 String description = getText("decription", DEFAULT_DESCRIPTION, locale);
72 return Set.of(new IconSet(BINDING_ID, label, description, Set.of(Format.SVG)));
75 private String getText(String entry, String defaultValue, @Nullable Locale locale) {
76 String text = defaultValue;
78 text = i18nProvider.getText(context.getBundle(), "iconset." + entry, defaultValue, locale);
79 text = text == null ? defaultValue : text;
85 public @Nullable Integer hasIcon(String category, String iconSetId, Format format) {
86 return ICONS.contains(category) && iconSetId.equals(BINDING_ID) && format == Format.SVG ? 0 : null;
90 public @Nullable InputStream getIcon(String category, String iconSetId, @Nullable String state, Format format) {
91 String iconName = category;
92 if (UV_INDEX.equals(category) && state != null) {
94 Double numeric = Double.valueOf(state);
95 iconName = "%s-%d".formatted(category, numeric.intValue());
96 } catch (NumberFormatException e) {
97 logger.debug("Unable to parse {} to a numeric value", state);
100 String icon = getResource(iconName);
101 if (UV_ALARM.equals(category) && state != null) {
103 Integer ordinal = Integer.valueOf(state);
104 AlertLevel alertLevel = ordinal < AlertLevel.values().length ? AlertLevel.values()[ordinal]
105 : AlertLevel.UNKNOWN;
106 icon = icon.replaceAll(AlertLevel.UNKNOWN.color, alertLevel.color);
107 } catch (NumberFormatException e) {
108 logger.debug("Unable to parse {} to a numeric value", state);
111 return icon.isEmpty() ? null : new ByteArrayInputStream(icon.getBytes());
114 private String getResource(String iconName) {
117 URL iconResource = context.getBundle().getEntry("icon/%s.svg".formatted(iconName));
118 if (iconResource != null) {
119 try (InputStream stream = iconResource.openStream()) {
120 result = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
121 } catch (IOException e) {
122 logger.warn("Unable to load resource '{}': {}", iconResource.getPath(), e.getMessage());
125 logger.warn("Unable to load icon named '{}'", iconName);