]> git.basschouten.com Git - openhab-addons.git/commitdiff
[OpenUV] Correcting a SAT finding. (#8967)
authorGaël L'hopital <gael@lhopital.org>
Mon, 9 Nov 2020 16:55:35 +0000 (17:55 +0100)
committerGitHub <noreply@github.com>
Mon, 9 Nov 2020 16:55:35 +0000 (08:55 -0800)
* Correcting a SAT finding.
Adding alert levels compatible with MeteoAlarm and Vigicrues
* Adressing code review.
* spotless apply

Signed-off-by: clinique <gael@lhopital.org>
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/OpenUVBindingConstants.java
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/OpenUVException.java
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/OpenUVHandlerFactory.java
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/handler/OpenUVReportHandler.java
bundles/org.openhab.binding.openuv/src/main/java/org/openhab/binding/openuv/internal/json/OpenUVResult.java
bundles/org.openhab.binding.openuv/src/main/resources/OH-INF/thing/thing-types.xml

index 9383e8709799a3e8125d601dc7a581881e8854fd..ee0559fae45423d6e8fdbfa14fb432aa7d20b5fc 100644 (file)
@@ -39,6 +39,7 @@ public class OpenUVBindingConstants {
 
     // List of all Channel id's
     public static final String UV_INDEX = "UVIndex";
+    public static final String ALERT_LEVEL = "Alert";
     public static final String UV_COLOR = "UVColor";
     public static final String UV_MAX = "UVMax";
     public static final String UV_MAX_TIME = "UVMaxTime";
index e51aeb42c2796f1b235ba190556a009d163b7078..a3bee7cc4058c395c6a835ee6aa2bef6176fd25a 100644 (file)
@@ -29,11 +29,16 @@ public class OpenUVException extends Exception {
         super(message);
     }
 
+    private boolean checkMatches(String message) {
+        String currentMessage = getMessage();
+        return currentMessage != null && currentMessage.startsWith(message);
+    }
+
     public boolean isApiKeyError() {
-        return this.getMessage().startsWith(ERROR_WRONG_KEY);
+        return checkMatches(ERROR_WRONG_KEY);
     }
 
     public boolean isQuotaError() {
-        return this.getMessage().startsWith(ERROR_QUOTA_EXCEEDED);
+        return checkMatches(ERROR_QUOTA_EXCEEDED);
     }
 }
index 3e66d08db83c8bcc24d67f7e4425d06a8dd32fc5..04486c3b7de26b4a2c5ac6af598d2d4f7c77d3ac 100644 (file)
@@ -22,7 +22,6 @@ import org.openhab.binding.openuv.internal.handler.OpenUVBridgeHandler;
 import org.openhab.binding.openuv.internal.handler.OpenUVReportHandler;
 import org.openhab.core.i18n.LocationProvider;
 import org.openhab.core.i18n.TimeZoneProvider;
-import org.openhab.core.library.types.DecimalType;
 import org.openhab.core.thing.Bridge;
 import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingTypeUID;
@@ -56,9 +55,6 @@ public class OpenUVHandlerFactory extends BaseThingHandlerFactory {
             @Reference LocationProvider locationProvider) {
         this.locationProvider = locationProvider;
         this.gson = new GsonBuilder()
-                .registerTypeAdapter(DecimalType.class,
-                        (JsonDeserializer<DecimalType>) (json, type, jsonDeserializationContext) -> DecimalType
-                                .valueOf(json.getAsJsonPrimitive().getAsString()))
                 .registerTypeAdapter(ZonedDateTime.class,
                         (JsonDeserializer<ZonedDateTime>) (json, type, jsonDeserializationContext) -> ZonedDateTime
                                 .parse(json.getAsJsonPrimitive().getAsString())
index 582e3ca67d79494a13d2581fcf6837d8b4bd9819..f0622f345697b13f270c4af532f2f212874aa635 100644 (file)
@@ -17,6 +17,7 @@ import static org.openhab.binding.openuv.internal.OpenUVBindingConstants.*;
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
 import java.time.temporal.ChronoUnit;
+import java.util.Map;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 
@@ -28,6 +29,7 @@ import org.openhab.binding.openuv.internal.config.ReportConfiguration;
 import org.openhab.binding.openuv.internal.config.SafeExposureConfiguration;
 import org.openhab.binding.openuv.internal.json.OpenUVResult;
 import org.openhab.core.library.types.DateTimeType;
+import org.openhab.core.library.types.DecimalType;
 import org.openhab.core.library.types.HSBType;
 import org.openhab.core.library.types.PointType;
 import org.openhab.core.library.types.QuantityType;
@@ -56,6 +58,17 @@ import org.slf4j.LoggerFactory;
  */
 @NonNullByDefault
 public class OpenUVReportHandler extends BaseThingHandler {
+    private static final DecimalType ALERT_GREEN = DecimalType.ZERO;
+    private static final DecimalType ALERT_YELLOW = new DecimalType(1);
+    private static final DecimalType ALERT_ORANGE = new DecimalType(2);
+    private static final DecimalType ALERT_RED = new DecimalType(3);
+    private static final DecimalType ALERT_PURPLE = new DecimalType(4);
+    private static final State ALERT_UNDEF = HSBType.fromRGB(179, 179, 179);
+
+    private static final Map<State, State> ALERT_COLORS = Map.of(ALERT_GREEN, HSBType.fromRGB(85, 139, 47),
+            ALERT_YELLOW, HSBType.fromRGB(249, 168, 37), ALERT_ORANGE, HSBType.fromRGB(239, 108, 0), ALERT_RED,
+            HSBType.fromRGB(183, 28, 28), ALERT_PURPLE, HSBType.fromRGB(106, 27, 154));
+
     private final Logger logger = LoggerFactory.getLogger(OpenUVReportHandler.class);
 
     private @NonNullByDefault({}) OpenUVBridgeHandler bridgeHandler;
@@ -195,13 +208,17 @@ public class OpenUVReportHandler extends BaseThingHandler {
             if (channelTypeUID != null) {
                 switch (channelTypeUID.getId()) {
                     case UV_INDEX:
-                        updateState(channelUID, openUVData.getUv());
+                        updateState(channelUID, asDecimalType(openUVData.getUv()));
+                        break;
+                    case ALERT_LEVEL:
+                        updateState(channelUID, asAlertLevel(openUVData.getUv()));
                         break;
                     case UV_COLOR:
-                        updateState(channelUID, getAsHSB(openUVData.getUv().intValue()));
+                        updateState(channelUID,
+                                ALERT_COLORS.getOrDefault(asAlertLevel(openUVData.getUv()), ALERT_UNDEF));
                         break;
                     case UV_MAX:
-                        updateState(channelUID, openUVData.getUvMax());
+                        updateState(channelUID, asDecimalType(openUVData.getUvMax()));
                         break;
                     case OZONE:
                         updateState(channelUID, new QuantityType<>(openUVData.getOzone(), SmartHomeUnits.DOBSON_UNIT));
@@ -228,17 +245,25 @@ public class OpenUVReportHandler extends BaseThingHandler {
         }
     }
 
-    private State getAsHSB(int uv) {
+    private State asDecimalType(int uv) {
+        if (uv >= 1) {
+            return new DecimalType(uv);
+        }
+        return UnDefType.NULL;
+    }
+
+    private State asAlertLevel(int uv) {
         if (uv >= 11) {
-            return HSBType.fromRGB(106, 27, 154);
+            return ALERT_PURPLE;
         } else if (uv >= 8) {
-            return HSBType.fromRGB(183, 28, 28);
+            return ALERT_RED;
         } else if (uv >= 6) {
-            return HSBType.fromRGB(239, 108, 0);
+            return ALERT_ORANGE;
         } else if (uv >= 3) {
-            return HSBType.fromRGB(249, 168, 37);
-        } else {
-            return HSBType.fromRGB(85, 139, 47);
+            return ALERT_YELLOW;
+        } else if (uv >= 1) {
+            return ALERT_GREEN;
         }
+        return UnDefType.NULL;
     }
 }
index b6a07e23f9a5248c035219af9e9fbd0e652a26cd..88b8dd33420aed1cc56b58700b7cc5f82850d591 100644 (file)
@@ -18,7 +18,6 @@ import java.time.ZonedDateTime;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.core.library.types.DateTimeType;
-import org.openhab.core.library.types.DecimalType;
 import org.openhab.core.types.State;
 import org.openhab.core.types.UnDefType;
 
@@ -31,23 +30,23 @@ import org.openhab.core.types.UnDefType;
 @NonNullByDefault
 public class OpenUVResult {
     private final ZonedDateTime DEFAULT_ZDT = ZonedDateTime.of(LocalDateTime.MIN, ZoneId.systemDefault());
-    private DecimalType uv = new DecimalType(0);
+    private double uv;
     private ZonedDateTime uvTime = DEFAULT_ZDT;
-    private DecimalType uvMax = new DecimalType(0);
+    private double uvMax;
     private ZonedDateTime uvMaxTime = DEFAULT_ZDT;
-    private DecimalType ozone = new DecimalType(0);
+    private double ozone;
     private ZonedDateTime ozoneTime = DEFAULT_ZDT;
     private SafeExposureTime safeExposureTime = new SafeExposureTime();
 
-    public DecimalType getUv() {
-        return uv;
+    public int getUv() {
+        return (int) uv;
     }
 
-    public DecimalType getUvMax() {
-        return uvMax;
+    public int getUvMax() {
+        return (int) uvMax;
     }
 
-    public DecimalType getOzone() {
+    public double getOzone() {
         return ozone;
     }
 
index 51ed38d85554441a4a932bb80f6aa542891be1b5..dc96bb92cf8230186e74a433fc677744b8a015ff 100644 (file)
@@ -35,6 +35,7 @@
 
                <channels>
                        <channel id="UVIndex" typeId="UVIndex"/>
+                       <channel id="Alert" typeId="Alert"/>
                        <channel id="UVColor" typeId="UVColor"/>
                        <channel id="UVMax" typeId="UVMax"/>
                        <channel id="UVMaxTime" typeId="UVMaxTime"/>
                <item-type>Number</item-type>
                <label>UV Index</label>
                <description>UV Index</description>
-               <state readOnly="true" pattern="%.2f/16" min="0" max="16"/>
+               <state readOnly="true" pattern="%d/16" min="0" max="16"/>
        </channel-type>
 
        <channel-type id="UVMax" advanced="true">
                <item-type>Number</item-type>
                <label>UV Max</label>
                <description>Max UV Index for the day (at solar noon)</description>
-               <state readOnly="true" pattern="%.2f/16" min="0" max="16"/>
+               <state readOnly="true" pattern="%d/16" min="0" max="16"/>
        </channel-type>
 
        <channel-type id="Ozone">
@@ -86,7 +87,8 @@
        <channel-type id="OzoneTime" advanced="true">
                <item-type>DateTime</item-type>
                <label>Ozone Observation Time</label>
-               <description>Latest OMI ozone update time</description>
+               <description>Latest OMI ozone update timestamp.</description>
+               <category>time</category>
                <state readOnly="true" pattern="%1$tF %1$tR"/>
        </channel-type>
 
                <item-type>DateTime</item-type>
                <label>UV Max Time</label>
                <description>Max UV Index time (solar noon)</description>
+               <category>time</category>
                <state readOnly="true" pattern="%1$tF %1$tR"/>
        </channel-type>
 
        <channel-type id="UVTime" advanced="true">
                <item-type>DateTime</item-type>
                <label>UV Time</label>
-               <description>UV Index time</description>
+               <description>UV Index timestamp.</description>
+               <category>time</category>
                <state readOnly="true" pattern="%1$tF %1$tR"/>
        </channel-type>
 
        <channel-type id="UVColor" advanced="true">
                <item-type>Color</item-type>
-               <label>UV Color</label>
-               <description>Color associated to given UV Index.</description>
+               <label>UV Alert Color</label>
+               <description>Color associated to given UV Index alert level.</description>
                <state readOnly="true"/>
        </channel-type>
 
                <description>Triggers when current UV Index reaches maximum of the day</description>
        </channel-type>
 
+       <channel-type id="Alert">
+               <item-type>Number</item-type>
+               <label>UV Alert</label>
+               <state readOnly="true">
+                       <options>
+                               <option value="0">Low</option>
+                               <option value="1">Medium</option>
+                               <option value="2">High</option>
+                               <option value="3">Very high</option>
+                               <option value="4">Extreme</option>
+                       </options>
+               </state>
+       </channel-type>
+
 </thing:thing-descriptions>