]> git.basschouten.com Git - openhab-addons.git/blob
826111e48b0e0696fa45c23040731483f49e19e8
[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.openuv.internal.handler;
14
15 import static org.openhab.binding.openuv.internal.OpenUVBindingConstants.*;
16
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;
22
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;
51
52 /**
53  * The {@link OpenUVReportHandler} is responsible for handling commands, which are
54  * sent to one of the channels.
55  *
56  * @author GaĆ«l L'hopital - Initial contribution
57  */
58 @NonNullByDefault
59 public class OpenUVReportHandler extends BaseThingHandler {
60
61     private final Logger logger = LoggerFactory.getLogger(OpenUVReportHandler.class);
62
63     private @NonNullByDefault({}) OpenUVBridgeHandler bridgeHandler;
64     private @Nullable ScheduledFuture<?> refreshJob;
65     private @Nullable ScheduledFuture<?> uvMaxJob;
66     private boolean suspendUpdates = false;
67
68     public OpenUVReportHandler(Thing thing) {
69         super(thing);
70     }
71
72     @Override
73     public void initialize() {
74         logger.debug("Initializing OpenUV handler.");
75
76         ReportConfiguration config = getConfigAs(ReportConfiguration.class);
77
78         if (config.refresh < 3) {
79             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
80                     "@text/offline.config-error-invalid-refresh");
81         } else {
82             Bridge bridge = getBridge();
83             if (bridge == null) {
84                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
85             } else {
86                 bridgeHandler = (OpenUVBridgeHandler) bridge.getHandler();
87                 updateStatus(ThingStatus.UNKNOWN);
88                 startAutomaticRefresh();
89             }
90         }
91     }
92
93     /**
94      * Start the job screening UV Max reached
95      *
96      * @param openUVData
97      */
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());
105                 if (timeDiff > 0) {
106                     logger.debug("Scheduling {} in {} minutes", UV_MAX_EVENT, timeDiff);
107                     uvMaxJob = scheduler.schedule(() -> {
108                         triggerChannel(UV_MAX_EVENT);
109                         uvMaxJob = null;
110                     }, timeDiff, TimeUnit.MINUTES);
111                 }
112             }
113         }
114     }
115
116     /**
117      * Start the job refreshing the data
118      */
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));
126                 }
127             }, 0, config.refresh, TimeUnit.MINUTES);
128         }
129     }
130
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);
141             } else {
142                 updateStatus(ThingStatus.OFFLINE, bridgeStatusInfo.getStatusDetail(),
143                         bridgeStatusInfo.getDescription());
144             }
145         }
146     }
147
148     @Override
149     public void dispose() {
150         logger.debug("Disposing the OpenUV handler.");
151
152         cancelFuture(refreshJob);
153         refreshJob = null;
154
155         cancelFuture(uvMaxJob);
156         uvMaxJob = null;
157     }
158
159     private void cancelFuture(@Nullable ScheduledFuture<?> job) {
160         if (job != null && !job.isCancelled()) {
161             job.cancel(true);
162         }
163     }
164
165     @Override
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));
171             });
172         } else if (ELEVATION.equals(channelUID.getId()) && command instanceof QuantityType<?> qtty) {
173             if (Units.DEGREE_ANGLE.equals(qtty.getUnit())) {
174                 suspendUpdates = qtty.doubleValue() < 0;
175             } else {
176                 logger.info("The OpenUV Report handles Sun Elevation of Number:Angle type, {} does not fit.", command);
177             }
178         } else {
179             logger.info("The OpenUV Report Thing handles Refresh or Sun Elevation command and not '{}'", command);
180         }
181     }
182
183     private State getState(Channel channel, OpenUVResult openUVData) {
184         ChannelUID uid = channel.getUID();
185         switch (uid.getId()) {
186             case UV_INDEX:
187                 return new DecimalType(openUVData.getUv());
188             case ALERT_LEVEL:
189                 return AlertLevel.fromUVIndex(openUVData.getUv()).state;
190             case UV_COLOR:
191                 return hexToHSB(AlertLevel.fromUVIndex(openUVData.getUv()).color);
192             case UV_MAX:
193                 return new DecimalType(openUVData.getUvMax());
194             case OZONE:
195                 return new QuantityType<>(openUVData.getOzone(), Units.DOBSON_UNIT);
196             case OZONE_TIME:
197                 return openUVData.getOzoneTime();
198             case UV_MAX_TIME:
199                 return openUVData.getUVMaxTime();
200             case UV_TIME:
201                 return openUVData.getUVTime();
202         }
203
204         ChannelTypeUID channelType = channel.getChannelTypeUID();
205         if (channelType != null && SAFE_EXPOSURE.equals(channelType.getId())) {
206             SafeExposureConfiguration configuration = channel.getConfiguration().as(SafeExposureConfiguration.class);
207             try {
208                 return openUVData.getSafeExposureTime(configuration.index);
209             } catch (OpenUVException e) {
210                 logger.warn("Error getting safe exposure value : {}", e.getMessage());
211             }
212         }
213
214         return UnDefType.NULL;
215     }
216
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);
222     }
223 }