[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.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Optional;
import java.util.Properties; import java.util.Properties;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
@ -67,7 +68,7 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
private final TranslationProvider i18nProvider; private final TranslationProvider i18nProvider;
private final LocaleProvider localeProvider; private final LocaleProvider localeProvider;
private @Nullable ScheduledFuture<?> reconnectJob; private Optional<ScheduledFuture<?>> reconnectJob = Optional.empty();
public OpenUVBridgeHandler(Bridge bridge, LocationProvider locationProvider, TranslationProvider i18nProvider, public OpenUVBridgeHandler(Bridge bridge, LocationProvider locationProvider, TranslationProvider i18nProvider,
LocaleProvider localeProvider, Gson gson) { LocaleProvider localeProvider, Gson gson) {
@ -93,11 +94,8 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
@Override @Override
public void dispose() { public void dispose() {
ScheduledFuture<?> job = this.reconnectJob; reconnectJob.ifPresent(job -> job.cancel(true));
if (job != null && !job.isCancelled()) { reconnectJob = Optional.empty();
job.cancel(true);
}
reconnectJob = null;
} }
@Override @Override
@ -129,7 +127,8 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
throw new OpenUVException(error); throw new OpenUVException(error);
} }
} catch (JsonSyntaxException e) { } 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) { } catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
} catch (OpenUVException e) { } catch (OpenUVException e) {
@ -139,8 +138,8 @@ public class OpenUVBridgeHandler extends BaseBridgeHandler {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, String updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, String
.format("@text/offline.comm-error-quota-exceeded [ \"%s\" ]", tomorrowMidnight.toString())); .format("@text/offline.comm-error-quota-exceeded [ \"%s\" ]", tomorrowMidnight.toString()));
reconnectJob = scheduler.schedule(this::initiateConnexion, reconnectJob = Optional.of(scheduler.schedule(this::initiateConnexion,
Duration.between(LocalDateTime.now(), tomorrowMidnight).toMinutes(), TimeUnit.MINUTES); Duration.between(LocalDateTime.now(), tomorrowMidnight).toMinutes(), TimeUnit.MINUTES));
} else { } else {
updateStatus(ThingStatus.OFFLINE, updateStatus(ThingStatus.OFFLINE,
e.isApiKeyError() ? ThingStatusDetail.CONFIGURATION_ERROR : ThingStatusDetail.NONE, 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.ThingStatusDetail;
import org.openhab.core.thing.ThingStatusInfo; import org.openhab.core.thing.ThingStatusInfo;
import org.openhab.core.thing.binding.BaseThingHandler; 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.Command;
import org.openhab.core.types.RefreshType; import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State; import org.openhab.core.types.State;
@ -208,10 +209,11 @@ public class OpenUVReportHandler extends BaseThingHandler {
return openUVData.getUVMaxTime(); return openUVData.getUVMaxTime();
case UV_TIME: case UV_TIME:
return openUVData.getUVTime(); return openUVData.getUVTime();
case SAFE_EXPOSURE: }
SafeExposureConfiguration configuration = channel.getConfiguration() ChannelTypeUID channelType = channel.getChannelTypeUID();
.as(SafeExposureConfiguration.class); if (channelType != null && SAFE_EXPOSURE.equals(channelType.getId())) {
return openUVData.getSafeExposureTime(configuration.index); SafeExposureConfiguration configuration = channel.getConfiguration().as(SafeExposureConfiguration.class);
return openUVData.getSafeExposureTime(configuration.index);
} }
return UnDefType.NULL; return UnDefType.NULL;
} }

View File

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