]> git.basschouten.com Git - openhab-addons.git/blob
cbfb357a1083bb70013a6d905318e4d89260cb26
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.meteoalerte.internal;
14
15 import static org.openhab.binding.meteoalerte.internal.MeteoAlerteBindingConstants.*;
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.binding.meteoalerte.internal.json.ResponseFieldDTO.AlertLevel;
28 import org.openhab.core.i18n.TranslationProvider;
29 import org.openhab.core.ui.icon.IconProvider;
30 import org.openhab.core.ui.icon.IconSet;
31 import org.openhab.core.ui.icon.IconSet.Format;
32 import org.osgi.framework.BundleContext;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link MeteoAlertIconProvider} is the class providing binding related icons.
41  *
42  * @author Gaël L'hopital - Initial contribution
43  */
44 @Component(service = { IconProvider.class, MeteoAlertIconProvider.class })
45 @NonNullByDefault
46 public class MeteoAlertIconProvider implements IconProvider {
47     private static final String DEFAULT_LABEL = "Météo Alerte Icons";
48     private static final String DEFAULT_DESCRIPTION = "Icons illustrating weather events provided by Météo Alerte";
49     private static final Set<String> ICONS = Set.of(WAVE, AVALANCHE, HEAT, FREEZE, FLOOD, SNOW, STORM, RAIN, WIND,
50             "meteo_france");
51
52     private final Logger logger = LoggerFactory.getLogger(MeteoAlertIconProvider.class);
53     private final BundleContext context;
54     private final TranslationProvider i18nProvider;
55
56     @Activate
57     public MeteoAlertIconProvider(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     public @Nullable InputStream getIcon(String category, String state) {
90         return getIcon(category, BINDING_ID, state, Format.SVG);
91     }
92
93     @Override
94     public @Nullable InputStream getIcon(String category, String iconSetId, @Nullable String state, Format format) {
95         String icon = getResource(category);
96         if (icon.isEmpty()) {
97             return null;
98         }
99
100         if (state != null) {
101             try {
102                 Integer ordinal = Integer.valueOf(state);
103                 AlertLevel alertLevel = ordinal < AlertLevel.values().length ? AlertLevel.values()[ordinal]
104                         : AlertLevel.UNKNOWN;
105                 icon = icon.replaceAll(AlertLevel.UNKNOWN.color, alertLevel.color);
106             } catch (NumberFormatException e) {
107                 logger.debug("{} is not a valid DecimalType", state);
108             }
109         }
110
111         return 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         try (InputStream stream = iconResource.openStream()) {
119             result = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
120         } catch (IOException e) {
121             logger.warn("Unable to load ressource '{}' : {}", iconResource.getPath(), e.getMessage());
122         }
123         return result;
124     }
125 }