Skip to content

Commit cf463fb

Browse files
author
Daniel Demus
committed
[wundergroundupdatereceiver] Fix parsing of dateutc query parameter
The parameter is formatted with 1 digit hours, spewing warnings into the log, but otherwise having no effects as normal last XXX values are also recorded. Additionally a couple of unnoticed capitalisations were fixed Signed-off-by: Daniel Demus <[email protected]>
1 parent 8a7504f commit cf463fb

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

bundles/org.openhab.binding.wundergroundupdatereceiver/src/main/java/org/openhab/binding/wundergroundupdatereceiver/internal/WundergroundUpdateReceiverHandler.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
import static org.openhab.binding.wundergroundupdatereceiver.internal.WundergroundUpdateReceiverBindingConstants.*;
1616

17-
import java.time.ZonedDateTime;
17+
import java.time.LocalDateTime;
18+
import java.time.ZoneOffset;
19+
import java.time.format.DateTimeFormatter;
1820
import java.util.List;
1921
import java.util.Map;
2022
import java.util.Objects;
@@ -60,6 +62,9 @@ public String getStationId() {
6062
return config.stationId;
6163
}
6264

65+
// The format can be "yyyy-MM-dd H:mm:ss" from the device
66+
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd H:mm:ss");
67+
6368
private final Logger logger = LoggerFactory.getLogger(WundergroundUpdateReceiverHandler.class);
6469
private final WundergroundUpdateReceiverServlet wundergroundUpdateReceiverServlet;
6570
private final WundergroundUpdateReceiverDiscoveryService discoveryService;
@@ -183,19 +188,14 @@ private void buildChannel(ThingBuilder thingBuilder, String parameter, String va
183188
private DateTimeType safeResolvUtcDateTime(String dateUtc) {
184189
if (!dateUtc.isEmpty() && !NOW.equals(dateUtc)) {
185190
try {
186-
// Supposedly the format is "yyyy-MM-dd hh:mm:ss" from the device
187-
return new DateTimeType(ZonedDateTime.parse(dateUtc.replace(" ", "T") + "Z"));
191+
return new DateTimeType(LocalDateTime.parse(dateUtc, FORMATTER).atZone(ZoneOffset.UTC));
188192
} catch (Exception ex) {
189193
logger.warn("The device is submitting unparsable datetime values: {}", dateUtc);
190194
}
191195
}
192196
return new DateTimeType();
193197
}
194198

195-
public void updateChannelState(String channelId, String[] stateParts) {
196-
updateChannelState(channelId, String.join("", stateParts));
197-
}
198-
199199
public void updateChannelState(String parameterName, String state) {
200200
Optional<Channel> channel = getThing().getChannels().stream()
201201
.filter(ch -> parameterName.equals(ch.getUID().getIdWithoutGroup())).findFirst();

bundles/org.openhab.binding.wundergroundupdatereceiver/src/test/java/org/openhab/binding/wundergroundupdatereceiver/internal/WundergroundUpdateReceiverDiscoveryServiceTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void programmaticChannelsAreAddedCorrectlyOnce() {
134134
}
135135

136136
@Test
137-
void aRequestWithAnUnregisteredStationidIsAddedToTheQueueOnce()
137+
void aRequestWithAnUnregisteredStationIdIsAddedToTheQueueOnce()
138138
throws ServletException, NamespaceException, IOException {
139139
// Given
140140
final String queryString = """

bundles/org.openhab.binding.wundergroundupdatereceiver/src/test/java/org/openhab/binding/wundergroundupdatereceiver/internal/WundergroundUpdateReceiverServletTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void onDisposeAllHandlersAreRemovedAndServletIsInactive() throws ServletExceptio
179179
}
180180

181181
@Test
182-
void OnDisposeAllHandlersAreRemovedAndServletIsInactiveEvenThoughBackgroundDiscoveryIsEnabled()
182+
void onDisposeAllHandlersAreRemovedAndServletIsInactiveEvenThoughBackgroundDiscoveryIsEnabled()
183183
throws ServletException, NamespaceException {
184184
// Given
185185
WundergroundUpdateReceiverServlet sut = new WundergroundUpdateReceiverServlet(discoveryService);
@@ -249,7 +249,7 @@ void aGetRequestIsCorrectlyParsed() throws IOException {
249249
// Given
250250
ThingUID testThingUID = new ThingUID(WundergroundUpdateReceiverBindingConstants.THING_TYPE_UPDATE_RECEIVER,
251251
"test-receiver");
252-
final String queryString = "ID=dfggger&PASSWORD=XXXXXX&tempf=26.1&humidity=74&dewptf=18.9&windchillf=26.1&winddir=14&windspeedmph=1.34&windgustmph=2.46&rainin=0.00&dailyrainin=0.00&weeklyrainin=0.00&monthlyrainin=0.08&yearlyrainin=3.06&solarradiation=42.24&UV=1&indoortempf=69.3&indoorhumidity=32&baromin=30.39&AqNOX=21&lowbatt=1&dateutc=2021-02-07%2014:04:03&softwaretype=WH2600%20V2.2.8&action=updateraw&realtime=1&rtfreq=5";
252+
final String queryString = "ID=dfggger&PASSWORD=XXXXXX&tempf=26.1&humidity=74&dewptf=18.9&windchillf=26.1&winddir=14&windspeedmph=1.34&windgustmph=2.46&rainin=0.00&dailyrainin=0.00&weeklyrainin=0.00&monthlyrainin=0.08&yearlyrainin=3.06&solarradiation=42.24&UV=1&indoortempf=69.3&indoorhumidity=32&baromin=30.39&AqNOX=21&lowbatt=1&dateutc=2021-02-07%209:04:03&softwaretype=WH2600%20V2.2.8&action=updateraw&realtime=1&rtfreq=5";
253253
WundergroundUpdateReceiverServlet sut = new WundergroundUpdateReceiverServlet(discoveryService);
254254
List<Channel> channels = List.of(
255255
ChannelBuilder
@@ -337,7 +337,7 @@ discoveryService, new WundergroundUpdateReceiverUnknownChannelTypeProvider(), ch
337337
// Then
338338
verify(callback).stateUpdated(
339339
new ChannelUID(TEST_THING_UID, METADATA_GROUP, WundergroundUpdateReceiverBindingConstants.DATEUTC),
340-
StringType.valueOf("2021-02-07 14:04:03"));
340+
StringType.valueOf("2021-02-07 9:04:03"));
341341
verify(callback).stateUpdated(
342342
new ChannelUID(TEST_THING_UID, METADATA_GROUP, WundergroundUpdateReceiverBindingConstants.LOW_BATTERY),
343343
OnOffType.ON);
@@ -393,7 +393,7 @@ void aGetRequestWithIndexedParametresAreCorrectlyParsed() throws IOException {
393393
// Given
394394
ThingUID testThingUID = new ThingUID(WundergroundUpdateReceiverBindingConstants.THING_TYPE_UPDATE_RECEIVER,
395395
"test-receiver");
396-
final String queryString = "ID=dfggger&PASSWORD=XXXXXX&temp1f=26.1&humidity=74&temp2f=25.1&lowbatt=1&soilmoisture1=78&soilmoisture2=73&dateutc=2021-02-07%2014:04:03&softwaretype=WH2600%20V2.2.8&action=updateraw&realtime=1&rtfreq=5";
396+
final String queryString = "ID=dfggger&PASSWORD=XXXXXX&temp1f=26.1&humidity=74&temp2f=25.1&lowbatt=1&soilmoisture1=78&soilmoisture2=73&dateutc=2021-02-07%2004:04:03&softwaretype=WH2600%20V2.2.8&action=updateraw&realtime=1&rtfreq=5";
397397
WundergroundUpdateReceiverServlet sut = new WundergroundUpdateReceiverServlet(discoveryService);
398398
List<Channel> channels = List.of(
399399
ChannelBuilder
@@ -451,7 +451,7 @@ discoveryService, new WundergroundUpdateReceiverUnknownChannelTypeProvider(), ch
451451
// Then
452452
verify(callback).stateUpdated(
453453
new ChannelUID(TEST_THING_UID, METADATA_GROUP, WundergroundUpdateReceiverBindingConstants.DATEUTC),
454-
StringType.valueOf("2021-02-07 14:04:03"));
454+
StringType.valueOf("2021-02-07 04:04:03"));
455455
verify(callback).stateUpdated(
456456
new ChannelUID(TEST_THING_UID, METADATA_GROUP, WundergroundUpdateReceiverBindingConstants.LOW_BATTERY),
457457
OnOffType.ON);

0 commit comments

Comments
 (0)