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.openuv.internal.handler;
15 import static org.openhab.binding.openuv.internal.OpenUVBindingConstants.*;
17 import java.time.ZoneId;
18 import java.time.ZonedDateTime;
19 import java.time.temporal.ChronoUnit;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.openuv.internal.AlertLevel;
26 import org.openhab.binding.openuv.internal.OpenUVException;
27 import org.openhab.binding.openuv.internal.config.ReportConfiguration;
28 import org.openhab.binding.openuv.internal.config.SafeExposureConfiguration;
29 import org.openhab.binding.openuv.internal.json.OpenUVResult;
30 import org.openhab.core.library.types.DateTimeType;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.HSBType;
33 import org.openhab.core.library.types.PointType;
34 import org.openhab.core.library.types.QuantityType;
35 import org.openhab.core.library.unit.Units;
36 import org.openhab.core.thing.Bridge;
37 import org.openhab.core.thing.Channel;
38 import org.openhab.core.thing.ChannelUID;
39 import org.openhab.core.thing.Thing;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingStatusDetail;
42 import org.openhab.core.thing.ThingStatusInfo;
43 import org.openhab.core.thing.binding.BaseThingHandler;
44 import org.openhab.core.thing.type.ChannelTypeUID;
45 import org.openhab.core.types.Command;
46 import org.openhab.core.types.RefreshType;
47 import org.openhab.core.types.State;
48 import org.openhab.core.types.UnDefType;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * The {@link OpenUVReportHandler} is responsible for handling commands, which are
54 * sent to one of the channels.
56 * @author Gaƫl L'hopital - Initial contribution
59 public class OpenUVReportHandler extends BaseThingHandler {
61 private final Logger logger = LoggerFactory.getLogger(OpenUVReportHandler.class);
63 private @NonNullByDefault({}) OpenUVBridgeHandler bridgeHandler;
64 private @Nullable ScheduledFuture<?> refreshJob;
65 private @Nullable ScheduledFuture<?> uvMaxJob;
66 private boolean suspendUpdates = false;
68 public OpenUVReportHandler(Thing thing) {
73 public void initialize() {
74 logger.debug("Initializing OpenUV handler.");
76 ReportConfiguration config = getConfigAs(ReportConfiguration.class);
78 if (config.refresh < 3) {
79 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
80 "@text/offline.config-error-invalid-refresh");
82 Bridge bridge = getBridge();
84 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
86 bridgeHandler = (OpenUVBridgeHandler) bridge.getHandler();
87 updateStatus(ThingStatus.UNKNOWN);
88 startAutomaticRefresh();
94 * Start the job screening UV Max reached
98 private void scheduleUVMaxEvent(OpenUVResult openUVData) {
99 ScheduledFuture<?> job = this.uvMaxJob;
100 if ((job == null || job.isCancelled())) {
101 State uvMaxTime = openUVData.getUVMaxTime();
102 if (uvMaxTime instanceof DateTimeType uvMaxDateTime) {
103 long timeDiff = ChronoUnit.MINUTES.between(ZonedDateTime.now(ZoneId.systemDefault()),
104 uvMaxDateTime.getZonedDateTime());
106 logger.debug("Scheduling {} in {} minutes", UV_MAX_EVENT, timeDiff);
107 uvMaxJob = scheduler.schedule(() -> {
108 triggerChannel(UV_MAX_EVENT);
110 }, timeDiff, TimeUnit.MINUTES);
117 * Start the job refreshing the data
119 private void startAutomaticRefresh() {
120 ScheduledFuture<?> job = this.refreshJob;
121 if (job == null || job.isCancelled()) {
122 ReportConfiguration config = getConfigAs(ReportConfiguration.class);
123 refreshJob = scheduler.scheduleWithFixedDelay(() -> {
124 if (!suspendUpdates) {
125 updateChannels(new PointType(config.location));
127 }, 0, config.refresh, TimeUnit.MINUTES);
131 private void updateChannels(PointType location) {
132 ThingStatusInfo bridgeStatusInfo = bridgeHandler.getThing().getStatusInfo();
133 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
134 OpenUVResult openUVData = bridgeHandler.getUVData(location.getLatitude().toBigDecimal(),
135 location.getLongitude().toBigDecimal(), location.getAltitude().toBigDecimal());
136 if (openUVData != null) {
137 scheduleUVMaxEvent(openUVData);
138 getThing().getChannels().stream().filter(channel -> isLinked(channel.getUID().getId()))
139 .forEach(channel -> updateState(channel.getUID(), getState(channel, openUVData)));
140 updateStatus(ThingStatus.ONLINE);
142 updateStatus(ThingStatus.OFFLINE, bridgeStatusInfo.getStatusDetail(),
143 bridgeStatusInfo.getDescription());
149 public void dispose() {
150 logger.debug("Disposing the OpenUV handler.");
152 cancelFuture(refreshJob);
155 cancelFuture(uvMaxJob);
159 private void cancelFuture(@Nullable ScheduledFuture<?> job) {
160 if (job != null && !job.isCancelled()) {
166 public void handleCommand(ChannelUID channelUID, Command command) {
167 if (command instanceof RefreshType) {
168 scheduler.execute(() -> {
169 ReportConfiguration config = getConfigAs(ReportConfiguration.class);
170 updateChannels(new PointType(config.location));
172 } else if (ELEVATION.equals(channelUID.getId()) && command instanceof QuantityType<?> qtty) {
173 if (Units.DEGREE_ANGLE.equals(qtty.getUnit())) {
174 suspendUpdates = qtty.doubleValue() < 0;
176 logger.info("The OpenUV Report handles Sun Elevation of Number:Angle type, {} does not fit.", command);
179 logger.info("The OpenUV Report Thing handles Refresh or Sun Elevation command and not '{}'", command);
183 private State getState(Channel channel, OpenUVResult openUVData) {
184 ChannelUID uid = channel.getUID();
185 switch (uid.getId()) {
187 return new DecimalType(openUVData.getUv());
189 return AlertLevel.fromUVIndex(openUVData.getUv()).state;
191 return hexToHSB(AlertLevel.fromUVIndex(openUVData.getUv()).color);
193 return new DecimalType(openUVData.getUvMax());
195 return new QuantityType<>(openUVData.getOzone(), Units.DOBSON_UNIT);
197 return openUVData.getOzoneTime();
199 return openUVData.getUVMaxTime();
201 return openUVData.getUVTime();
204 ChannelTypeUID channelType = channel.getChannelTypeUID();
205 if (channelType != null && SAFE_EXPOSURE.equals(channelType.getId())) {
206 SafeExposureConfiguration configuration = channel.getConfiguration().as(SafeExposureConfiguration.class);
208 return openUVData.getSafeExposureTime(configuration.index);
209 } catch (OpenUVException e) {
210 logger.warn("Error getting safe exposure value : {}", e.getMessage());
214 return UnDefType.NULL;
217 private State hexToHSB(String hexValue) {
218 int resultRed = Integer.valueOf(hexValue.substring(0, 2), 16);
219 int resultGreen = Integer.valueOf(hexValue.substring(2, 4), 16);
220 int resultBlue = Integer.valueOf(hexValue.substring(4, 6), 16);
221 return HSBType.fromRGB(resultRed, resultGreen, resultBlue);