[remoteopenhab] Handle IllegalArgumentException when building channels (#9638)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
lolodomo 2021-01-03 01:25:09 +01:00 committed by GitHub
parent 8e1efa101b
commit 270f7b645d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -213,8 +213,9 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
} }
} }
private void createChannels(List<RemoteopenhabItem> items, boolean replace) { private boolean createChannels(List<RemoteopenhabItem> items, boolean replace) {
synchronized (updateThingLock) { synchronized (updateThingLock) {
try {
int nbGroups = 0; int nbGroups = 0;
List<Channel> channels = new ArrayList<>(); List<Channel> channels = new ArrayList<>();
for (RemoteopenhabItem item : items) { for (RemoteopenhabItem item : items) {
@ -242,7 +243,8 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
logger.trace("Create the channel type {} for item type {}", channelTypeUID, itemType); logger.trace("Create the channel type {} for item type {}", channelTypeUID, itemType);
label = String.format("Remote %s Item", itemType); label = String.format("Remote %s Item", itemType);
description = String.format("An item of type %s from the remote server.", itemType); description = String.format("An item of type %s from the remote server.", itemType);
channelType = ChannelTypeBuilder.state(channelTypeUID, label, itemType).withDescription(description) channelType = ChannelTypeBuilder.state(channelTypeUID, label, itemType)
.withDescription(description)
.withStateDescriptionFragment( .withStateDescriptionFragment(
StateDescriptionFragmentBuilder.create().withReadOnly(readOnly).build()) StateDescriptionFragmentBuilder.create().withReadOnly(readOnly).build())
.withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build(); .withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
@ -270,8 +272,8 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
} }
} }
if (nbRemoved > 0) { if (nbRemoved > 0) {
logger.debug("{} channels removed for the thing {} (from {} items)", nbRemoved, getThing().getUID(), logger.debug("{} channels removed for the thing {} (from {} items)", nbRemoved,
items.size()); getThing().getUID(), items.size());
} }
for (Channel channel : channels) { for (Channel channel : channels) {
thingBuilder.withChannel(channel); thingBuilder.withChannel(channel);
@ -285,6 +287,12 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
getThing().getUID(), items.size()); getThing().getUID(), items.size());
} }
} }
return true;
} catch (IllegalArgumentException e) {
logger.warn("An error occurred while creating the channels for the server {}: {}", getThing().getUID(),
e.getMessage());
return false;
}
} }
} }
@ -333,7 +341,7 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
} else if (getThing().getStatus() != ThingStatus.ONLINE) { } else if (getThing().getStatus() != ThingStatus.ONLINE) {
List<RemoteopenhabItem> items = restClient.getRemoteItems("name,type,groupType,state,stateDescription"); List<RemoteopenhabItem> items = restClient.getRemoteItems("name,type,groupType,state,stateDescription");
createChannels(items, true); if (createChannels(items, true)) {
setStateOptions(items); setStateOptions(items);
for (RemoteopenhabItem item : items) { for (RemoteopenhabItem item : items) {
updateChannelState(item.name, null, item.state, false); updateChannelState(item.name, null, item.state, false);
@ -342,6 +350,11 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
updateStatus(ThingStatus.ONLINE); updateStatus(ThingStatus.ONLINE);
restartStreamingUpdates(); restartStreamingUpdates();
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
"Dynamic creation of the channels for the remote server items failed");
stopStreamingUpdates();
}
} }
} catch (RemoteopenhabException e) { } catch (RemoteopenhabException e) {
logger.debug("{}", e.getMessage()); logger.debug("{}", e.getMessage());