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.json;
15 import java.time.ZonedDateTime;
16 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.library.types.DateTimeType;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.library.unit.Units;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.UnDefType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.google.gson.annotations.SerializedName;
32 * The {@link OpenUVResult} is responsible for storing the result node from the OpenUV JSON response
34 * @author Gaƫl L'hopital - Initial contribution
37 public class OpenUVResult {
38 private final Logger logger = LoggerFactory.getLogger(OpenUVResult.class);
40 public enum FitzpatrickType {
41 @SerializedName("st1")
42 I, // Fitzpatrick Skin Type I
43 @SerializedName("st2")
44 II, // Fitzpatrick Skin Type II
45 @SerializedName("st3")
46 III, // Fitzpatrick Skin Type III
47 @SerializedName("st4")
48 IV, // Fitzpatrick Skin Type IV
49 @SerializedName("st5")
50 V, // Fitzpatrick Skin Type V
51 @SerializedName("st6")
52 VI;// Fitzpatrick Skin Type VI
58 private @Nullable ZonedDateTime uvTime;
59 private @Nullable ZonedDateTime uvMaxTime;
60 private @Nullable ZonedDateTime ozoneTime;
61 private Map<FitzpatrickType, @Nullable Integer> safeExposureTime = new HashMap<>();
63 public double getUv() {
67 public double getUvMax() {
71 public double getOzone() {
75 private State getValueOrNull(@Nullable ZonedDateTime value) {
76 return value == null ? UnDefType.NULL : new DateTimeType(value);
79 public State getUVTime() {
80 return getValueOrNull(uvTime);
83 public State getUVMaxTime() {
84 return getValueOrNull(uvMaxTime);
87 public State getOzoneTime() {
88 return getValueOrNull(ozoneTime);
91 public State getSafeExposureTime(String index) {
93 FitzpatrickType value = FitzpatrickType.valueOf(index);
94 Integer duration = safeExposureTime.get(value);
95 if (duration != null) {
96 return QuantityType.valueOf(duration, Units.MINUTE);
98 } catch (IllegalArgumentException e) {
99 logger.warn("Unexpected Fitzpatrick index value '{}' : {}", index, e.getMessage());
101 return UnDefType.NULL;