}
}
- 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;
}
}
}
} 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());