[remoteopenhab] Handle IllegalArgumentException when building channels (#9638)
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
This commit is contained in:
parent
8e1efa101b
commit
270f7b645d
@ -213,77 +213,85 @@ 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) {
|
||||||
int nbGroups = 0;
|
try {
|
||||||
List<Channel> channels = new ArrayList<>();
|
int nbGroups = 0;
|
||||||
for (RemoteopenhabItem item : items) {
|
List<Channel> channels = new ArrayList<>();
|
||||||
String itemType = item.type;
|
for (RemoteopenhabItem item : items) {
|
||||||
boolean readOnly = false;
|
String itemType = item.type;
|
||||||
if ("Group".equals(itemType)) {
|
boolean readOnly = false;
|
||||||
if (item.groupType.isEmpty()) {
|
if ("Group".equals(itemType)) {
|
||||||
// Standard groups are ignored
|
if (item.groupType.isEmpty()) {
|
||||||
nbGroups++;
|
// Standard groups are ignored
|
||||||
continue;
|
nbGroups++;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
itemType = item.groupType;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
itemType = item.groupType;
|
if (item.stateDescription != null && item.stateDescription.readOnly) {
|
||||||
|
readOnly = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
String channelTypeId = String.format("item%s%s", itemType.replace(":", ""), readOnly ? "RO" : "");
|
||||||
if (item.stateDescription != null && item.stateDescription.readOnly) {
|
ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channelTypeId);
|
||||||
readOnly = true;
|
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" : "");
|
ThingBuilder thingBuilder = editThing();
|
||||||
ChannelTypeUID channelTypeUID = new ChannelTypeUID(BINDING_ID, channelTypeId);
|
if (replace) {
|
||||||
ChannelType channelType = channelTypeProvider.getChannelType(channelTypeUID, null);
|
thingBuilder.withChannels(channels);
|
||||||
String label;
|
updateThing(thingBuilder.build());
|
||||||
String description;
|
logger.debug("{} channels defined for the thing {} (from {} items including {} groups)",
|
||||||
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)",
|
|
||||||
channels.size(), getThing().getUID(), items.size(), nbGroups);
|
channels.size(), getThing().getUID(), items.size(), nbGroups);
|
||||||
} else {
|
} else if (channels.size() > 0) {
|
||||||
logger.debug("{} channels added for the thing {} (from {} items)", channels.size(),
|
int nbRemoved = 0;
|
||||||
getThing().getUID(), items.size());
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -333,15 +341,20 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
updateStatus(ThingStatus.ONLINE);
|
||||||
|
|
||||||
|
restartStreamingUpdates();
|
||||||
|
} else {
|
||||||
|
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
|
||||||
|
"Dynamic creation of the channels for the remote server items failed");
|
||||||
|
stopStreamingUpdates();
|
||||||
}
|
}
|
||||||
|
|
||||||
updateStatus(ThingStatus.ONLINE);
|
|
||||||
|
|
||||||
restartStreamingUpdates();
|
|
||||||
}
|
}
|
||||||
} catch (RemoteopenhabException e) {
|
} catch (RemoteopenhabException e) {
|
||||||
logger.debug("{}", e.getMessage());
|
logger.debug("{}", e.getMessage());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user