Skip to content

Commit

Permalink
Revert "[meteoalerte] Small code enhancements (openhab#10993)"
Browse files Browse the repository at this point in the history
This reverts commit 29bdbd4.

Signed-off-by: dw-8 <[email protected]>
  • Loading branch information
dw-8 committed Jul 25, 2021
1 parent 844c07b commit 4e483ca
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
@NonNullByDefault
public class MeteoAlerteBindingConstants {

public static final String BINDING_ID = "meteoalerte";

// List of all Thing Type UIDs
public static final ThingTypeUID THING_TYPE_METEO_ALERT = new ThingTypeUID("meteoalerte", "department");
public static final ThingTypeUID THING_TYPE_METEO_ALERT = new ThingTypeUID(BINDING_ID, "department");

// List of all Channel id's
public static final String WAVE = "vague-submersion";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime;
import java.util.AbstractMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ScheduledFuture;
Expand Down Expand Up @@ -61,15 +62,15 @@ public class MeteoAlerteHandler extends BaseThingHandler {
+ "facet=etat_grand_froid&facet=etat_avalanches&refine.nom_dept=%s";
private static final int TIMEOUT_MS = 30000;
private static final String UNKNOWN_COLOR = "b3b3b3";
private static final Map<AlertLevel, String> ALERT_COLORS = Map.ofEntries(Map.entry(AlertLevel.GREEN, "00ff00"),
Map.entry(AlertLevel.YELLOW, "ffff00"), Map.entry(AlertLevel.ORANGE, "ff6600"),
Map.entry(AlertLevel.RED, "ff0000"), Map.entry(AlertLevel.UNKNOWN, UNKNOWN_COLOR));

private static final Map<AlertLevel, State> ALERT_LEVELS = Map.ofEntries(
Map.entry(AlertLevel.GREEN, DecimalType.ZERO), Map.entry(AlertLevel.YELLOW, new DecimalType(1)),
Map.entry(AlertLevel.ORANGE, new DecimalType(2)), Map.entry(AlertLevel.RED, new DecimalType(3)));
private static final Map<AlertLevel, String> ALERT_COLORS = Map.ofEntries(
new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.GREEN, "00ff00"),
new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.YELLOW, "ffff00"),
new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.ORANGE, "ff6600"),
new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.RED, "ff0000"),
new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.UNKNOWN, UNKNOWN_COLOR));

private final Logger logger = LoggerFactory.getLogger(MeteoAlerteHandler.class);
// Time zone provider representing time zone configured in openHAB configuration
private final Gson gson;
private @Nullable ScheduledFuture<?> refreshJob;
private String queryUrl = "";
Expand All @@ -95,11 +96,11 @@ public void initialize() {
@Override
public void dispose() {
logger.debug("Disposing the Météo Alerte handler.");
ScheduledFuture<?> localJob = refreshJob;
if (localJob != null) {
localJob.cancel(true);
ScheduledFuture<?> refreshJob = this.refreshJob;
if (refreshJob != null) {
refreshJob.cancel(true);
}
refreshJob = null;
this.refreshJob = null;
}

@Override
Expand All @@ -116,7 +117,8 @@ private void updateAndPublish() {
}
String response = HttpUtil.executeUrl("GET", queryUrl, TIMEOUT_MS);
if (response == null) {
throw new IOException("Empty response");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "empty response");
return;
}
updateStatus(ThingStatus.ONLINE);
ApiResponse apiResponse = gson.fromJson(response, ApiResponse.class);
Expand Down Expand Up @@ -151,7 +153,7 @@ private void updateChannels(ApiResponse apiResponse) {
}));
}

private byte @Nullable [] getResource(String iconPath) {
public byte @Nullable [] getResource(String iconPath) {
ClassLoader classLoader = MeteoAlerteHandler.class.getClassLoader();
if (classLoader != null) {
try (InputStream stream = classLoader.getResourceAsStream(iconPath)) {
Expand All @@ -163,10 +165,10 @@ private void updateChannels(ApiResponse apiResponse) {
return null;
}

private void updateAlert(String channelId, AlertLevel value) {
public void updateAlert(String channelId, AlertLevel value) {
String channelIcon = channelId + "-icon";
if (isLinked(channelId)) {
updateState(channelId, ALERT_LEVELS.getOrDefault(value, UnDefType.UNDEF));
updateState(channelId, getAlertLevel(value));
}
if (isLinked(channelIcon)) {
State result = UnDefType.UNDEF;
Expand All @@ -180,9 +182,24 @@ private void updateAlert(String channelId, AlertLevel value) {
}
}

private void updateDate(String channelId, ZonedDateTime zonedDateTime) {
public void updateDate(String channelId, ZonedDateTime zonedDateTime) {
if (isLinked(channelId)) {
updateState(channelId, new DateTimeType(zonedDateTime));
}
}

public State getAlertLevel(AlertLevel alert) {
switch (alert) {
case GREEN:
return DecimalType.ZERO;
case YELLOW:
return new DecimalType(1);
case ORANGE:
return new DecimalType(2);
case RED:
return new DecimalType(3);
default:
return UnDefType.UNDEF;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ public String getNomReg() {
}

public Optional<ZonedDateTime> getDatePrevue() {
return Optional.ofNullable(datePrevue);
ZonedDateTime datePrevue = this.datePrevue;
if (datePrevue != null) {
return Optional.of(datePrevue);
}
return Optional.empty();
}

public String getTypePrev() {
Expand Down Expand Up @@ -122,7 +126,11 @@ public AlertLevel getVent() {
}

public Optional<ZonedDateTime> getDateInsert() {
return Optional.ofNullable(dateInsert);
ZonedDateTime dateInsert = this.dateInsert;
if (dateInsert != null) {
return Optional.of(dateInsert);
}
return Optional.empty();
}

public AlertLevel getInondation() {
Expand Down Expand Up @@ -150,6 +158,10 @@ public String getDep() {
}

public Optional<ZonedDateTime> getDateRun() {
return Optional.ofNullable(dateRun);
ZonedDateTime dateRun = this.dateRun;
if (dateRun != null) {
return Optional.of(dateRun);
}
return Optional.empty();
}
}

0 comments on commit 4e483ca

Please sign in to comment.