2 * Copyright (c) 2010-2023 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.openweathermap.internal.handler;
15 import static org.openhab.binding.openweathermap.internal.OpenWeatherMapBindingConstants.*;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.openweathermap.internal.config.OpenWeatherMapUVIndexConfiguration;
25 import org.openhab.binding.openweathermap.internal.connection.OpenWeatherMapConnection;
26 import org.openhab.binding.openweathermap.internal.dto.OpenWeatherMapJsonUVIndexData;
27 import org.openhab.core.i18n.CommunicationException;
28 import org.openhab.core.i18n.ConfigurationException;
29 import org.openhab.core.i18n.TimeZoneProvider;
30 import org.openhab.core.thing.Channel;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingStatus;
34 import org.openhab.core.thing.ThingStatusDetail;
35 import org.openhab.core.thing.binding.builder.ThingBuilder;
36 import org.openhab.core.types.State;
37 import org.openhab.core.types.UnDefType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.gson.JsonSyntaxException;
44 * The {@link OpenWeatherMapUVIndexHandler} is responsible for handling commands, which are sent to one of the
47 * @author Christoph Weitkamp - Initial contribution
50 public class OpenWeatherMapUVIndexHandler extends AbstractOpenWeatherMapHandler {
52 private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapUVIndexHandler.class);
54 private static final String CHANNEL_GROUP_FORECAST_PREFIX = "forecastDay";
55 private static final Pattern CHANNEL_GROUP_FORECAST_PREFIX_PATTERN = Pattern
56 .compile(CHANNEL_GROUP_FORECAST_PREFIX + "([0-9]*)");
58 // keeps track of the parsed count
59 private int forecastDays = 6;
61 private @Nullable OpenWeatherMapJsonUVIndexData uvindexData;
62 private @Nullable List<OpenWeatherMapJsonUVIndexData> uvindexForecastData;
64 public OpenWeatherMapUVIndexHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
65 super(thing, timeZoneProvider);
69 public void initialize() {
71 logger.debug("Initialize OpenWeatherMapUVIndexHandler handler '{}'.", getThing().getUID());
72 OpenWeatherMapUVIndexConfiguration config = getConfigAs(OpenWeatherMapUVIndexConfiguration.class);
74 boolean configValid = true;
75 int newForecastDays = config.forecastDays;
76 if (newForecastDays < 1 || newForecastDays > 8) {
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
78 "@text/offline.conf-error-not-supported-uvindex-number-of-days");
83 logger.debug("Rebuilding thing '{}'.", getThing().getUID());
84 List<Channel> toBeAddedChannels = new ArrayList<>();
85 List<Channel> toBeRemovedChannels = new ArrayList<>();
86 if (forecastDays != newForecastDays) {
87 logger.debug("Rebuilding UV index channel groups.");
88 if (forecastDays > newForecastDays) {
89 if (newForecastDays < 2) {
90 toBeRemovedChannels.addAll(removeChannelsOfGroup(CHANNEL_GROUP_FORECAST_TOMORROW));
92 for (int i = newForecastDays; i < forecastDays; ++i) {
94 .addAll(removeChannelsOfGroup(CHANNEL_GROUP_FORECAST_PREFIX + Integer.toString(i)));
97 if (forecastDays <= 1 && newForecastDays > 1) {
98 toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_FORECAST_TOMORROW,
99 CHANNEL_GROUP_TYPE_UVINDEX_FORECAST));
101 for (int i = (forecastDays < 2) ? 2 : forecastDays; i < newForecastDays; ++i) {
103 .addAll(createChannelsForGroup(CHANNEL_GROUP_FORECAST_PREFIX + Integer.toString(i),
104 CHANNEL_GROUP_TYPE_UVINDEX_FORECAST));
107 forecastDays = newForecastDays;
109 ThingBuilder builder = editThing().withoutChannels(toBeRemovedChannels);
110 for (Channel channel : toBeAddedChannels) {
111 builder.withChannel(channel);
113 updateThing(builder.build());
118 protected boolean requestData(OpenWeatherMapConnection connection)
119 throws CommunicationException, ConfigurationException {
120 logger.debug("Update UV Index data of thing '{}'.", getThing().getUID());
122 uvindexData = connection.getUVIndexData(location);
123 if (forecastDays > 0) {
124 uvindexForecastData = connection.getUVIndexForecastData(location, forecastDays);
127 } catch (JsonSyntaxException e) {
128 logger.debug("JsonSyntaxException occurred during execution: {}", e.getMessage(), e);
134 protected void updateChannel(ChannelUID channelUID) {
135 switch (channelUID.getGroupId()) {
136 case CHANNEL_GROUP_CURRENT_UVINDEX:
137 updateUVIndexChannel(channelUID);
139 case CHANNEL_GROUP_FORECAST_TOMORROW:
140 updateUVIndexForecastChannel(channelUID, 1);
143 Matcher m = CHANNEL_GROUP_FORECAST_PREFIX_PATTERN.matcher(channelUID.getGroupId());
145 if (m.find() && (i = Integer.parseInt(m.group(1))) > 1 && i <= 8) {
146 updateUVIndexForecastChannel(channelUID, i);
153 * Update the channel from the last OpenWeatherMap data retrieved.
155 * @param channelUID the id identifying the channel to be updated
157 private void updateUVIndexChannel(ChannelUID channelUID) {
158 String channelId = channelUID.getIdWithoutGroup();
159 String channelGroupId = channelUID.getGroupId();
160 OpenWeatherMapJsonUVIndexData localUVIndexData = uvindexData;
161 if (localUVIndexData != null) {
162 State state = UnDefType.UNDEF;
164 case CHANNEL_TIME_STAMP:
165 state = getDateTimeTypeState(localUVIndexData.getDate());
167 case CHANNEL_UVINDEX:
168 state = getDecimalTypeState(localUVIndexData.getValue());
171 logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
172 updateState(channelUID, state);
174 logger.debug("No UV Index data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
179 * Update the channel from the last OpenWeatherMap data retrieved.
181 * @param channelUID the id identifying the channel to be updated
184 private void updateUVIndexForecastChannel(ChannelUID channelUID, int count) {
185 String channelId = channelUID.getIdWithoutGroup();
186 String channelGroupId = channelUID.getGroupId();
187 List<OpenWeatherMapJsonUVIndexData> localUVIndexForecastData = uvindexForecastData;
188 if (localUVIndexForecastData != null && localUVIndexForecastData.size() >= count) {
189 OpenWeatherMapJsonUVIndexData forecastData = localUVIndexForecastData.get(count - 1);
190 State state = UnDefType.UNDEF;
192 case CHANNEL_TIME_STAMP:
193 state = getDateTimeTypeState(forecastData.getDate());
195 case CHANNEL_UVINDEX:
196 state = getDecimalTypeState(forecastData.getValue());
199 logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
200 updateState(channelUID, state);
202 logger.debug("No UV Index data available to update channel '{}' of group '{}'.", channelId, channelGroupId);