[remoteopenhab] Catch exception when parsing item state (#9690)

* [remoteopenhab] Catch exception when parsing item state
* Review comment: set channel state to UNDEF in case item state parsing failed

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2021-01-05 00:55:25 +01:00 committed by GitHub
parent e2c9656148
commit 7e06da2401
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 98 additions and 98 deletions

View File

@ -14,9 +14,9 @@ package org.openhab.binding.remoteopenhab.internal.handler;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.DateTimeException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@ -484,6 +484,7 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
return;
}
State channelState = null;
try {
if (stateType == null && "NULL".equals(state)) {
channelState = UnDefType.NULL;
} else if (stateType == null && "UNDEF".equals(state)) {
@ -544,12 +545,7 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
break;
case CoreItemFactory.DATETIME:
if (checkStateType(itemName, stateType, "DateTime")) {
try {
channelState = new DateTimeType(ZonedDateTime.parse(state, FORMATTER_DATE));
} catch (DateTimeParseException e) {
logger.debug("Failed to parse date {} for item {}", state, itemName);
channelState = null;
}
}
break;
case CoreItemFactory.LOCATION:
@ -587,6 +583,10 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
break;
}
}
} catch (IllegalArgumentException | DateTimeException e) {
logger.warn("Failed to parse state \"{}\" for item {}: {}", state, itemName, e.getMessage());
channelState = UnDefType.UNDEF;
}
if (channelState != null) {
if (onlyIfStateChanged && channelState.equals(channelsLastStates.get(channel.getUID()))) {
logger.trace("ItemStateChangedEvent ignored for item {} as state is identical to the last state",