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