Skip to content

Commit aa8cac1

Browse files
committed
Fix review comments
Signed-off-by: Anders Alfredsson <andersb86@gmail.com>
1 parent ebb5fbc commit aa8cac1

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

bundles/org.openhab.binding.smhi/src/main/java/org/openhab/binding/smhi/internal/ForecastAggregator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public class ForecastAggregator {
3838
* @return
3939
*/
4040
public static State max(SmhiTimeSeries timeSeries, int dayOffset, @Nullable ParameterMetadata metadata) {
41-
if (metadata == null)
41+
if (metadata == null) {
4242
return UnDefType.UNDEF;
43+
}
4344

4445
List<Forecast> dayForecasts = timeSeries.getDay(dayOffset, false);
4546
return dayForecasts.stream().map(forecast -> forecast.getParameter(metadata.name()))
@@ -57,8 +58,9 @@ public static State max(SmhiTimeSeries timeSeries, int dayOffset, @Nullable Para
5758
* @return
5859
*/
5960
public static State min(SmhiTimeSeries timeSeries, int dayOffset, @Nullable ParameterMetadata metadata) {
60-
if (metadata == null)
61+
if (metadata == null) {
6162
return UnDefType.UNDEF;
63+
}
6264

6365
List<Forecast> dayForecasts = timeSeries.getDay(dayOffset, false);
6466
return dayForecasts.stream().map(forecast -> forecast.getParameter(metadata.name()))
@@ -79,8 +81,9 @@ public static State min(SmhiTimeSeries timeSeries, int dayOffset, @Nullable Para
7981
*/
8082
public static State total(SmhiTimeSeries timeSeries, int dayOffset, @Nullable ParameterMetadata baseMetadata,
8183
@Nullable ParameterMetadata totalMetadata) {
82-
if (baseMetadata == null || totalMetadata == null)
84+
if (baseMetadata == null || totalMetadata == null) {
8385
return UnDefType.UNDEF;
86+
}
8487

8588
List<Forecast> dayForecasts = timeSeries.getDay(dayOffset, true);
8689
if (dayForecasts.size() == 1) {
@@ -105,8 +108,9 @@ public static State total(SmhiTimeSeries timeSeries, int dayOffset, @Nullable Pa
105108
* @return
106109
*/
107110
public static State noonOrFirst(SmhiTimeSeries timeSeries, int dayOffset, @Nullable ParameterMetadata metadata) {
108-
if (metadata == null)
111+
if (metadata == null) {
109112
return UnDefType.UNDEF;
113+
}
110114

111115
List<Forecast> dayForecasts = timeSeries.getDay(dayOffset, false);
112116
return dayForecasts.stream().filter(forecast -> forecast.getTime().getHour() >= 12).findFirst()

bundles/org.openhab.binding.smhi/src/main/java/org/openhab/binding/smhi/internal/SmhiConnector.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
*/
1313
package org.openhab.binding.smhi.internal;
1414

15+
import static org.eclipse.jetty.http.HttpStatus.*;
1516
import static org.openhab.binding.smhi.internal.SmhiBindingConstants.*;
1617

1718
import java.time.ZonedDateTime;
1819
import java.time.format.DateTimeParseException;
1920
import java.util.List;
2021
import java.util.Locale;
2122
import java.util.concurrent.ExecutionException;
23+
import java.util.concurrent.TimeUnit;
2224
import java.util.concurrent.TimeoutException;
2325

2426
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -58,7 +60,7 @@ public ZonedDateTime getCreatedTime() throws SmhiException {
5860
logger.debug("Fetching reference time");
5961
ContentResponse resp = makeRequest(CREATED_TIME_URL);
6062

61-
if (resp.getStatus() == 200) {
63+
if (resp.getStatus() == OK_200) {
6264
return Parser.parseCreatedTime(resp.getContentAsString());
6365
} else {
6466
throw new SmhiException(resp.getReason());
@@ -78,14 +80,14 @@ public SmhiTimeSeries getForecast(double lat, double lon) throws SmhiException,
7880
ContentResponse resp = makeRequest(url);
7981

8082
switch (resp.getStatus()) {
81-
case 200:
83+
case OK_200:
8284
try {
8385
return Parser.parseTimeSeries(resp.getContentAsString());
8486
} catch (JsonParseException | DateTimeParseException e) {
8587
throw new SmhiException(e);
8688
}
87-
case 400:
88-
case 404:
89+
case BAD_REQUEST_400:
90+
case NOT_FOUND_404:
8991
throw new PointOutOfBoundsException();
9092
default:
9193
throw new SmhiException(resp.getReason());
@@ -96,7 +98,7 @@ public List<ParameterMetadata> getParameterMetadata() throws SmhiException {
9698
logger.debug("Fetching parameter metadata");
9799
ContentResponse resp = makeRequest(PARAMETER_METADATA_URL);
98100

99-
if (resp.getStatus() == 200) {
101+
if (resp.getStatus() == OK_200) {
100102
try {
101103
return Parser.parseParameterMetadata(resp.getContentAsString());
102104
} catch (JsonParseException | DateTimeParseException e) {
@@ -108,6 +110,7 @@ public List<ParameterMetadata> getParameterMetadata() throws SmhiException {
108110

109111
private ContentResponse makeRequest(String url) throws SmhiException {
110112
Request req = httpClient.newRequest(url);
113+
req.timeout(5, TimeUnit.SECONDS);
111114
req.accept(ACCEPT);
112115
ContentResponse resp;
113116
try {

bundles/org.openhab.binding.smhi/src/main/java/org/openhab/binding/smhi/internal/SmhiHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,9 @@ private void updateChannels(SmhiTimeSeries timeSeries) {
230230
id = PMP3G_BACKWARD_COMP.getOrDefault(id, id);
231231
// TODO: end
232232
ParameterMetadata metadata = channelTypeProvider.getParameterMetadata(id);
233-
if (metadata == null)
233+
if (metadata == null) {
234234
return;
235+
}
235236

236237
updateState(c.getUID(), f.getParameterAsState(metadata));
237238
}));
@@ -399,8 +400,9 @@ private List<Channel> createChannels() {
399400
channelTypeProvider.getAllParameterMetadata().forEach(metadata -> {
400401
ChannelType channelType = channelTypeRegistry
401402
.getChannelType(new ChannelTypeUID(BINDING_ID, metadata.name()));
402-
if (channelType == null)
403+
if (channelType == null) {
403404
return;
405+
}
404406

405407
if (!AGGREGATE_CHANNELS.contains(metadata.name())) {
406408
channels.add(createChannel(tsGroup, channelType));

0 commit comments

Comments
 (0)