[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

View File

@ -14,9 +14,9 @@ package org.openhab.binding.remoteopenhab.internal.handler;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.time.DateTimeException;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -484,108 +484,108 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
return; return;
} }
State channelState = null; State channelState = null;
if (stateType == null && "NULL".equals(state)) { try {
channelState = UnDefType.NULL; if (stateType == null && "NULL".equals(state)) {
} else if (stateType == null && "UNDEF".equals(state)) { channelState = UnDefType.NULL;
channelState = UnDefType.UNDEF; } else if (stateType == null && "UNDEF".equals(state)) {
} else if ("UnDef".equals(stateType)) { channelState = UnDefType.UNDEF;
switch (state) { } else if ("UnDef".equals(stateType)) {
case "NULL": switch (state) {
channelState = UnDefType.NULL; case "NULL":
break; channelState = UnDefType.NULL;
case "UNDEF": break;
channelState = UnDefType.UNDEF; case "UNDEF":
break; channelState = UnDefType.UNDEF;
default: break;
logger.debug("Invalid UnDef value {} for item {}", state, itemName); default:
break; logger.debug("Invalid UnDef value {} for item {}", state, itemName);
} break;
} else if (acceptedItemType.startsWith(CoreItemFactory.NUMBER + ":")) { }
// Item type Number with dimension } else if (acceptedItemType.startsWith(CoreItemFactory.NUMBER + ":")) {
if (stateType == null || "Quantity".equals(stateType)) { // Item type Number with dimension
List<Class<? extends State>> stateTypes = Collections.singletonList(QuantityType.class); if (stateType == null || "Quantity".equals(stateType)) {
channelState = TypeParser.parseState(stateTypes, state); List<Class<? extends State>> stateTypes = Collections.singletonList(QuantityType.class);
} else if ("Decimal".equals(stateType)) { channelState = TypeParser.parseState(stateTypes, state);
channelState = new DecimalType(state); } else if ("Decimal".equals(stateType)) {
channelState = new DecimalType(state);
} else {
logger.debug("Unexpected value type {} for item {}", stateType, itemName);
}
} else { } else {
logger.debug("Unexpected value type {} for item {}", stateType, itemName); switch (acceptedItemType) {
} case CoreItemFactory.STRING:
} else { if (checkStateType(itemName, stateType, "String")) {
switch (acceptedItemType) { channelState = new StringType(state);
case CoreItemFactory.STRING: }
if (checkStateType(itemName, stateType, "String")) { break;
channelState = new StringType(state); case CoreItemFactory.NUMBER:
} if (checkStateType(itemName, stateType, "Decimal")) {
break; channelState = new DecimalType(state);
case CoreItemFactory.NUMBER: }
if (checkStateType(itemName, stateType, "Decimal")) { break;
channelState = new DecimalType(state); case CoreItemFactory.SWITCH:
} if (checkStateType(itemName, stateType, "OnOff")) {
break; channelState = "ON".equals(state) ? OnOffType.ON : OnOffType.OFF;
case CoreItemFactory.SWITCH: }
if (checkStateType(itemName, stateType, "OnOff")) { break;
channelState = "ON".equals(state) ? OnOffType.ON : OnOffType.OFF; case CoreItemFactory.CONTACT:
} if (checkStateType(itemName, stateType, "OpenClosed")) {
break; channelState = "OPEN".equals(state) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
case CoreItemFactory.CONTACT: }
if (checkStateType(itemName, stateType, "OpenClosed")) { break;
channelState = "OPEN".equals(state) ? OpenClosedType.OPEN : OpenClosedType.CLOSED; case CoreItemFactory.DIMMER:
} if (checkStateType(itemName, stateType, "Percent")) {
break; channelState = new PercentType(state);
case CoreItemFactory.DIMMER: }
if (checkStateType(itemName, stateType, "Percent")) { break;
channelState = new PercentType(state); case CoreItemFactory.COLOR:
} if (checkStateType(itemName, stateType, "HSB")) {
break; channelState = HSBType.valueOf(state);
case CoreItemFactory.COLOR: }
if (checkStateType(itemName, stateType, "HSB")) { break;
channelState = HSBType.valueOf(state); case CoreItemFactory.DATETIME:
} if (checkStateType(itemName, stateType, "DateTime")) {
break;
case CoreItemFactory.DATETIME:
if (checkStateType(itemName, stateType, "DateTime")) {
try {
channelState = new DateTimeType(ZonedDateTime.parse(state, FORMATTER_DATE)); channelState = new DateTimeType(ZonedDateTime.parse(state, FORMATTER_DATE));
} catch (DateTimeParseException e) {
logger.debug("Failed to parse date {} for item {}", state, itemName);
channelState = null;
} }
} break;
break; case CoreItemFactory.LOCATION:
case CoreItemFactory.LOCATION: if (checkStateType(itemName, stateType, "Point")) {
if (checkStateType(itemName, stateType, "Point")) { channelState = new PointType(state);
channelState = new PointType(state);
}
break;
case CoreItemFactory.IMAGE:
if (checkStateType(itemName, stateType, "Raw")) {
channelState = RawType.valueOf(state);
}
break;
case CoreItemFactory.PLAYER:
if (checkStateType(itemName, stateType, "PlayPause")) {
switch (state) {
case "PLAY":
channelState = PlayPauseType.PLAY;
break;
case "PAUSE":
channelState = PlayPauseType.PAUSE;
break;
default:
logger.debug("Unexpected value {} for item {}", state, itemName);
break;
} }
} break;
break; case CoreItemFactory.IMAGE:
case CoreItemFactory.ROLLERSHUTTER: if (checkStateType(itemName, stateType, "Raw")) {
if (checkStateType(itemName, stateType, "Percent")) { channelState = RawType.valueOf(state);
channelState = new PercentType(state); }
} break;
break; case CoreItemFactory.PLAYER:
default: if (checkStateType(itemName, stateType, "PlayPause")) {
logger.debug("Item type {} is not yet supported", acceptedItemType); switch (state) {
break; case "PLAY":
channelState = PlayPauseType.PLAY;
break;
case "PAUSE":
channelState = PlayPauseType.PAUSE;
break;
default:
logger.debug("Unexpected value {} for item {}", state, itemName);
break;
}
}
break;
case CoreItemFactory.ROLLERSHUTTER:
if (checkStateType(itemName, stateType, "Percent")) {
channelState = new PercentType(state);
}
break;
default:
logger.debug("Item type {} is not yet supported", acceptedItemType);
break;
}
} }
} catch (IllegalArgumentException | DateTimeException e) {
logger.warn("Failed to parse state \"{}\" for item {}: {}", state, itemName, e.getMessage());
channelState = UnDefType.UNDEF;
} }
if (channelState != null) { if (channelState != null) {
if (onlyIfStateChanged && channelState.equals(channelsLastStates.get(channel.getUID()))) { if (onlyIfStateChanged && channelState.equals(channelsLastStates.get(channel.getUID()))) {