]> git.basschouten.com Git - openhab-addons.git/blob
a2b6bd61092f6987bf66cae349557f14cde2f7d3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.HashMap;
17 import java.util.Map;
18
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;
28
29 import com.google.gson.annotations.SerializedName;
30
31 /**
32  * The {@link OpenUVResult} is responsible for storing
33  * the "result" node from the OpenUV JSON response
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  */
37 @NonNullByDefault
38 public class OpenUVResult {
39     private final Logger logger = LoggerFactory.getLogger(OpenUVResult.class);
40
41     public enum FitzpatrickType {
42         @SerializedName("st1")
43         I, // Fitzpatrick Skin Type I
44         @SerializedName("st2")
45         II, // Fitzpatrick Skin Type II
46         @SerializedName("st3")
47         III, // Fitzpatrick Skin Type III
48         @SerializedName("st4")
49         IV, // Fitzpatrick Skin Type IV
50         @SerializedName("st5")
51         V, // Fitzpatrick Skin Type V
52         @SerializedName("st6")
53         VI;// Fitzpatrick Skin Type VI
54     }
55
56     private double uv;
57     private @Nullable ZonedDateTime uvTime;
58     private double uvMax;
59     private @Nullable ZonedDateTime uvMaxTime;
60     private double ozone;
61     private @Nullable ZonedDateTime ozoneTime;
62     private Map<FitzpatrickType, @Nullable Integer> safeExposureTime = new HashMap<>();
63
64     public double getUv() {
65         return uv;
66     }
67
68     public double getUvMax() {
69         return uvMax;
70     }
71
72     public double getOzone() {
73         return ozone;
74     }
75
76     public State getUVTime() {
77         ZonedDateTime value = uvTime;
78         return value != null ? new DateTimeType(value) : UnDefType.NULL;
79     }
80
81     public State getUVMaxTime() {
82         ZonedDateTime value = uvMaxTime;
83         return value != null ? new DateTimeType(value) : UnDefType.NULL;
84     }
85
86     public State getOzoneTime() {
87         ZonedDateTime value = ozoneTime;
88         return value != null ? new DateTimeType(value) : UnDefType.NULL;
89     }
90
91     public State getSafeExposureTime(String index) {
92         try {
93             FitzpatrickType value = FitzpatrickType.valueOf(index);
94             Integer duration = safeExposureTime.get(value);
95             if (duration != null) {
96                 return new QuantityType<>(duration, Units.MINUTE);
97             }
98         } catch (IllegalArgumentException e) {
99             logger.warn("Unexpected Fitzpatrick index value '{}' : {}", index, e.getMessage());
100         }
101         return UnDefType.NULL;
102     }
103 }