mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 23:22:02 +01:00
[meteoalerte] Small code enhancements (#10993)
Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
parent
71638cead2
commit
11a66e5ce8
@ -24,10 +24,8 @@ import org.openhab.core.thing.ThingTypeUID;
|
|||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class MeteoAlerteBindingConstants {
|
public class MeteoAlerteBindingConstants {
|
||||||
|
|
||||||
public static final String BINDING_ID = "meteoalerte";
|
|
||||||
|
|
||||||
// List of all Thing Type UIDs
|
// List of all Thing Type UIDs
|
||||||
public static final ThingTypeUID THING_TYPE_METEO_ALERT = new ThingTypeUID(BINDING_ID, "department");
|
public static final ThingTypeUID THING_TYPE_METEO_ALERT = new ThingTypeUID("meteoalerte", "department");
|
||||||
|
|
||||||
// List of all Channel id's
|
// List of all Channel id's
|
||||||
public static final String WAVE = "vague-submersion";
|
public static final String WAVE = "vague-submersion";
|
||||||
|
@ -19,7 +19,6 @@ import java.io.InputStream;
|
|||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.util.AbstractMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
@ -62,15 +61,15 @@ public class MeteoAlerteHandler extends BaseThingHandler {
|
|||||||
+ "facet=etat_grand_froid&facet=etat_avalanches&refine.nom_dept=%s";
|
+ "facet=etat_grand_froid&facet=etat_avalanches&refine.nom_dept=%s";
|
||||||
private static final int TIMEOUT_MS = 30000;
|
private static final int TIMEOUT_MS = 30000;
|
||||||
private static final String UNKNOWN_COLOR = "b3b3b3";
|
private static final String UNKNOWN_COLOR = "b3b3b3";
|
||||||
private static final Map<AlertLevel, String> ALERT_COLORS = Map.ofEntries(
|
private static final Map<AlertLevel, String> ALERT_COLORS = Map.ofEntries(Map.entry(AlertLevel.GREEN, "00ff00"),
|
||||||
new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.GREEN, "00ff00"),
|
Map.entry(AlertLevel.YELLOW, "ffff00"), Map.entry(AlertLevel.ORANGE, "ff6600"),
|
||||||
new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.YELLOW, "ffff00"),
|
Map.entry(AlertLevel.RED, "ff0000"), Map.entry(AlertLevel.UNKNOWN, UNKNOWN_COLOR));
|
||||||
new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.ORANGE, "ff6600"),
|
|
||||||
new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.RED, "ff0000"),
|
private static final Map<AlertLevel, State> ALERT_LEVELS = Map.ofEntries(
|
||||||
new AbstractMap.SimpleEntry<AlertLevel, String>(AlertLevel.UNKNOWN, UNKNOWN_COLOR));
|
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 final Logger logger = LoggerFactory.getLogger(MeteoAlerteHandler.class);
|
private final Logger logger = LoggerFactory.getLogger(MeteoAlerteHandler.class);
|
||||||
// Time zone provider representing time zone configured in openHAB configuration
|
|
||||||
private final Gson gson;
|
private final Gson gson;
|
||||||
private @Nullable ScheduledFuture<?> refreshJob;
|
private @Nullable ScheduledFuture<?> refreshJob;
|
||||||
private String queryUrl = "";
|
private String queryUrl = "";
|
||||||
@ -96,11 +95,11 @@ public class MeteoAlerteHandler extends BaseThingHandler {
|
|||||||
@Override
|
@Override
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
logger.debug("Disposing the Météo Alerte handler.");
|
logger.debug("Disposing the Météo Alerte handler.");
|
||||||
ScheduledFuture<?> refreshJob = this.refreshJob;
|
ScheduledFuture<?> localJob = refreshJob;
|
||||||
if (refreshJob != null) {
|
if (localJob != null) {
|
||||||
refreshJob.cancel(true);
|
localJob.cancel(true);
|
||||||
}
|
}
|
||||||
this.refreshJob = null;
|
refreshJob = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -117,8 +116,7 @@ public class MeteoAlerteHandler extends BaseThingHandler {
|
|||||||
}
|
}
|
||||||
String response = HttpUtil.executeUrl("GET", queryUrl, TIMEOUT_MS);
|
String response = HttpUtil.executeUrl("GET", queryUrl, TIMEOUT_MS);
|
||||||
if (response == null) {
|
if (response == null) {
|
||||||
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "empty response");
|
throw new IOException("Empty response");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
updateStatus(ThingStatus.ONLINE);
|
updateStatus(ThingStatus.ONLINE);
|
||||||
ApiResponse apiResponse = gson.fromJson(response, ApiResponse.class);
|
ApiResponse apiResponse = gson.fromJson(response, ApiResponse.class);
|
||||||
@ -153,7 +151,7 @@ public class MeteoAlerteHandler extends BaseThingHandler {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte @Nullable [] getResource(String iconPath) {
|
private byte @Nullable [] getResource(String iconPath) {
|
||||||
ClassLoader classLoader = MeteoAlerteHandler.class.getClassLoader();
|
ClassLoader classLoader = MeteoAlerteHandler.class.getClassLoader();
|
||||||
if (classLoader != null) {
|
if (classLoader != null) {
|
||||||
try (InputStream stream = classLoader.getResourceAsStream(iconPath)) {
|
try (InputStream stream = classLoader.getResourceAsStream(iconPath)) {
|
||||||
@ -165,10 +163,10 @@ public class MeteoAlerteHandler extends BaseThingHandler {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateAlert(String channelId, AlertLevel value) {
|
private void updateAlert(String channelId, AlertLevel value) {
|
||||||
String channelIcon = channelId + "-icon";
|
String channelIcon = channelId + "-icon";
|
||||||
if (isLinked(channelId)) {
|
if (isLinked(channelId)) {
|
||||||
updateState(channelId, getAlertLevel(value));
|
updateState(channelId, ALERT_LEVELS.getOrDefault(value, UnDefType.UNDEF));
|
||||||
}
|
}
|
||||||
if (isLinked(channelIcon)) {
|
if (isLinked(channelIcon)) {
|
||||||
State result = UnDefType.UNDEF;
|
State result = UnDefType.UNDEF;
|
||||||
@ -182,24 +180,9 @@ public class MeteoAlerteHandler extends BaseThingHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateDate(String channelId, ZonedDateTime zonedDateTime) {
|
private void updateDate(String channelId, ZonedDateTime zonedDateTime) {
|
||||||
if (isLinked(channelId)) {
|
if (isLinked(channelId)) {
|
||||||
updateState(channelId, new DateTimeType(zonedDateTime));
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -86,11 +86,7 @@ public class ResponseFieldDTO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Optional<ZonedDateTime> getDatePrevue() {
|
public Optional<ZonedDateTime> getDatePrevue() {
|
||||||
ZonedDateTime datePrevue = this.datePrevue;
|
return Optional.ofNullable(datePrevue);
|
||||||
if (datePrevue != null) {
|
|
||||||
return Optional.of(datePrevue);
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTypePrev() {
|
public String getTypePrev() {
|
||||||
@ -126,11 +122,7 @@ public class ResponseFieldDTO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Optional<ZonedDateTime> getDateInsert() {
|
public Optional<ZonedDateTime> getDateInsert() {
|
||||||
ZonedDateTime dateInsert = this.dateInsert;
|
return Optional.ofNullable(dateInsert);
|
||||||
if (dateInsert != null) {
|
|
||||||
return Optional.of(dateInsert);
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public AlertLevel getInondation() {
|
public AlertLevel getInondation() {
|
||||||
@ -158,10 +150,6 @@ public class ResponseFieldDTO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Optional<ZonedDateTime> getDateRun() {
|
public Optional<ZonedDateTime> getDateRun() {
|
||||||
ZonedDateTime dateRun = this.dateRun;
|
return Optional.ofNullable(dateRun);
|
||||||
if (dateRun != null) {
|
|
||||||
return Optional.of(dateRun);
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user