From faf400eb8d0887d5922a5fb82f629b439bd050d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Sat, 9 Dec 2023 22:31:15 +0100 Subject: [PATCH] Enhance error handling when no value is provided for Synop (#16024) Signed-off-by: clinique --- .../handler/SynopAnalyzerHandler.java | 27 +++++++++++-------- .../synopanalyzer/internal/synop/Synop.java | 4 +-- .../internal/synop/WindDirections.java | 6 +++-- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/handler/SynopAnalyzerHandler.java b/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/handler/SynopAnalyzerHandler.java index 012191bb0f6fe..428fa8602b522 100644 --- a/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/handler/SynopAnalyzerHandler.java +++ b/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/handler/SynopAnalyzerHandler.java @@ -69,6 +69,9 @@ */ @NonNullByDefault public class SynopAnalyzerHandler extends BaseThingHandler { + private static final String DISTANCE = "Distance"; + private static final String LOCATION = "Location"; + private static final String USUAL_NAME = "Usual name"; private static final String OGIMET_SYNOP_PATH = "http://www.ogimet.com/cgi-bin/getsynop?block=%s&begin=%s"; private static final int REQUEST_TIMEOUT_MS = 5000; private static final DateTimeFormatter SYNOP_DATE_FORMAT = DateTimeFormatter.ofPattern("yyyyMMddHH00"); @@ -91,9 +94,9 @@ public SynopAnalyzerHandler(Thing thing, LocationProvider locationProvider, List @Override public void initialize() { SynopAnalyzerConfiguration configuration = getConfigAs(SynopAnalyzerConfiguration.class); - formattedStationId = String.format("%05d", configuration.stationId); - logger.info("Scheduling Synop update thread to run every {} minute for Station '{}'", - configuration.refreshInterval, formattedStationId); + formattedStationId = "%05d".formatted(configuration.stationId); + logger.info("Scheduling Synop update every {} minute for Station '{}'", configuration.refreshInterval, + formattedStationId); if (thing.getProperties().isEmpty()) { discoverAttributes(configuration.stationId, locationProvider.getLocation()); @@ -108,13 +111,13 @@ public void initialize() { private void discoverAttributes(int stationId, @Nullable PointType serverLocation) { stations.stream().filter(s -> stationId == s.idOmm).findFirst().ifPresent(station -> { Map properties = new HashMap<>( - Map.of("Usual name", station.usualName, "Location", station.getLocation())); + Map.of(USUAL_NAME, station.usualName, LOCATION, station.getLocation())); if (serverLocation != null) { PointType stationLocation = new PointType(station.getLocation()); DecimalType distance = serverLocation.distanceFrom(stationLocation); - properties.put("Distance", new QuantityType<>(distance, SIUnits.METRE).toString()); + properties.put(DISTANCE, new QuantityType<>(distance, SIUnits.METRE).toString()); } updateProperties(properties); }); @@ -138,11 +141,11 @@ private Optional getLastAvailableSynop() { return createSynopObject(synopMessage); } - logger.warn("Message does not belong to station {} : {}", formattedStationId, message); + logger.warn("Message does not belong to station {}: {}", formattedStationId, message); } logger.warn("No valid Synop found for last 24h"); } catch (IOException e) { - logger.warn("Synop request timedout : {}", e.getMessage()); + logger.warn("Synop request timedout: {}", e.getMessage()); } return Optional.empty(); } @@ -163,6 +166,7 @@ private void updateChannels() { private State getChannelState(String channelId, Synop synop) { int octa = synop.getOcta(); + Integer direction = synop.getWindDirection(); switch (channelId) { case HORIZONTAL_VISIBILITY: return new StringType(synop.getHorizontalVisibility().name()); @@ -183,9 +187,10 @@ private State getChannelState(String channelId, Synop synop) { case TEMPERATURE: return new QuantityType<>(synop.getTemperature(), TEMPERATURE_UNIT); case WIND_ANGLE: - return new QuantityType<>(synop.getWindDirection(), WIND_DIRECTION_UNIT); + return direction != null ? new QuantityType<>(direction, WIND_DIRECTION_UNIT) : UnDefType.NULL; case WIND_DIRECTION: - return new StringType(WindDirections.getWindDirection(synop.getWindDirection()).name()); + return direction != null ? new StringType(WindDirections.getWindDirection(direction).name()) + : UnDefType.NULL; case WIND_STRENGTH: return getWindStrength(synop); case WIND_SPEED_BEAUFORT: @@ -199,7 +204,7 @@ private State getChannelState(String channelId, Synop synop) { return new DateTimeType( ZonedDateTime.of(year, month, synop.getDay(), synop.getHour(), 0, 0, 0, ZoneOffset.UTC)); default: - logger.error("Unsupported channel Id '{}'", channelId); + logger.error("Unsupported channel '{}'", channelId); return UnDefType.UNDEF; } } @@ -226,7 +231,7 @@ private Optional createSynopObject(String synopMessage) { private String forgeURL() { ZonedDateTime utc = ZonedDateTime.now(ZoneOffset.UTC).minusDays(1); String beginDate = SYNOP_DATE_FORMAT.format(utc); - return String.format(OGIMET_SYNOP_PATH, formattedStationId, beginDate); + return OGIMET_SYNOP_PATH.formatted(formattedStationId, beginDate); } @Override diff --git a/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/synop/Synop.java b/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/synop/Synop.java index f761f3b933208..525ecff577222 100644 --- a/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/synop/Synop.java +++ b/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/synop/Synop.java @@ -292,8 +292,8 @@ public float getTemperature() { return temperature; } - public int getWindDirection() { - return windDirection; + public @Nullable Integer getWindDirection() { + return windDirection != INITIAL_VALUE ? windDirection : null; } public int getWindSpeed() { diff --git a/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/synop/WindDirections.java b/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/synop/WindDirections.java index 50d271ccbd7dc..62f778a7a3d62 100644 --- a/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/synop/WindDirections.java +++ b/bundles/org.openhab.binding.synopanalyzer/src/main/java/org/openhab/binding/synopanalyzer/internal/synop/WindDirections.java @@ -38,12 +38,14 @@ public enum WindDirections { NW, NNW; + private static final double STEP = 360.0 / values().length; + /** * Returns the wind direction based on degree. */ public static WindDirections getWindDirection(int degree) { - double step = 360.0 / values().length; - double b = Math.floor((degree + (step / 2.0)) / step); + + double b = Math.floor((degree + (STEP / 2.0)) / STEP); return values()[(int) (b % values().length)]; } }