[openuv] adressing issue #12688 (#12695)

* openuv adressing issue #12688

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2022-05-07 20:37:03 +02:00 committed by GitHub
parent cb60d891fb
commit f6ab246a41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 24 deletions

View File

@ -17,6 +17,7 @@ import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
@ -67,7 +68,7 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
private final TranslationProvider i18nProvider;
private final LocaleProvider localeProvider;
private @Nullable ScheduledFuture<?> reconnectJob;
private Optional<ScheduledFuture<?>> reconnectJob = Optional.empty();
public OpenUVBridgeHandler(Bridge bridge, LocationProvider locationProvider, TranslationProvider i18nProvider,
LocaleProvider localeProvider, Gson gson) {
@ -93,11 +94,8 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
@Override
public void dispose() {
ScheduledFuture<?> job = this.reconnectJob;
if (job != null && !job.isCancelled()) {
job.cancel(true);
}
reconnectJob = null;
reconnectJob.ifPresent(job -> job.cancel(true));
reconnectJob = Optional.empty();
}
@Override
@ -129,7 +127,8 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
throw new OpenUVException(error);
}
} catch (JsonSyntaxException e) {
logger.debug("No valid json received when calling `{}` : {}", url, jsonData);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
String.format("Invalid json received when calling `%s` : %s", url, jsonData));
} catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
} catch (OpenUVException e) {
@ -139,8 +138,8 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, String
.format("@text/offline.comm-error-quota-exceeded [ \"%s\" ]", tomorrowMidnight.toString()));
reconnectJob = scheduler.schedule(this::initiateConnexion,
Duration.between(LocalDateTime.now(), tomorrowMidnight).toMinutes(), TimeUnit.MINUTES);
reconnectJob = Optional.of(scheduler.schedule(this::initiateConnexion,
Duration.between(LocalDateTime.now(), tomorrowMidnight).toMinutes(), TimeUnit.MINUTES));
} else {
updateStatus(ThingStatus.OFFLINE,
e.isApiKeyError() ? ThingStatusDetail.CONFIGURATION_ERROR : ThingStatusDetail.NONE,

View File

@ -40,6 +40,7 @@ import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.ThingStatusInfo;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.thing.type.ChannelTypeUID;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State;
@ -208,10 +209,11 @@ public class OpenUVReportHandler extends BaseThingHandler {
return openUVData.getUVMaxTime();
case UV_TIME:
return openUVData.getUVTime();
case SAFE_EXPOSURE:
SafeExposureConfiguration configuration = channel.getConfiguration()
.as(SafeExposureConfiguration.class);
return openUVData.getSafeExposureTime(configuration.index);
}
ChannelTypeUID channelType = channel.getChannelTypeUID();
if (channelType != null && SAFE_EXPOSURE.equals(channelType.getId())) {
SafeExposureConfiguration configuration = channel.getConfiguration().as(SafeExposureConfiguration.class);
return openUVData.getSafeExposureTime(configuration.index);
}
return UnDefType.NULL;
}

View File

@ -29,8 +29,7 @@ import org.slf4j.LoggerFactory;
import com.google.gson.annotations.SerializedName;
/**
* The {@link OpenUVResult} is responsible for storing
* the "result" node from the OpenUV JSON response
* The {@link OpenUVResult} is responsible for storing the result node from the OpenUV JSON response
*
* @author Gaël L'hopital - Initial contribution
*/
@ -54,10 +53,10 @@ public class OpenUVResult {
}
private double uv;
private @Nullable ZonedDateTime uvTime;
private double uvMax;
private @Nullable ZonedDateTime uvMaxTime;
private double ozone;
private @Nullable ZonedDateTime uvTime;
private @Nullable ZonedDateTime uvMaxTime;
private @Nullable ZonedDateTime ozoneTime;
private Map<FitzpatrickType, @Nullable Integer> safeExposureTime = new HashMap<>();
@ -73,19 +72,20 @@ public class OpenUVResult {
return ozone;
}
private State getValueOrNull(@Nullable ZonedDateTime value) {
return value == null ? UnDefType.NULL : new DateTimeType(value);
}
public State getUVTime() {
ZonedDateTime value = uvTime;
return value != null ? new DateTimeType(value) : UnDefType.NULL;
return getValueOrNull(uvTime);
}
public State getUVMaxTime() {
ZonedDateTime value = uvMaxTime;
return value != null ? new DateTimeType(value) : UnDefType.NULL;
return getValueOrNull(uvMaxTime);
}
public State getOzoneTime() {
ZonedDateTime value = ozoneTime;
return value != null ? new DateTimeType(value) : UnDefType.NULL;
return getValueOrNull(ozoneTime);
}
public State getSafeExposureTime(String index) {
@ -93,7 +93,7 @@ public class OpenUVResult {
FitzpatrickType value = FitzpatrickType.valueOf(index);
Integer duration = safeExposureTime.get(value);
if (duration != null) {
return new QuantityType<>(duration, Units.MINUTE);
return QuantityType.valueOf(duration, Units.MINUTE);
}
} catch (IllegalArgumentException e) {
logger.warn("Unexpected Fitzpatrick index value '{}' : {}", index, e.getMessage());