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;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.openuv.internal.OpenUVException;
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;
27 import com.google.gson.annotations.SerializedName;
30 * The {@link OpenUVResult} is responsible for storing the result node from the OpenUV JSON response
32 * @author Gaƫl L'hopital - Initial contribution
35 public class OpenUVResult {
36 public enum FitzpatrickType {
37 @SerializedName("st1")
38 I, // Fitzpatrick Skin Type I
39 @SerializedName("st2")
40 II, // Fitzpatrick Skin Type II
41 @SerializedName("st3")
42 III, // Fitzpatrick Skin Type III
43 @SerializedName("st4")
44 IV, // Fitzpatrick Skin Type IV
45 @SerializedName("st5")
46 V, // Fitzpatrick Skin Type V
47 @SerializedName("st6")
48 VI;// Fitzpatrick Skin Type VI
54 private @Nullable ZonedDateTime uvTime;
55 private @Nullable ZonedDateTime uvMaxTime;
56 private @Nullable ZonedDateTime ozoneTime;
57 private Map<FitzpatrickType, @Nullable Integer> safeExposureTime = Map.of();
59 public double getUv() {
63 public double getUvMax() {
67 public double getOzone() {
71 private State getValueOrNull(@Nullable ZonedDateTime value) {
72 return value == null ? UnDefType.NULL : new DateTimeType(value);
75 public State getUVTime() {
76 return getValueOrNull(uvTime);
79 public State getUVMaxTime() {
80 return getValueOrNull(uvMaxTime);
83 public State getOzoneTime() {
84 return getValueOrNull(ozoneTime);
87 public State getSafeExposureTime(String index) throws OpenUVException {
89 Integer duration = safeExposureTime.get(FitzpatrickType.valueOf(index));
90 return duration != null ? QuantityType.valueOf(duration, Units.MINUTE) : UnDefType.NULL;
91 } catch (IllegalArgumentException e) {
92 throw new OpenUVException(index, e);