Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[remoteopenhab] Handle IllegalArgumentException when building channels #9638

Merged
merged 1 commit into from
Jan 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,77 +213,85 @@ public void handleCommand(ChannelUID channelUID, Command command) {
}
}

private void createChannels(List<RemoteopenhabItem> items, boolean replace) {
private boolean createChannels(List<RemoteopenhabItem> items, boolean replace) {
synchronized (updateThingLock) {
int nbGroups = 0;
List<Channel> channels = new ArrayList<>();
for (RemoteopenhabItem item : items) {
String itemType = item.type;
boolean readOnly = false;
if ("Group".equals(itemType)) {
if (item.groupType.isEmpty()) {
// Standard groups are ignored
nbGroups++;
continue;
try {
int nbGroups = 0;
List<Channel> channels = new ArrayList<>();
for (RemoteopenhabItem item : items) {
String itemType = item.type;
boolean readOnly = false;
if ("Group".equals(itemType)) {
if (item.groupType.isEmpty()) {
// Standard groups are ignored
nbGroups++;
continue;
} else {
itemType = item.groupType;
}
} else {
itemType = item.groupType;
if (item.stateDescription != null && item.stateDescription.readOnly) {
readOnly = true;
}
}
} else {
if (item.stateDescription != null && item.stateDescription.readOnly) {
readOnly = true;
String channelTypeId = String.format("item%s%s", itemType.replace(":", ""), readOnly ? "RO" : "");
ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channelTypeId);
ChannelType channelType = channelTypeProvider.getChannelType(channelTypeUID, null);
String label;
String description;
if (channelType == null) {
logger.trace("Create the channel type {} for item type {}", channelTypeUID, itemType);
label = String.format("Remote %s Item", itemType);
description = String.format("An item of type %s from the remote server.", itemType);
channelType = ChannelTypeBuilder.state(channelTypeUID, label, itemType)
.withDescription(description)
.withStateDescriptionFragment(
StateDescriptionFragmentBuilder.create().withReadOnly(readOnly).build())
.withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
channelTypeProvider.addChannelType(channelType);
}
ChannelUID channelUID = new ChannelUID(getThing().getUID(), item.name);
logger.trace("Create the channel {} of type {}", channelUID, channelTypeUID);
label = "Item " + item.name;
description = String.format("Item %s from the remote server.", item.name);
channels.add(ChannelBuilder.create(channelUID, itemType).withType(channelTypeUID)
.withKind(ChannelKind.STATE).withLabel(label).withDescription(description).build());
}
String channelTypeId = String.format("item%s%s", itemType.replace(":", ""), readOnly ? "RO" : "");
ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channelTypeId);
ChannelType channelType = channelTypeProvider.getChannelType(channelTypeUID, null);
String label;
String description;
if (channelType == null) {
logger.trace("Create the channel type {} for item type {}", channelTypeUID, itemType);
label = String.format("Remote %s Item", itemType);
description = String.format("An item of type %s from the remote server.", itemType);
channelType = ChannelTypeBuilder.state(channelTypeUID, label, itemType).withDescription(description)
.withStateDescriptionFragment(
StateDescriptionFragmentBuilder.create().withReadOnly(readOnly).build())
.withAutoUpdatePolicy(AutoUpdatePolicy.VETO).build();
channelTypeProvider.addChannelType(channelType);
}
ChannelUID channelUID = new ChannelUID(getThing().getUID(), item.name);
logger.trace("Create the channel {} of type {}", channelUID, channelTypeUID);
label = "Item " + item.name;
description = String.format("Item %s from the remote server.", item.name);
channels.add(ChannelBuilder.create(channelUID, itemType).withType(channelTypeUID)
.withKind(ChannelKind.STATE).withLabel(label).withDescription(description).build());
}
ThingBuilder thingBuilder = editThing();
if (replace) {
thingBuilder.withChannels(channels);
updateThing(thingBuilder.build());
logger.debug("{} channels defined for the thing {} (from {} items including {} groups)",
channels.size(), getThing().getUID(), items.size(), nbGroups);
} else if (channels.size() > 0) {
int nbRemoved = 0;
for (Channel channel : channels) {
if (getThing().getChannel(channel.getUID()) != null) {
thingBuilder.withoutChannel(channel.getUID());
nbRemoved++;
}
}
if (nbRemoved > 0) {
logger.debug("{} channels removed for the thing {} (from {} items)", nbRemoved, getThing().getUID(),
items.size());
}
for (Channel channel : channels) {
thingBuilder.withChannel(channel);
}
updateThing(thingBuilder.build());
if (nbGroups > 0) {
logger.debug("{} channels added for the thing {} (from {} items including {} groups)",
ThingBuilder thingBuilder = editThing();
if (replace) {
thingBuilder.withChannels(channels);
updateThing(thingBuilder.build());
logger.debug("{} channels defined for the thing {} (from {} items including {} groups)",
channels.size(), getThing().getUID(), items.size(), nbGroups);
} else {
logger.debug("{} channels added for the thing {} (from {} items)", channels.size(),
getThing().getUID(), items.size());
} else if (channels.size() > 0) {
int nbRemoved = 0;
for (Channel channel : channels) {
if (getThing().getChannel(channel.getUID()) != null) {
thingBuilder.withoutChannel(channel.getUID());
nbRemoved++;
}
}
if (nbRemoved > 0) {
logger.debug("{} channels removed for the thing {} (from {} items)", nbRemoved,
getThing().getUID(), items.size());
}
for (Channel channel : channels) {
thingBuilder.withChannel(channel);
}
updateThing(thingBuilder.build());
if (nbGroups > 0) {
logger.debug("{} channels added for the thing {} (from {} items including {} groups)",
channels.size(), getThing().getUID(), items.size(), nbGroups);
} else {
logger.debug("{} channels added for the thing {} (from {} items)", channels.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;
}
}
}
Expand Down Expand Up @@ -333,15 +341,20 @@ public void checkConnection() {
} else if (getThing().getStatus() != ThingStatus.ONLINE) {
List<RemoteopenhabItem> items = restClient.getRemoteItems("name,type,groupType,state,stateDescription");

createChannels(items, true);
setStateOptions(items);
for (RemoteopenhabItem item : items) {
updateChannelState(item.name, null, item.state, false);
}
if (createChannels(items, true)) {
setStateOptions(items);
for (RemoteopenhabItem item : items) {
updateChannelState(item.name, null, item.state, false);
}

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) {
logger.debug("{}", e.getMessage());
Expand Down