2 * Copyright (c) 2010-2020 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.handler;
15 import static org.openhab.binding.meteoalerte.internal.MeteoAlerteBindingConstants.*;
17 import java.io.BufferedInputStream;
18 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.MalformedURLException;
24 import java.time.ZonedDateTime;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.concurrent.ScheduledFuture;
28 import java.util.concurrent.TimeUnit;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.openhab.binding.meteoalerte.internal.MeteoAlerteConfiguration;
33 import org.openhab.binding.meteoalerte.internal.json.ApiResponse;
34 import org.openhab.core.i18n.TimeZoneProvider;
35 import org.openhab.core.io.net.http.HttpUtil;
36 import org.openhab.core.library.types.DateTimeType;
37 import org.openhab.core.library.types.RawType;
38 import org.openhab.core.library.types.StringType;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingStatus;
42 import org.openhab.core.thing.ThingStatusDetail;
43 import org.openhab.core.thing.binding.BaseThingHandler;
44 import org.openhab.core.types.Command;
45 import org.openhab.core.types.RefreshType;
46 import org.openhab.core.types.UnDefType;
47 import org.osgi.framework.FrameworkUtil;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import com.google.gson.Gson;
54 * The {@link MeteoAlerteHandler} is responsible for updating channels
55 * and querying the API
57 * @author Gaël L'hopital - Initial contribution
60 public class MeteoAlerteHandler extends BaseThingHandler {
61 private static final String URL = "https://public.opendatasoft.com/api/records/1.0/search/?dataset=risques-meteorologiques-copy&"
62 + "facet=etat_vent&facet=etat_pluie_inondation&facet=etat_orage&facet=etat_inondation&facet=etat_neige&facet=etat_canicule&"
63 + "facet=etat_grand_froid&facet=etat_avalanches&refine.nom_dept=";
64 private static final int TIMEOUT_MS = 30000;
65 private static final List<String> ALERT_LEVELS = Arrays.asList("Vert", "Jaune", "Orange", "Rouge");
66 private final Logger logger = LoggerFactory.getLogger(MeteoAlerteHandler.class);
68 // Time zone provider representing time zone configured in openHAB configuration
69 private final TimeZoneProvider timeZoneProvider;
70 private final Gson gson;
71 private @Nullable ScheduledFuture<?> refreshJob;
72 private String queryUrl = "";
74 public MeteoAlerteHandler(Thing thing, TimeZoneProvider timeZoneProvider, Gson gson) {
76 this.timeZoneProvider = timeZoneProvider;
81 public void initialize() {
82 logger.debug("Initializing Météo Alerte handler.");
84 MeteoAlerteConfiguration config = getConfigAs(MeteoAlerteConfiguration.class);
85 logger.debug("config department = {}", config.department);
86 logger.debug("config refresh = {}", config.refresh);
88 updateStatus(ThingStatus.UNKNOWN);
89 queryUrl = URL + config.department;
90 refreshJob = scheduler.scheduleWithFixedDelay(this::updateAndPublish, 0, config.refresh, TimeUnit.MINUTES);
94 public void dispose() {
95 logger.debug("Disposing the Météo Alerte handler.");
96 ScheduledFuture<?> refreshJob = this.refreshJob;
97 if (refreshJob != null) {
98 refreshJob.cancel(true);
100 this.refreshJob = null;
104 public void handleCommand(ChannelUID channelUID, Command command) {
105 if (command instanceof RefreshType) {
110 private void updateAndPublish() {
112 if (queryUrl.isEmpty()) {
113 throw new MalformedURLException("queryUrl not initialized");
115 String response = HttpUtil.executeUrl("GET", queryUrl, TIMEOUT_MS);
116 updateStatus(ThingStatus.ONLINE);
117 ApiResponse apiResponse = gson.fromJson(response, ApiResponse.class);
118 updateChannels(apiResponse);
119 } catch (MalformedURLException e) {
120 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
121 String.format("Querying '%s' raised : %s", queryUrl, e.getMessage()));
122 } catch (IOException e) {
123 logger.warn("Error opening connection to Meteo Alerte webservice : {}", e.getMessage());
124 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
129 * Update the channel from the last Meteo Alerte data retrieved
131 * @param channelId the id identifying the channel to be updated
133 private void updateChannels(ApiResponse apiResponse) {
134 Arrays.stream(apiResponse.getRecords()).findFirst()
135 .ifPresent((record) -> record.getResponseFieldDTO().ifPresent(fields -> {
136 updateAlertString(WIND, fields.getVent());
137 updateAlertString(RAIN, fields.getPluieInondation());
138 updateAlertString(STORM, fields.getOrage());
139 updateAlertString(FLOOD, fields.getInondation());
140 updateAlertString(SNOW, fields.getNeige());
141 updateAlertString(HEAT, fields.getCanicule());
142 updateAlertString(FREEZE, fields.getGrandFroid());
143 updateAlertString(AVALANCHE, fields.getAvalanches());
145 fields.getDateInsert().ifPresent(date -> updateDate(OBSERVATION_TIME, date));
146 updateState(COMMENT, new StringType(fields.getVigilanceComment()));
147 updateIcon(WIND, fields.getVent());
148 updateIcon(RAIN, fields.getPluieInondation());
149 updateIcon(STORM, fields.getOrage());
150 updateIcon(FLOOD, fields.getInondation());
151 updateIcon(SNOW, fields.getNeige());
152 updateIcon(HEAT, fields.getCanicule());
153 updateIcon(FREEZE, fields.getGrandFroid());
154 updateIcon(AVALANCHE, fields.getAvalanches());
158 public void updateIcon(String channelId, String value) {
159 String iconChannelId = channelId + "-icon";
160 if (isLinked(iconChannelId)) {
161 String pictoName = channelId + (!value.isEmpty() ? "_" + value.toLowerCase() : "");
162 byte[] image = getImage("picto" + File.separator + pictoName + ".gif");
164 RawType picto = new RawType(image, "image/gif");
165 updateState(iconChannelId, picto);
170 private byte @Nullable [] getImage(String iconPath) {
171 URL url = FrameworkUtil.getBundle(getClass()).getResource(iconPath);
172 logger.debug("Path to icon image resource is: {}", url);
173 try (InputStream in = new BufferedInputStream(url.openStream())) {
174 ByteArrayOutputStream bos = new ByteArrayOutputStream();
175 int next = in.read();
181 return bos.toByteArray();
182 } catch (IOException e) {
183 logger.debug("I/O exception occurred getting image data: {}", e.getMessage(), e);
188 public void updateAlertString(String channelId, String value) {
189 if (!value.isEmpty() && isLinked(channelId)) {
190 int level = ALERT_LEVELS.indexOf(value);
192 updateState(channelId, new StringType(Integer.toString(level)));
194 updateState(channelId, UnDefType.UNDEF);
195 logger.warn("Value {} is not a valid alert level for channel {}", value, channelId);
200 public void updateDate(String channelId, ZonedDateTime zonedDateTime) {
201 if (isLinked(channelId)) {
202 ZonedDateTime localDateTime = zonedDateTime.withZoneSameInstant(timeZoneProvider.getTimeZone());
203 updateState(channelId, new DateTimeType(localDateTime));