]> git.basschouten.com Git - openhab-addons.git/blob
cb74f03541691dddb1270d2db92d0fcb4d83a580
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.openuv.internal;
14
15 import static org.openhab.binding.openuv.internal.OpenUVBindingConstants.BINDING_ID;
16
17 import java.io.ByteArrayInputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.URL;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Locale;
23 import java.util.Set;
24
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;
37
38 /**
39  * The {@link OpenUVIconProvider} is the class providing binding related icons.
40  *
41  * @author GaĆ«l L'hopital - Initial contribution
42  */
43 @Component(service = { IconProvider.class })
44 @NonNullByDefault
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);
51
52     private final Logger logger = LoggerFactory.getLogger(OpenUVIconProvider.class);
53     private final BundleContext context;
54     private final TranslationProvider i18nProvider;
55
56     @Activate
57     public OpenUVIconProvider(final BundleContext context, final @Reference TranslationProvider i18nProvider) {
58         this.context = context;
59         this.i18nProvider = i18nProvider;
60     }
61
62     @Override
63     public Set<IconSet> getIconSets() {
64         return getIconSets(null);
65     }
66
67     @Override
68     public Set<IconSet> getIconSets(@Nullable Locale locale) {
69         String label = getText("label", DEFAULT_LABEL, locale);
70         String description = getText("decription", DEFAULT_DESCRIPTION, locale);
71
72         return Set.of(new IconSet(BINDING_ID, label, description, Set.of(Format.SVG)));
73     }
74
75     private String getText(String entry, String defaultValue, @Nullable Locale locale) {
76         String text = defaultValue;
77         if (locale != null) {
78             text = i18nProvider.getText(context.getBundle(), "iconset." + entry, defaultValue, locale);
79             text = text == null ? defaultValue : text;
80         }
81         return text;
82     }
83
84     @Override
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;
87     }
88
89     @Override
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) {
93             try {
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);
98             }
99         }
100         String icon = getResource(iconName);
101         if (UV_ALARM.equals(category) && state != null) {
102             try {
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);
109             }
110         }
111         return icon.isEmpty() ? null : new ByteArrayInputStream(icon.getBytes());
112     }
113
114     private String getResource(String iconName) {
115         String result = "";
116
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());
123             }
124         } else {
125             logger.warn("Unable to load icon named '{}'", iconName);
126         }
127         return result;
128     }
129 }