]> git.basschouten.com Git - openhab-addons.git/blob
70a6cf6d30c153828ce00bc327f720ebcf2ee35b
[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.json;
14
15 import java.time.ZonedDateTime;
16 import java.util.Map;
17
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;
26
27 import com.google.gson.annotations.SerializedName;
28
29 /**
30  * The {@link OpenUVResult} is responsible for storing the result node from the OpenUV JSON response
31  *
32  * @author GaĆ«l L'hopital - Initial contribution
33  */
34 @NonNullByDefault
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
49     }
50
51     private double uv;
52     private double uvMax;
53     private double ozone;
54     private @Nullable ZonedDateTime uvTime;
55     private @Nullable ZonedDateTime uvMaxTime;
56     private @Nullable ZonedDateTime ozoneTime;
57     private Map<FitzpatrickType, @Nullable Integer> safeExposureTime = Map.of();
58
59     public double getUv() {
60         return uv;
61     }
62
63     public double getUvMax() {
64         return uvMax;
65     }
66
67     public double getOzone() {
68         return ozone;
69     }
70
71     private State getValueOrNull(@Nullable ZonedDateTime value) {
72         return value == null ? UnDefType.NULL : new DateTimeType(value);
73     }
74
75     public State getUVTime() {
76         return getValueOrNull(uvTime);
77     }
78
79     public State getUVMaxTime() {
80         return getValueOrNull(uvMaxTime);
81     }
82
83     public State getOzoneTime() {
84         return getValueOrNull(ozoneTime);
85     }
86
87     public State getSafeExposureTime(String index) throws OpenUVException {
88         try {
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);
93         }
94     }
95 }