]> git.basschouten.com Git - openhab-addons.git/blob
9cd2d030847d5603a600411010aca9c401ed620a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.openweathermap.internal.handler;
14
15 import static org.openhab.binding.openweathermap.internal.OpenWeatherMapBindingConstants.*;
16 import static org.openhab.core.library.unit.MetricPrefix.*;
17 import static org.openhab.core.library.unit.SIUnits.*;
18 import static org.openhab.core.library.unit.Units.*;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapOneCallConfiguration;
28 import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapCommunicationException;
29 import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConfigurationException;
30 import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
31 import org.openhab.binding.openweathermap.internal.dto.onecall.Alert;
32 import org.openhab.binding.openweathermap.internal.dto.onecall.FeelsLike;
33 import org.openhab.binding.openweathermap.internal.dto.onecall.OpenWeatherMapOneCallAPIData;
34 import org.openhab.binding.openweathermap.internal.dto.onecall.Rain;
35 import org.openhab.binding.openweathermap.internal.dto.onecall.Snow;
36 import org.openhab.binding.openweathermap.internal.dto.onecall.Temp;
37 import org.openhab.core.i18n.TimeZoneProvider;
38 import org.openhab.core.library.types.QuantityType;
39 import org.openhab.core.thing.Channel;
40 import org.openhab.core.thing.ChannelUID;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.thing.ThingStatus;
43 import org.openhab.core.thing.ThingStatusDetail;
44 import org.openhab.core.thing.binding.builder.ThingBuilder;
45 import org.openhab.core.types.State;
46 import org.openhab.core.types.UnDefType;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import com.google.gson.JsonSyntaxException;
51
52 /**
53  * The {@link OpenWeatherMapOneCallHandler} is responsible for handling commands, which are sent to one of
54  * the channels.
55  *
56  * @author Wolfgang Klimt - Initial contribution
57  * @author Christoph Weitkamp - Added weather alerts
58  */
59 @NonNullByDefault
60 public class OpenWeatherMapOneCallHandler extends AbstractOpenWeatherMapHandler {
61
62     private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapOneCallHandler.class);
63
64     private static final String CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX = "forecastMinutes";
65     private static final String CHANNEL_GROUP_HOURLY_FORECAST_PREFIX = "forecastHours";
66     private static final String CHANNEL_GROUP_DAILY_FORECAST_PREFIX = "forecastDay";
67     private static final String CHANNEL_GROUP_ALERTS_PREFIX = "alerts";
68     private static final Pattern CHANNEL_GROUP_HOURLY_FORECAST_PREFIX_PATTERN = Pattern
69             .compile(CHANNEL_GROUP_HOURLY_FORECAST_PREFIX + "([0-9]*)");
70     private static final Pattern CHANNEL_GROUP_DAILY_FORECAST_PREFIX_PATTERN = Pattern
71             .compile(CHANNEL_GROUP_DAILY_FORECAST_PREFIX + "([0-9]*)");
72     private static final Pattern CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX_PATTERN = Pattern
73             .compile(CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX + "([0-9]*)");
74     private static final Pattern CHANNEL_GROUP_ALERTS_PREFIX_PATTERN = Pattern
75             .compile(CHANNEL_GROUP_ALERTS_PREFIX + "([0-9]*)");
76
77     private @Nullable OpenWeatherMapOneCallAPIData weatherData;
78
79     private int forecastMinutes = 60;
80     private int forecastHours = 24;
81     private int forecastDays = 8;
82     private int numberOfAlerts = 0;
83
84     public OpenWeatherMapOneCallHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
85         super(thing, timeZoneProvider);
86     }
87
88     @Override
89     public void initialize() {
90         super.initialize();
91         logger.debug("Initialize OpenWeatherMapOneCallHandler handler '{}'.", getThing().getUID());
92         OpenWeatherMapOneCallConfiguration config = getConfigAs(OpenWeatherMapOneCallConfiguration.class);
93
94         boolean configValid = true;
95         int newForecastMinutes = config.forecastMinutes;
96         if (newForecastMinutes < 0 || newForecastMinutes > 60) {
97             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
98                     "@text/offline.conf-error-not-supported-onecall-number-of-minutes");
99             configValid = false;
100         }
101         int newForecastHours = config.forecastHours;
102         if (newForecastHours < 0 || newForecastHours > 48) {
103             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
104                     "@text/offline.conf-error-not-supported-onecall-number-of-hours");
105             configValid = false;
106         }
107         int newForecastDays = config.forecastDays;
108         if (newForecastDays < 0 || newForecastDays > 8) {
109             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
110                     "@text/offline.conf-error-not-supported-onecall-number-of-days");
111             configValid = false;
112         }
113         int newNumberOfAlerts = config.numberOfAlerts;
114         if (newNumberOfAlerts < 0) {
115             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
116                     "@text/offline.conf-error-not-supported-onecall-number-of-alerts");
117             configValid = false;
118         }
119
120         if (configValid) {
121             logger.debug("Rebuilding thing '{}'.", getThing().getUID());
122             List<Channel> toBeAddedChannels = new ArrayList<>();
123             List<Channel> toBeRemovedChannels = new ArrayList<>();
124             toBeAddedChannels
125                     .addAll(createChannelsForGroup(CHANNEL_GROUP_ONECALL_CURRENT, CHANNEL_GROUP_TYPE_ONECALL_CURRENT));
126             if (forecastMinutes != newForecastMinutes) {
127                 logger.debug("forecastMinutes changed from {} to {}. Rebuilding minutely forecast channel groups.",
128                         forecastMinutes, newForecastMinutes);
129                 if (forecastMinutes > newForecastMinutes) {
130                     for (int i = newForecastMinutes + 1; i <= forecastMinutes; i++) {
131                         toBeRemovedChannels.addAll(removeChannelsOfGroup(
132                                 CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i)));
133                     }
134                 } else {
135                     for (int i = forecastMinutes + 1; i <= newForecastMinutes; i++) {
136                         toBeAddedChannels.addAll(createChannelsForGroup(
137                                 CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i),
138                                 CHANNEL_GROUP_TYPE_ONECALL_MINUTELY_FORECAST));
139                     }
140                 }
141                 forecastMinutes = newForecastMinutes;
142             }
143             if (forecastHours != newForecastHours) {
144                 logger.debug("ForecastHours changed from {} to {}. Rebuilding hourly forecast channel groups.",
145                         forecastHours, newForecastHours);
146                 if (forecastHours > newForecastHours) {
147                     for (int i = newForecastHours + 1; i <= forecastHours; i++) {
148                         toBeRemovedChannels.addAll(removeChannelsOfGroup(
149                                 CHANNEL_GROUP_HOURLY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i)));
150                     }
151                 } else {
152                     for (int i = forecastHours + 1; i <= newForecastHours; i++) {
153                         toBeAddedChannels.addAll(createChannelsForGroup(
154                                 CHANNEL_GROUP_HOURLY_FORECAST_PREFIX + ((i < 10) ? "0" : "") + Integer.toString(i),
155                                 CHANNEL_GROUP_TYPE_ONECALL_HOURLY_FORECAST));
156                     }
157                 }
158                 forecastHours = newForecastHours;
159             }
160             if (forecastDays != newForecastDays) {
161                 logger.debug("ForecastDays changed from {} to {}. Rebuilding daily forecast channel groups.",
162                         forecastDays, newForecastDays);
163                 if (forecastDays > newForecastDays) {
164                     if (newForecastDays < 1) {
165                         toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_FORECAST_TODAY));
166                     }
167                     if (newForecastDays < 2) {
168                         toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_FORECAST_TOMORROW));
169                     }
170                     for (int i = newForecastDays; i < forecastDays; ++i) {
171                         toBeRemovedChannels.addAll(
172                                 removeChannelsOfGroup(CHANNEL_GROUP_DAILY_FORECAST_PREFIX + Integer.toString(i)));
173                     }
174                 } else {
175                     if (forecastDays == 0 && newForecastDays > 0) {
176                         toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_FORECAST_TODAY,
177                                 CHANNEL_GROUP_TYPE_ONECALL_DAILY_FORECAST));
178                     }
179                     if (forecastDays <= 1 && newForecastDays > 1) {
180                         toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_FORECAST_TOMORROW,
181                                 CHANNEL_GROUP_TYPE_ONECALL_DAILY_FORECAST));
182                     }
183                     for (int i = Math.max(forecastDays, 2); i < newForecastDays; ++i) {
184                         toBeAddedChannels.addAll(
185                                 createChannelsForGroup(CHANNEL_GROUP_DAILY_FORECAST_PREFIX + Integer.toString(i),
186                                         CHANNEL_GROUP_TYPE_ONECALL_DAILY_FORECAST));
187                     }
188                 }
189                 forecastDays = newForecastDays;
190                 if (numberOfAlerts != newNumberOfAlerts) {
191                     logger.debug("Rebuilding alerts channel groups.");
192                     if (numberOfAlerts > newNumberOfAlerts) {
193                         for (int i = newNumberOfAlerts + 1; i <= numberOfAlerts; ++i) {
194                             toBeRemovedChannels
195                                     .addAll(removeChannelsOfGroup(CHANNEL_GROUP_ALERTS_PREFIX + Integer.toString(i)));
196                         }
197                     } else {
198                         for (int i = numberOfAlerts + 1; i <= newNumberOfAlerts; ++i) {
199                             toBeAddedChannels
200                                     .addAll(createChannelsForGroup(CHANNEL_GROUP_ALERTS_PREFIX + Integer.toString(i),
201                                             CHANNEL_GROUP_TYPE_ONECALL_ALERTS));
202                         }
203                     }
204                     numberOfAlerts = newNumberOfAlerts;
205                 }
206             }
207             logger.debug("toBeRemovedChannels: {}. toBeAddedChannels: {}", toBeRemovedChannels, toBeAddedChannels);
208             ThingBuilder builder = editThing().withoutChannels(toBeRemovedChannels);
209             for (Channel channel : toBeAddedChannels) {
210                 builder.withChannel(channel);
211             }
212             updateThing(builder.build());
213         }
214     }
215
216     @Override
217     protected boolean requestData(OpenWeatherMapConnection connection)
218             throws OpenWeatherMapCommunicationException, OpenWeatherMapConfigurationException {
219         logger.debug("Update weather and forecast data of thing '{}'.", getThing().getUID());
220         try {
221             weatherData = connection.getOneCallAPIData(location, forecastMinutes == 0, forecastHours == 0,
222                     forecastDays == 0, numberOfAlerts == 0);
223             return true;
224         } catch (JsonSyntaxException e) {
225             logger.debug("JsonSyntaxException occurred during execution: {}", e.getLocalizedMessage(), e);
226             return false;
227         }
228     }
229
230     @Override
231     protected void updateChannel(ChannelUID channelUID) {
232         String channelGroupId = channelUID.getGroupId();
233         logger.debug("OneCallHandler: updateChannel {}, groupID {}", channelUID, channelGroupId);
234         switch (channelGroupId) {
235             case CHANNEL_GROUP_ONECALL_CURRENT:
236                 updateCurrentChannel(channelUID);
237                 break;
238             case CHANNEL_GROUP_ONECALL_TODAY:
239                 updateDailyForecastChannel(channelUID, 0);
240                 break;
241             case CHANNEL_GROUP_ONECALL_TOMORROW:
242                 updateDailyForecastChannel(channelUID, 1);
243                 break;
244             default:
245                 int i;
246                 Matcher hourlyForecastMatcher = CHANNEL_GROUP_HOURLY_FORECAST_PREFIX_PATTERN.matcher(channelGroupId);
247                 if (hourlyForecastMatcher.find() && (i = Integer.parseInt(hourlyForecastMatcher.group(1))) >= 1
248                         && i <= 48) {
249                     updateHourlyForecastChannel(channelUID, (i - 1));
250                     break;
251                 }
252                 Matcher dailyForecastMatcher = CHANNEL_GROUP_DAILY_FORECAST_PREFIX_PATTERN.matcher(channelGroupId);
253                 if (dailyForecastMatcher.find() && (i = Integer.parseInt(dailyForecastMatcher.group(1))) >= 1
254                         && i <= 7) {
255                     updateDailyForecastChannel(channelUID, i);
256                     break;
257                 }
258                 Matcher minutelyForecastMatcher = CHANNEL_GROUP_MINUTELY_FORECAST_PREFIX_PATTERN
259                         .matcher(channelGroupId);
260                 if (minutelyForecastMatcher.find() && (i = Integer.parseInt(minutelyForecastMatcher.group(1))) >= 1
261                         && i <= 60) {
262                     updateMinutelyForecastChannel(channelUID, i - 1);
263                     break;
264                 }
265                 Matcher alertsMatcher = CHANNEL_GROUP_ALERTS_PREFIX_PATTERN.matcher(channelGroupId);
266                 if (alertsMatcher.find() && (i = Integer.parseInt(alertsMatcher.group(1))) >= 1) {
267                     updateAlertsChannel(channelUID, i);
268                     break;
269                 }
270                 break;
271         }
272     }
273
274     /**
275      * Update the channel from the last OpenWeatherMap data retrieved.
276      *
277      * @param channelUID the id identifying the channel to be updated
278      */
279     private void updateCurrentChannel(ChannelUID channelUID) {
280         String channelId = channelUID.getIdWithoutGroup();
281         String channelGroupId = channelUID.getGroupId();
282         OpenWeatherMapOneCallAPIData localWeatherData = weatherData;
283         if (localWeatherData != null) {
284             State state = UnDefType.UNDEF;
285             switch (channelId) {
286                 case CHANNEL_STATION_LOCATION:
287                     state = getPointTypeState(localWeatherData.getLat(), localWeatherData.getLon());
288                     break;
289                 case CHANNEL_TIME_STAMP:
290                     state = getDateTimeTypeState(localWeatherData.getCurrent().getDt());
291                     break;
292                 case CHANNEL_SUNRISE:
293                     state = getDateTimeTypeState(localWeatherData.getCurrent().getSunrise());
294                     break;
295                 case CHANNEL_SUNSET:
296                     state = getDateTimeTypeState(localWeatherData.getCurrent().getSunset());
297                     break;
298                 case CHANNEL_CONDITION:
299                     if (!localWeatherData.getCurrent().getWeather().isEmpty()) {
300                         state = getStringTypeState(localWeatherData.getCurrent().getWeather().get(0).getDescription());
301                     }
302                     break;
303                 case CHANNEL_CONDITION_ID:
304                     if (!localWeatherData.getCurrent().getWeather().isEmpty()) {
305                         state = getStringTypeState(
306                                 Integer.toString(localWeatherData.getCurrent().getWeather().get(0).getId()));
307                     }
308                     break;
309                 case CHANNEL_CONDITION_ICON:
310                     if (!localWeatherData.getCurrent().getWeather().isEmpty()) {
311                         state = getRawTypeState(OpenWeatherMapConnection
312                                 .getWeatherIcon(localWeatherData.getCurrent().getWeather().get(0).getIcon()));
313                     }
314                     break;
315                 case CHANNEL_CONDITION_ICON_ID:
316                     if (!localWeatherData.getCurrent().getWeather().isEmpty()) {
317                         state = getStringTypeState(localWeatherData.getCurrent().getWeather().get(0).getIcon());
318                     }
319                     break;
320                 case CHANNEL_TEMPERATURE:
321                     state = getQuantityTypeState(localWeatherData.getCurrent().getTemp(), CELSIUS);
322                     break;
323                 case CHANNEL_APPARENT_TEMPERATURE:
324                     state = getQuantityTypeState(localWeatherData.getCurrent().getFeelsLike(), CELSIUS);
325                     break;
326                 case CHANNEL_PRESSURE:
327                     state = getQuantityTypeState(localWeatherData.getCurrent().getPressure(), HECTO(PASCAL));
328                     break;
329                 case CHANNEL_HUMIDITY:
330                     state = getQuantityTypeState(localWeatherData.getCurrent().getHumidity(), PERCENT);
331                     break;
332                 case CHANNEL_DEW_POINT:
333                     state = getQuantityTypeState(localWeatherData.getCurrent().getDewPoint(), CELSIUS);
334                     break;
335                 case CHANNEL_WIND_SPEED:
336                     state = getQuantityTypeState(localWeatherData.getCurrent().getWindSpeed(), METRE_PER_SECOND);
337                     break;
338                 case CHANNEL_WIND_DIRECTION:
339                     state = getQuantityTypeState(localWeatherData.getCurrent().getWindDeg(), DEGREE_ANGLE);
340                     break;
341                 case CHANNEL_GUST_SPEED:
342                     state = getQuantityTypeState(localWeatherData.getCurrent().getWindGust(), METRE_PER_SECOND);
343                     break;
344                 case CHANNEL_CLOUDINESS:
345                     state = getQuantityTypeState(localWeatherData.getCurrent().getClouds(), PERCENT);
346                     break;
347                 case CHANNEL_UVINDEX:
348                     state = getDecimalTypeState(localWeatherData.getCurrent().getUvi());
349                     break;
350                 case CHANNEL_RAIN:
351                     Rain rain = localWeatherData.getCurrent().getRain();
352                     state = getQuantityTypeState(rain == null ? 0 : rain.get1h(), MILLI(METRE));
353                     break;
354                 case CHANNEL_SNOW:
355                     Snow snow = localWeatherData.getCurrent().getSnow();
356                     state = getQuantityTypeState(snow == null ? 0 : snow.get1h(), MILLI(METRE));
357                     break;
358                 case CHANNEL_VISIBILITY:
359                     State tempstate = new QuantityType<>(localWeatherData.getCurrent().getVisibility(), METRE)
360                             .toUnit(KILO(METRE));
361                     state = (tempstate == null ? state : tempstate);
362                     break;
363                 default:
364                     // This should not happen
365                     logger.warn("Unknown channel id {} in onecall current weather data", channelId);
366                     break;
367             }
368             logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
369             updateState(channelUID, state);
370         } else {
371             logger.debug("No weather data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
372         }
373     }
374
375     private void updateMinutelyForecastChannel(ChannelUID channelUID, int count) {
376         String channelId = channelUID.getIdWithoutGroup();
377         String channelGroupId = channelUID.getGroupId();
378         OpenWeatherMapOneCallAPIData localWeatherData = weatherData;
379         if (forecastMinutes == 0) {
380             logger.warn(
381                     "Can't update channel group {} because forecastMinutes is set to '0'. Please adjust config accordingly",
382                     channelGroupId);
383             return;
384         }
385         if (localWeatherData != null && localWeatherData.getMinutely().size() > count) {
386             org.openhab.binding.openweathermap.internal.dto.onecall.Minutely forecastData = localWeatherData
387                     .getMinutely().get(count);
388             State state = UnDefType.UNDEF;
389             switch (channelId) {
390                 case CHANNEL_TIME_STAMP:
391                     state = getDateTimeTypeState(forecastData.getDt());
392                     break;
393                 case CHANNEL_PRECIPITATION:
394                     double precipitation = forecastData.getPrecipitation();
395                     state = getQuantityTypeState(precipitation, MILLI(METRE));
396                     break;
397                 default:
398                     // This should not happen
399                     logger.warn("Unknown channel id {} in onecall minutely weather data", channelId);
400                     break;
401             }
402             logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
403             updateState(channelUID, state);
404         } else {
405             logger.debug("No weather data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
406         }
407     }
408
409     /**
410      * Update the channel from the last OpenWeatherMap data retrieved.
411      *
412      * @param channelUID the id identifying the channel to be updated
413      * @param count the number of the hour referenced by the channel
414      */
415     private void updateHourlyForecastChannel(ChannelUID channelUID, int count) {
416         String channelId = channelUID.getIdWithoutGroup();
417         String channelGroupId = channelUID.getGroupId();
418         if (forecastHours == 0) {
419             logger.warn(
420                     "Can't update channel group {} because forecastHours is set to '0'. Please adjust config accordingly",
421                     channelGroupId);
422             return;
423         }
424         OpenWeatherMapOneCallAPIData localWeatherData = weatherData;
425         if (localWeatherData != null && localWeatherData.getHourly().size() > count) {
426             org.openhab.binding.openweathermap.internal.dto.onecall.Hourly forecastData = localWeatherData.getHourly()
427                     .get(count);
428             State state = UnDefType.UNDEF;
429             switch (channelId) {
430                 case CHANNEL_TIME_STAMP:
431                     state = getDateTimeTypeState(forecastData.getDt());
432                     break;
433                 case CHANNEL_CONDITION:
434                     if (!forecastData.getWeather().isEmpty()) {
435                         state = getStringTypeState(forecastData.getWeather().get(0).getDescription());
436                     }
437                     break;
438                 case CHANNEL_CONDITION_ID:
439                     if (!forecastData.getWeather().isEmpty()) {
440                         state = getStringTypeState(Integer.toString(forecastData.getWeather().get(0).getId()));
441                     }
442                     break;
443                 case CHANNEL_CONDITION_ICON:
444                     if (!forecastData.getWeather().isEmpty()) {
445                         state = getRawTypeState(
446                                 OpenWeatherMapConnection.getWeatherIcon(forecastData.getWeather().get(0).getIcon()));
447                     }
448                     break;
449                 case CHANNEL_CONDITION_ICON_ID:
450                     if (!forecastData.getWeather().isEmpty()) {
451                         state = getStringTypeState(forecastData.getWeather().get(0).getIcon());
452                     }
453                     break;
454                 case CHANNEL_TEMPERATURE:
455                     state = getQuantityTypeState(forecastData.getTemp(), CELSIUS);
456                     break;
457                 case CHANNEL_APPARENT_TEMPERATURE:
458                     state = getQuantityTypeState(forecastData.getFeelsLike(), CELSIUS);
459                     break;
460                 case CHANNEL_PRESSURE:
461                     state = getQuantityTypeState(forecastData.getPressure(), HECTO(PASCAL));
462                     break;
463                 case CHANNEL_HUMIDITY:
464                     state = getQuantityTypeState(forecastData.getHumidity(), PERCENT);
465                     break;
466                 case CHANNEL_DEW_POINT:
467                     state = getQuantityTypeState(forecastData.getDewPoint(), CELSIUS);
468                     break;
469                 case CHANNEL_WIND_SPEED:
470                     state = getQuantityTypeState(forecastData.getWindSpeed(), METRE_PER_SECOND);
471                     break;
472                 case CHANNEL_WIND_DIRECTION:
473                     state = getQuantityTypeState(forecastData.getWindDeg(), DEGREE_ANGLE);
474                     break;
475                 case CHANNEL_GUST_SPEED:
476                     state = getQuantityTypeState(forecastData.getWindGust(), METRE_PER_SECOND);
477                     break;
478                 case CHANNEL_CLOUDINESS:
479                     state = getQuantityTypeState(forecastData.getClouds(), PERCENT);
480                     break;
481                 case CHANNEL_VISIBILITY:
482                     State tempstate = new QuantityType<>(localWeatherData.getCurrent().getVisibility(), METRE)
483                             .toUnit(KILO(METRE));
484                     state = (tempstate == null ? state : tempstate);
485                 case CHANNEL_PRECIP_PROBABILITY:
486                     state = getQuantityTypeState(forecastData.getPop() * 100.0, PERCENT);
487                     break;
488                 case CHANNEL_RAIN:
489                     Rain rain = forecastData.getRain();
490                     state = getQuantityTypeState(rain == null ? 0 : rain.get1h(), MILLI(METRE));
491                     break;
492                 case CHANNEL_SNOW:
493                     Snow snow = forecastData.getSnow();
494                     state = getQuantityTypeState(snow == null ? 0 : snow.get1h(), MILLI(METRE));
495                     break;
496                 default:
497                     // This should not happen
498                     logger.warn("Unknown channel id {} in onecall hourly weather data", channelId);
499                     break;
500             }
501             logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
502             updateState(channelUID, state);
503         } else {
504             logger.debug("No weather data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
505         }
506     }
507
508     /**
509      * Update the channel from the last OpenWeatherMap data retrieved.
510      *
511      * @param channelUID the id identifying the channel to be updated
512      * @param count
513      */
514     private void updateDailyForecastChannel(ChannelUID channelUID, int count) {
515         String channelId = channelUID.getIdWithoutGroup();
516         String channelGroupId = channelUID.getGroupId();
517         if (forecastDays == 0) {
518             logger.warn(
519                     "Can't update channel group {} because forecastDays is set to '0'. Please adjust config accordingly",
520                     channelGroupId);
521             return;
522         }
523         OpenWeatherMapOneCallAPIData localWeatherData = weatherData;
524         if (localWeatherData != null && localWeatherData.getDaily().size() > count) {
525             org.openhab.binding.openweathermap.internal.dto.onecall.Daily forecastData = localWeatherData.getDaily()
526                     .get(count);
527             State state = UnDefType.UNDEF;
528             Temp temp;
529             FeelsLike feelsLike;
530             switch (channelId) {
531                 case CHANNEL_TIME_STAMP:
532                     state = getDateTimeTypeState(forecastData.getDt());
533                     break;
534                 case CHANNEL_SUNRISE:
535                     state = getDateTimeTypeState(forecastData.getSunrise());
536                     break;
537                 case CHANNEL_SUNSET:
538                     state = getDateTimeTypeState(forecastData.getSunset());
539                     break;
540                 case CHANNEL_CONDITION:
541                     if (!forecastData.getWeather().isEmpty()) {
542                         state = getStringTypeState(forecastData.getWeather().get(0).getDescription());
543                     }
544                     break;
545                 case CHANNEL_CONDITION_ID:
546                     if (!forecastData.getWeather().isEmpty()) {
547                         state = getStringTypeState(Integer.toString(forecastData.getWeather().get(0).getId()));
548                     }
549                     break;
550                 case CHANNEL_CONDITION_ICON:
551                     if (!forecastData.getWeather().isEmpty()) {
552                         state = getRawTypeState(
553                                 OpenWeatherMapConnection.getWeatherIcon(forecastData.getWeather().get(0).getIcon()));
554                     }
555                     break;
556                 case CHANNEL_CONDITION_ICON_ID:
557                     if (!forecastData.getWeather().isEmpty()) {
558                         state = getStringTypeState(forecastData.getWeather().get(0).getIcon());
559                     }
560                     break;
561                 case CHANNEL_MIN_TEMPERATURE:
562                     temp = forecastData.getTemp();
563                     if (temp != null) {
564                         state = getQuantityTypeState(temp.getMin(), CELSIUS);
565                     }
566                     break;
567                 case CHANNEL_MAX_TEMPERATURE:
568                     temp = forecastData.getTemp();
569                     if (temp != null) {
570                         state = getQuantityTypeState(temp.getMax(), CELSIUS);
571                     }
572                     break;
573                 case CHANNEL_MORNING_TEMPERATURE:
574                     temp = forecastData.getTemp();
575                     if (temp != null) {
576                         state = getQuantityTypeState(temp.getMorn(), CELSIUS);
577                     }
578                     break;
579                 case CHANNEL_DAY_TEMPERATURE:
580                     temp = forecastData.getTemp();
581                     if (temp != null) {
582                         state = getQuantityTypeState(temp.getDay(), CELSIUS);
583                     }
584                     break;
585                 case CHANNEL_EVENING_TEMPERATURE:
586                     temp = forecastData.getTemp();
587                     if (temp != null) {
588                         state = getQuantityTypeState(temp.getEve(), CELSIUS);
589                     }
590                     break;
591                 case CHANNEL_NIGHT_TEMPERATURE:
592                     temp = forecastData.getTemp();
593                     if (temp != null) {
594                         state = getQuantityTypeState(temp.getNight(), CELSIUS);
595                     }
596                     break;
597
598                 case CHANNEL_APPARENT_DAY:
599                     feelsLike = forecastData.getFeelsLike();
600                     if (feelsLike != null) {
601                         state = getQuantityTypeState(feelsLike.getDay(), CELSIUS);
602                     }
603                     break;
604                 case CHANNEL_APPARENT_MORNING:
605                     feelsLike = forecastData.getFeelsLike();
606                     if (feelsLike != null) {
607                         state = getQuantityTypeState(feelsLike.getMorn(), CELSIUS);
608                     }
609                     break;
610                 case CHANNEL_APPARENT_EVENING:
611                     feelsLike = forecastData.getFeelsLike();
612                     if (feelsLike != null) {
613                         state = getQuantityTypeState(feelsLike.getEve(), CELSIUS);
614                     }
615                     break;
616                 case CHANNEL_APPARENT_NIGHT:
617                     feelsLike = forecastData.getFeelsLike();
618                     if (feelsLike != null) {
619                         state = getQuantityTypeState(feelsLike.getNight(), CELSIUS);
620                     }
621                     break;
622                 case CHANNEL_PRESSURE:
623                     state = getQuantityTypeState(forecastData.getPressure(), HECTO(PASCAL));
624                     break;
625                 case CHANNEL_HUMIDITY:
626                     state = getQuantityTypeState(forecastData.getHumidity(), PERCENT);
627                     break;
628                 case CHANNEL_WIND_SPEED:
629                     state = getQuantityTypeState(forecastData.getWindSpeed(), METRE_PER_SECOND);
630                     break;
631                 case CHANNEL_WIND_DIRECTION:
632                     state = getQuantityTypeState(forecastData.getWindDeg(), DEGREE_ANGLE);
633                     break;
634                 case CHANNEL_GUST_SPEED:
635                     state = getQuantityTypeState(forecastData.getWindGust(), METRE_PER_SECOND);
636                     break;
637                 case CHANNEL_CLOUDINESS:
638                     state = getQuantityTypeState(forecastData.getClouds(), PERCENT);
639                     break;
640                 case CHANNEL_DEW_POINT:
641                     state = getQuantityTypeState(forecastData.getDewPoint(), CELSIUS);
642                     break;
643                 case CHANNEL_UVINDEX:
644                     state = getDecimalTypeState(forecastData.getUvi());
645                     break;
646                 case CHANNEL_VISIBILITY:
647                     State tempstate = new QuantityType<>(localWeatherData.getCurrent().getVisibility(), METRE)
648                             .toUnit(KILO(METRE));
649                     state = (tempstate == null ? state : tempstate);
650                 case CHANNEL_PRECIP_PROBABILITY:
651                     state = getQuantityTypeState(forecastData.getPop() * 100.0, PERCENT);
652                     break;
653                 case CHANNEL_RAIN:
654                     state = getQuantityTypeState(forecastData.getRain(), MILLI(METRE));
655                     break;
656                 case CHANNEL_SNOW:
657                     state = getQuantityTypeState(forecastData.getSnow(), MILLI(METRE));
658                     break;
659                 default:
660                     // This should not happen
661                     logger.warn("Unknown channel id {} in onecall daily weather data", channelId);
662                     break;
663             }
664             logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
665             updateState(channelUID, state);
666         } else {
667             logger.debug("No weather data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
668         }
669     }
670
671     /**
672      * Update the channel from the last OpenWeaterhMap data retrieved.
673      *
674      * @param channelUID the id identifying the channel to be updated
675      * @param count
676      */
677     private void updateAlertsChannel(ChannelUID channelUID, int count) {
678         String channelId = channelUID.getIdWithoutGroup();
679         String channelGroupId = channelUID.getGroupId();
680         OpenWeatherMapOneCallAPIData localWeatherData = weatherData;
681         List<Alert> alerts = localWeatherData != null ? localWeatherData.alerts : null;
682         State state = UnDefType.UNDEF;
683         if (alerts != null && alerts.size() > count) {
684             Alert alert = alerts.get(count - 1);
685             switch (channelId) {
686                 case CHANNEL_ALERT_EVENT:
687                     state = getStringTypeState(alert.event);
688                     break;
689                 case CHANNEL_ALERT_DESCRIPTION:
690                     state = getStringTypeState(alert.description);
691                     break;
692                 case CHANNEL_ALERT_ONSET:
693                     state = getDateTimeTypeState(alert.start);
694                     break;
695                 case CHANNEL_ALERT_EXPIRES:
696                     state = getDateTimeTypeState(alert.end);
697                     break;
698                 case CHANNEL_ALERT_SOURCE:
699                     state = getStringTypeState(alert.senderName);
700                     break;
701             }
702             logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
703         } else {
704             logger.debug("No data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
705         }
706         updateState(channelUID, state);
707     }
708 }