2 * Copyright (c) 2010-2024 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.meteoalerte.internal;
15 import static org.openhab.binding.meteoalerte.internal.MeteoAlerteBindingConstants.*;
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.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;
40 * The {@link MeteoAlertIconProvider} is the class providing binding related icons.
42 * @author Gaël L'hopital - Initial contribution
44 @Component(service = { IconProvider.class, MeteoAlertIconProvider.class })
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,
52 private final Logger logger = LoggerFactory.getLogger(MeteoAlertIconProvider.class);
53 private final BundleContext context;
54 private final TranslationProvider i18nProvider;
57 public MeteoAlertIconProvider(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;
89 public @Nullable InputStream getIcon(String category, String state) {
90 return getIcon(category, BINDING_ID, state, Format.SVG);
94 public @Nullable InputStream getIcon(String category, String iconSetId, @Nullable String state, Format format) {
95 String icon = getResource(category);
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);
111 return new ByteArrayInputStream(icon.getBytes());
114 private String getResource(String iconName) {
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());