Channels cannot be linked if dynamically created [message #1752648] |
Wed, 25 January 2017 13:54  |
Eclipse User |
|
|
|
Hi,
maybe it's a bug or I do something wrong, so I need a bit help.
I'm creating channels in the initialization of a thing. The creation is working and the created channels are appearing. (Between 2-15 Channels in a List).
The problem is, if I start PaperUI then I cannot link them to a item. I I'm push the button but nothing happen.
If the simple mode is on, then the items are created and they are working like expected.
public void initialize() {
.
.
List<Channel> subChannels = new ArrayList<Channel>();
.
// In a loop
Channel newChannel = ChannelBuilder
.create(new ChannelUID(thing.getUID(), subKey), "Number")
.withType(new ChannelTypeUID(thing.getUID().getBindingId(), CHANNEL_FLOAT_VALUE)) .withDescription(subKey).withLabel(subKey).withKind(ChannelKind.STATE).build();
logger.info("create: {}", thing.getUID());
.
.
subChannels.add(newChannel);
.
.
ThingBuilder thingBuilder = editThing();
thingBuilder.withChannels(subChannels);
updateThing(thingBuilder.build());
Any ideas?
Markus
[Updated on: Wed, 25 January 2017 14:49] by Moderator
|
|
|
|
|
Re: Channels cannot be linked if dynamically created [message #1759268 is a reply to message #1752648] |
Sun, 09 April 2017 11:57   |
Eclipse User |
|
|
|
I ran into a similar issue. Dynamically created channels were linking in simple mode (Paper UI) but nothing would happened when I tried manually. Digging into the UI java code, i realized it was because controllers.min.js that manages the linking process on the client side checks the ChannelTypeUID of the channel to be linked against a list maintained in a variable called channelTypes. I haven't done extensive testing but i assume channelTypes is initialized at startup and doesn't reflect any new ChannelTypeUID created dynamically. In other words, you can create channels dynamically but not channel types ! Not sure, it is true but at least it inspired the following approach that is working for me:
1. Define all your channel types in the binding's thing_types.xml file with channel-type clauses. Let's assume you use "waterlevel" as for the channel type id.
<channel-type id="waterlevel" advanced="true">
<item-type>Number</item-type>
<label>Water Level</label>
<state readOnly="true" pattern="%.1f °F"></state>
</channel-type>
2. Declare and initialize a constant ChannelTypeUID using the is.
public final static ChannelTypeUID waterlevelTypeUID = new ChannelTypeUID(BINDING_ID, "waterlevel");
3. Modify your ChannelBuilder call to refer to this ChannelTypeUID:
.withType(waterlevelTypeUID )
By the way, this approach let's you define general aspects of the channel type (description, formatting, readonly, ... ) in the the XML file, which feels easier to me than doing so in the code.
|
|
|
Re: Channels cannot be linked if dynamically created [message #1759318 is a reply to message #1759268] |
Mon, 10 April 2017 09:47  |
Eclipse User |
|
|
|
Quote:which feels easier to me than doing so in the code.
Nice to read that! This the recommended approach for use-cases like yours, where you have (dynamic) Channels with ChannelTypes that you know in advance. Would be different of course if the ChannelTypes are dynamic too...
Afaik there are no events fired when new ChannelTypes are showing up, hence the framework would need to be enhanced first otherwise. So I'm glad you are happy with the current solution.
|
|
|
Powered by
FUDForum. Page generated in 0.06680 seconds