]> git.basschouten.com Git - openhab-addons.git/blob
399e23247831b6df4b998aa828b9e5cbc981d95d
[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.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 the result node from the OpenUV JSON response
33  *
34  * @author GaĆ«l L'hopital - Initial contribution
35  */
36 @NonNullByDefault
37 public class OpenUVResult {
38     private final Logger logger = LoggerFactory.getLogger(OpenUVResult.class);
39
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
53     }
54
55     private double uv;
56     private double uvMax;
57     private double ozone;
58     private @Nullable ZonedDateTime uvTime;
59     private @Nullable ZonedDateTime uvMaxTime;
60     private @Nullable ZonedDateTime ozoneTime;
61     private Map<FitzpatrickType, @Nullable Integer> safeExposureTime = new HashMap<>();
62
63     public double getUv() {
64         return uv;
65     }
66
67     public double getUvMax() {
68         return uvMax;
69     }
70
71     public double getOzone() {
72         return ozone;
73     }
74
75     private State getValueOrNull(@Nullable ZonedDateTime value) {
76         return value == null ? UnDefType.NULL : new DateTimeType(value);
77     }
78
79     public State getUVTime() {
80         return getValueOrNull(uvTime);
81     }
82
83     public State getUVMaxTime() {
84         return getValueOrNull(uvMaxTime);
85     }
86
87     public State getOzoneTime() {
88         return getValueOrNull(ozoneTime);
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 QuantityType.valueOf(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 }