]> git.basschouten.com Git - openhab-addons.git/blob
7dfcc29952c1fc1cf1d453e3c0b9fc101ebc1636
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.handler;
14
15 import static org.openhab.binding.meteoalerte.internal.MeteoAlerteBindingConstants.*;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.MalformedURLException;
20 import java.nio.charset.StandardCharsets;
21 import java.time.ZonedDateTime;
22 import java.util.AbstractMap;
23 import java.util.Map;
24 import java.util.concurrent.ScheduledFuture;
25 import java.util.concurrent.TimeUnit;
26
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.meteoalerte.internal.MeteoAlerteConfiguration;
30 import org.openhab.binding.meteoalerte.internal.json.ApiResponse;
31 import org.openhab.binding.meteoalerte.internal.json.ResponseFieldDTO.AlertLevel;
32 import org.openhab.core.io.net.http.HttpUtil;
33 import org.openhab.core.library.types.DateTimeType;
34 import org.openhab.core.library.types.DecimalType;
35 import org.openhab.core.library.types.RawType;
36 import org.openhab.core.library.types.StringType;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.Thing;
39 import org.openhab.core.thing.ThingStatus;
40 import org.openhab.core.thing.ThingStatusDetail;
41 import org.openhab.core.thing.binding.BaseThingHandler;
42 import org.openhab.core.types.Command;
43 import org.openhab.core.types.RefreshType;
44 import org.openhab.core.types.State;
45 import org.openhab.core.types.UnDefType;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import com.google.gson.Gson;
50
51 /**
52  * The {@link MeteoAlerteHandler} is responsible for updating channels
53  * and querying the API
54  *
55  * @author Gaël L'hopital - Initial contribution
56  */
57 @NonNullByDefault
58 public class MeteoAlerteHandler extends BaseThingHandler {
59     private static final String URL = "https://public.opendatasoft.com/api/records/1.0/search/?dataset=risques-meteorologiques-copy&"
60             + "facet=etat_vent&facet=etat_pluie_inondation&facet=etat_orage&facet=etat_inondation&facet=etat_neige&facet=etat_canicule&"
61             + "facet=etat_grand_froid&facet=etat_avalanches&refine.nom_dept=%s";
62     private static final int TIMEOUT_MS = 30000;
63     private static final String UNKNOWN_COLOR = "b3b3b3";
64     private static final Map<AlertLevel, String> ALERT_COLORS = Map.ofEntries(
65             new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.GREEN, "00ff00"),
66             new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.YELLOW, "ffff00"),
67             new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.ORANGE, "ff6600"),
68             new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.RED, "ff0000"),
69             new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.UNKNOWN, UNKNOWN_COLOR));
70
71     private final Logger logger = LoggerFactory.getLogger(MeteoAlerteHandler.class);
72     // Time zone provider representing time zone configured in openHAB configuration
73     private final Gson gson;
74     private @Nullable ScheduledFuture<?> refreshJob;
75     private String queryUrl = "";
76
77     public MeteoAlerteHandler(Thing thing, Gson gson) {
78         super(thing);
79         this.gson = gson;
80     }
81
82     @Override
83     public void initialize() {
84         logger.debug("Initializing Météo Alerte handler.");
85
86         MeteoAlerteConfiguration config = getConfigAs(MeteoAlerteConfiguration.class);
87         logger.debug("config department = {}", config.department);
88         logger.debug("config refresh = {}", config.refresh);
89
90         updateStatus(ThingStatus.UNKNOWN);
91         queryUrl = String.format(URL, config.department);
92         refreshJob = scheduler.scheduleWithFixedDelay(this::updateAndPublish, 0, config.refresh, TimeUnit.MINUTES);
93     }
94
95     @Override
96     public void dispose() {
97         logger.debug("Disposing the Météo Alerte handler.");
98         ScheduledFuture<?> refreshJob = this.refreshJob;
99         if (refreshJob != null) {
100             refreshJob.cancel(true);
101         }
102         this.refreshJob = null;
103     }
104
105     @Override
106     public void handleCommand(ChannelUID channelUID, Command command) {
107         if (command instanceof RefreshType) {
108             updateAndPublish();
109         }
110     }
111
112     private void updateAndPublish() {
113         try {
114             if (queryUrl.isEmpty()) {
115                 throw new MalformedURLException("queryUrl not initialized");
116             }
117             String response = HttpUtil.executeUrl("GET", queryUrl, TIMEOUT_MS);
118             updateStatus(ThingStatus.ONLINE);
119             ApiResponse apiResponse = gson.fromJson(response, ApiResponse.class);
120             updateChannels(apiResponse);
121         } catch (MalformedURLException e) {
122             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
123                     String.format("Querying '%s' raised : %s", queryUrl, e.getMessage()));
124         } catch (IOException e) {
125             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
126         }
127     }
128
129     /**
130      * Update the channel from the last Meteo Alerte data retrieved
131      *
132      * @param channelId the id identifying the channel to be updated
133      */
134     private void updateChannels(ApiResponse apiResponse) {
135         apiResponse.getRecords().findFirst().ifPresent((record) -> record.getResponseFieldDTO().ifPresent(fields -> {
136             updateAlert(WIND, fields.getVent());
137             updateAlert(RAIN, fields.getPluieInondation());
138             updateAlert(STORM, fields.getOrage());
139             updateAlert(FLOOD, fields.getInondation());
140             updateAlert(SNOW, fields.getNeige());
141             updateAlert(HEAT, fields.getCanicule());
142             updateAlert(FREEZE, fields.getGrandFroid());
143             updateAlert(AVALANCHE, fields.getAvalanches());
144             updateAlert(WAVE, fields.getVagueSubmersion());
145             updateState(COMMENT, new StringType(fields.getVigilanceComment()));
146             fields.getDateInsert().ifPresent(date -> updateDate(OBSERVATION_TIME, date));
147             fields.getDatePrevue().ifPresent(date -> updateDate(END_TIME, date));
148         }));
149     }
150
151     public byte @Nullable [] getResource(String iconPath) {
152         ClassLoader classLoader = MeteoAlerteHandler.class.getClassLoader();
153         if (classLoader != null) {
154             try (InputStream stream = classLoader.getResourceAsStream(iconPath)) {
155                 return stream != null ? stream.readAllBytes() : null;
156             } catch (IOException e) {
157                 logger.warn("Unable to load ressource '{}' : {}", iconPath, e.getMessage());
158             }
159         }
160         return null;
161     }
162
163     public void updateAlert(String channelId, AlertLevel value) {
164         String channelIcon = channelId + "-icon";
165         if (isLinked(channelId)) {
166             updateState(channelId, getAlertLevel(value));
167         }
168         if (isLinked(channelIcon)) {
169             State result = UnDefType.UNDEF;
170             byte[] bytes = getResource(String.format("picto/%s.svg", channelId));
171             if (bytes != null) {
172                 String resource = new String(bytes, StandardCharsets.UTF_8);
173                 resource = resource.replaceAll(UNKNOWN_COLOR, ALERT_COLORS.getOrDefault(value, UNKNOWN_COLOR));
174                 result = new RawType(resource.getBytes(StandardCharsets.UTF_8), "image/svg+xml");
175             }
176             updateState(channelIcon, result);
177         }
178     }
179
180     public void updateDate(String channelId, ZonedDateTime zonedDateTime) {
181         if (isLinked(channelId)) {
182             updateState(channelId, new DateTimeType(zonedDateTime));
183         }
184     }
185
186     public State getAlertLevel(AlertLevel alert) {
187         switch (alert) {
188             case GREEN:
189                 return DecimalType.ZERO;
190             case YELLOW:
191                 return new DecimalType(1);
192             case ORANGE:
193                 return new DecimalType(2);
194             case RED:
195                 return new DecimalType(3);
196             default:
197                 return UnDefType.UNDEF;
198         }
199     }
200 }