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.dwdunwetter.internal.handler;
15 import static org.openhab.binding.dwdunwetter.internal.DwdUnwetterBindingConstants.*;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21 import java.util.stream.Collectors;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.dwdunwetter.internal.config.DwdUnwetterConfiguration;
26 import org.openhab.binding.dwdunwetter.internal.dto.DwdWarningsData;
27 import org.openhab.core.library.types.DateTimeType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerCallback;
35 import org.openhab.core.thing.binding.builder.ThingBuilder;
36 import org.openhab.core.thing.type.ChannelTypeUID;
37 import org.openhab.core.thing.util.ThingHandlerHelper;
38 import org.openhab.core.types.Command;
39 import org.openhab.core.types.RefreshType;
40 import org.openhab.core.types.State;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@link DwdUnwetterHandler} is responsible for handling commands, which are sent to one of the channels.
47 * @author Martin Koehler - Initial contribution
50 public class DwdUnwetterHandler extends BaseThingHandler {
52 private final Logger logger = LoggerFactory.getLogger(DwdUnwetterHandler.class);
54 private @Nullable ScheduledFuture<?> refreshJob;
55 private int warningCount;
56 private @Nullable DwdWarningsData data;
58 private boolean inRefresh;
60 public DwdUnwetterHandler(Thing thing) {
65 public void handleCommand(ChannelUID channelUID, Command command) {
66 if (command instanceof RefreshType) {
67 scheduler.submit(this::refresh);
72 * Refreshes the Warning Data.
74 * The Switch Channel is switched to ON only after all other Channels are updated.
75 * The Switch Channel is switched to OFF before all other Channels are updated.
77 private void refresh() {
79 logger.trace("Already refreshing. Ignoring refresh request.");
83 if (!ThingHandlerHelper.isHandlerInitialized(getThing())) {
84 logger.debug("Unable to refresh. Thing status is '{}'", getThing().getStatus());
88 final DwdWarningsData warningsData = data;
89 if (warningsData == null) {
90 logger.debug("Unable to refresh. No data to use.");
96 boolean refreshSucceeded = warningsData.refresh();
97 if (!refreshSucceeded) {
98 logger.debug("Failed to retrieve new data from the server.");
103 updateStatus(ThingStatus.ONLINE);
105 updateState(getChannelUuid(CHANNEL_LAST_UPDATED), new DateTimeType());
107 for (int i = 0; i < warningCount; i++) {
108 State warning = warningsData.getWarning(i);
109 int warningNumber = i + 1;
110 if (warning == OnOffType.OFF) {
111 updateState(getChannelUuid(CHANNEL_WARNING, warningNumber), warning);
113 updateState(getChannelUuid(CHANNEL_SEVERITY, warningNumber), warningsData.getSeverity(i));
114 updateState(getChannelUuid(CHANNEL_DESCRIPTION, warningNumber), warningsData.getDescription(i));
115 updateState(getChannelUuid(CHANNEL_EFFECTIVE, warningNumber), warningsData.getEffective(i));
116 updateState(getChannelUuid(CHANNEL_EXPIRES, warningNumber), warningsData.getExpires(i));
117 updateState(getChannelUuid(CHANNEL_ONSET, warningNumber), warningsData.getOnset(i));
118 updateState(getChannelUuid(CHANNEL_EVENT, warningNumber), warningsData.getEvent(i));
119 updateState(getChannelUuid(CHANNEL_HEADLINE, warningNumber), warningsData.getHeadline(i));
120 updateState(getChannelUuid(CHANNEL_ALTITUDE, warningNumber), warningsData.getAltitude(i));
121 updateState(getChannelUuid(CHANNEL_CEILING, warningNumber), warningsData.getCeiling(i));
122 updateState(getChannelUuid(CHANNEL_INSTRUCTION, warningNumber), warningsData.getInstruction(i));
123 updateState(getChannelUuid(CHANNEL_URGENCY, warningNumber), warningsData.getUrgency(i));
124 if (warning == OnOffType.ON) {
125 updateState(getChannelUuid(CHANNEL_WARNING, warningNumber), warning);
127 if (warningsData.isNew(i)) {
128 triggerChannel(getChannelUuid(CHANNEL_UPDATED, warningNumber), "NEW");
132 warningsData.updateCache();
137 public void initialize() {
138 logger.debug("Start initializing!");
139 updateStatus(ThingStatus.UNKNOWN);
141 DwdUnwetterConfiguration config = getConfigAs(DwdUnwetterConfiguration.class);
142 int newWarningCount = config.warningCount;
144 if (warningCount != newWarningCount) {
145 List<Channel> toBeAddedChannels = new ArrayList<>();
146 List<Channel> toBeRemovedChannels = new ArrayList<>();
147 if (warningCount > newWarningCount) {
148 for (int i = newWarningCount + 1; i <= warningCount; ++i) {
149 toBeRemovedChannels.addAll(removeChannels(i));
152 for (int i = warningCount + 1; i <= newWarningCount; ++i) {
153 toBeAddedChannels.addAll(createChannels(i));
156 warningCount = newWarningCount;
158 ThingBuilder builder = editThing().withoutChannels(toBeRemovedChannels);
159 for (Channel channel : toBeAddedChannels) {
160 builder.withChannel(channel);
162 updateThing(builder.build());
165 data = new DwdWarningsData(config.cellId);
167 refreshJob = scheduler.scheduleWithFixedDelay(this::refresh, 0, config.refresh, TimeUnit.MINUTES);
169 logger.debug("Finished initializing!");
172 private ChannelUID getChannelUuid(String typeId, int warningNumber) {
173 return new ChannelUID(getThing().getUID(), typeId + warningNumber);
176 private ChannelUID getChannelUuid(String typeId) {
177 return new ChannelUID(getThing().getUID(), typeId);
181 * Creates a normal, state based, channel associated with a warning.
183 private void createChannelIfNotExist(ThingHandlerCallback cb, List<Channel> channels, String typeId, String label,
185 ChannelUID channelUID = getChannelUuid(typeId, warningNumber);
186 Channel existingChannel = getThing().getChannel(channelUID);
187 if (existingChannel != null) {
188 logger.trace("Thing '{}' already has an existing channel '{}'. Omit adding new channel '{}'.",
189 getThing().getUID(), existingChannel.getUID(), channelUID);
191 channels.add(cb.createChannelBuilder(channelUID, new ChannelTypeUID(BINDING_ID, typeId))
192 .withLabel(label + " " + getChannelLabelSuffix(warningNumber)).build());
196 private String getChannelLabelSuffix(int warningNumber) {
197 return "(" + warningNumber + ")";
201 * Creates the Channels for each warning.
203 * @return The List of Channels to be added
205 private List<Channel> createChannels(int warningNumber) {
206 logger.debug("Building channels for thing '{}'.", getThing().getUID());
207 List<Channel> channels = new ArrayList<>();
208 ThingHandlerCallback callback = getCallback();
209 if (callback != null) {
210 createChannelIfNotExist(callback, channels, CHANNEL_UPDATED, "Updated", warningNumber);
211 createChannelIfNotExist(callback, channels, CHANNEL_WARNING, "Warning", warningNumber);
212 createChannelIfNotExist(callback, channels, CHANNEL_SEVERITY, "Severity", warningNumber);
213 createChannelIfNotExist(callback, channels, CHANNEL_DESCRIPTION, "Description", warningNumber);
214 createChannelIfNotExist(callback, channels, CHANNEL_EFFECTIVE, "Issued", warningNumber);
215 createChannelIfNotExist(callback, channels, CHANNEL_ONSET, "Valid From", warningNumber);
216 createChannelIfNotExist(callback, channels, CHANNEL_EXPIRES, "Valid To", warningNumber);
217 createChannelIfNotExist(callback, channels, CHANNEL_EVENT, "Type", warningNumber);
218 createChannelIfNotExist(callback, channels, CHANNEL_HEADLINE, "Headline", warningNumber);
219 createChannelIfNotExist(callback, channels, CHANNEL_ALTITUDE, "Height (from)", warningNumber);
220 createChannelIfNotExist(callback, channels, CHANNEL_CEILING, "Height (to)", warningNumber);
221 createChannelIfNotExist(callback, channels, CHANNEL_INSTRUCTION, "Instruction", warningNumber);
222 createChannelIfNotExist(callback, channels, CHANNEL_URGENCY, "Urgency", warningNumber);
228 * Filters the Channels for each warning
230 * @return The List of Channels to be removed
232 @SuppressWarnings("null")
233 private List<Channel> removeChannels(int warningNumber) {
234 return getThing().getChannels().stream()
235 .filter(channel -> channel.getLabel() != null
236 && channel.getLabel().endsWith(getChannelLabelSuffix(warningNumber)))
237 .collect(Collectors.toList());
241 public void dispose() {
242 final ScheduledFuture<?> job = refreshJob;