]> git.basschouten.com Git - openhab-addons.git/blob
e9075b3cdb55e6f8cb26ba45b586c67e06d5276a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
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;
40
41 import com.google.gson.JsonSyntaxException;
42
43 /**
44  * The {@link OpenWeatherMapUVIndexHandler} is responsible for handling commands, which are sent to one of the
45  * channels.
46  *
47  * @author Christoph Weitkamp - Initial contribution
48  */
49 @NonNullByDefault
50 public class OpenWeatherMapUVIndexHandler extends AbstractOpenWeatherMapHandler {
51
52     private final Logger logger = LoggerFactory.getLogger(OpenWeatherMapUVIndexHandler.class);
53
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]*)");
57
58     // keeps track of the parsed count
59     private int forecastDays = 6;
60
61     private @Nullable OpenWeatherMapJsonUVIndexData uvindexData;
62     private @Nullable List<OpenWeatherMapJsonUVIndexData> uvindexForecastData;
63
64     public OpenWeatherMapUVIndexHandler(Thing thing, final TimeZoneProvider timeZoneProvider) {
65         super(thing, timeZoneProvider);
66     }
67
68     @Override
69     public void initialize() {
70         super.initialize();
71         logger.debug("Initialize OpenWeatherMapUVIndexHandler handler '{}'.", getThing().getUID());
72         OpenWeatherMapUVIndexConfiguration config = getConfigAs(OpenWeatherMapUVIndexConfiguration.class);
73
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");
79             configValid = false;
80         }
81
82         if (configValid) {
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));
91                     }
92                     for (int i = newForecastDays; i < forecastDays; ++i) {
93                         toBeRemovedChannels
94                                 .addAll(removeChannelsOfGroup(CHANNEL_GROUP_FORECAST_PREFIX + Integer.toString(i)));
95                     }
96                 } else {
97                     if (forecastDays <= 1 && newForecastDays > 1) {
98                         toBeAddedChannels.addAll(createChannelsForGroup(CHANNEL_GROUP_FORECAST_TOMORROW,
99                                 CHANNEL_GROUP_TYPE_UVINDEX_FORECAST));
100                     }
101                     for (int i = (forecastDays < 2) ? 2 : forecastDays; i < newForecastDays; ++i) {
102                         toBeAddedChannels
103                                 .addAll(createChannelsForGroup(CHANNEL_GROUP_FORECAST_PREFIX + Integer.toString(i),
104                                         CHANNEL_GROUP_TYPE_UVINDEX_FORECAST));
105                     }
106                 }
107                 forecastDays = newForecastDays;
108             }
109             ThingBuilder builder = editThing().withoutChannels(toBeRemovedChannels);
110             for (Channel channel : toBeAddedChannels) {
111                 builder.withChannel(channel);
112             }
113             updateThing(builder.build());
114         }
115     }
116
117     @Override
118     protected boolean requestData(OpenWeatherMapConnection connection)
119             throws CommunicationException, ConfigurationException {
120         logger.debug("Update UV Index data of thing '{}'.", getThing().getUID());
121         try {
122             uvindexData = connection.getUVIndexData(location);
123             if (forecastDays > 0) {
124                 uvindexForecastData = connection.getUVIndexForecastData(location, forecastDays);
125             }
126             return true;
127         } catch (JsonSyntaxException e) {
128             logger.debug("JsonSyntaxException occurred during execution: {}", e.getMessage(), e);
129             return false;
130         }
131     }
132
133     @Override
134     protected void updateChannel(ChannelUID channelUID) {
135         switch (channelUID.getGroupId()) {
136             case CHANNEL_GROUP_CURRENT_UVINDEX:
137                 updateUVIndexChannel(channelUID);
138                 break;
139             case CHANNEL_GROUP_FORECAST_TOMORROW:
140                 updateUVIndexForecastChannel(channelUID, 1);
141                 break;
142             default:
143                 Matcher m = CHANNEL_GROUP_FORECAST_PREFIX_PATTERN.matcher(channelUID.getGroupId());
144                 int i;
145                 if (m.find() && (i = Integer.parseInt(m.group(1))) > 1 && i <= 8) {
146                     updateUVIndexForecastChannel(channelUID, i);
147                 }
148                 break;
149         }
150     }
151
152     /**
153      * Update the channel from the last OpenWeatherMap data retrieved.
154      *
155      * @param channelUID the id identifying the channel to be updated
156      */
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;
163             switch (channelId) {
164                 case CHANNEL_TIME_STAMP:
165                     state = getDateTimeTypeState(localUVIndexData.getDate());
166                     break;
167                 case CHANNEL_UVINDEX:
168                     state = getDecimalTypeState(localUVIndexData.getValue());
169                     break;
170             }
171             logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
172             updateState(channelUID, state);
173         } else {
174             logger.debug("No UV Index data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
175         }
176     }
177
178     /**
179      * Update the channel from the last OpenWeatherMap data retrieved.
180      *
181      * @param channelUID the id identifying the channel to be updated
182      * @param count
183      */
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;
191             switch (channelId) {
192                 case CHANNEL_TIME_STAMP:
193                     state = getDateTimeTypeState(forecastData.getDate());
194                     break;
195                 case CHANNEL_UVINDEX:
196                     state = getDecimalTypeState(forecastData.getValue());
197                     break;
198             }
199             logger.debug("Update channel '{}' of group '{}' with new state '{}'.", channelId, channelGroupId, state);
200             updateState(channelUID, state);
201         } else {
202             logger.debug("No UV Index data available to update channel '{}' of group '{}'.", channelId, channelGroupId);
203         }
204     }
205 }