Dynamically create channel with additional attributes

I’m developing a new Binidng (CarNet), which dynamically creates a bunch of channels. Depending on the vehicle type the set of channels can be quite different so I can’t have a static definition. I also want to use UBM setting the correct units and modify the advance and read-only attributes.

I want to create a channel like this one

    <channel-type id="test" advanced="true">
        <item-type>Number:Temperature</item-type>
        <label>Test</label>
        <state min="1" readOnly="true" pattern="%f %unit%"/>
    </channel-type>

I’m able to create the channels with the following code

                Channel channel = ChannelBuilder
                        .create(new ChannelUID(getThing().getUID(), groupId + "#" + channelId), itemType)
                        .withType(channelTypeUID).withLabel(label).withDescription(description)
                        .withKind(ChannelKind.STATE).build();
                updatedThing.withChannel(channel);

but so far didn’t found a way to set attributes like advanced or the pattern etc.

How to achieve that programmatically?

The attributes you are looking for are part of the StateDescription. You are able to create them during runtime via StateDescriptionFragmentBuilder or change them dynamically by implementing a DynamicStateDescriptionProvider.

2 Likes

good pointer, thanks

@cweitkamp how to modify the advanced attribute?

I found this a bit awkward, but to set the advanced attribute you need to create a ChannelType using the ChannelTypeBuilder and set the channel to use that. You might even have to provide a ChannelTypeProvider service for this if you don’t specify it in xml-files first, not sure I remember correctly though.

@pacive exactly. The advanced flag is part of the channel-type.