ChannelType object from Channel object?

Tags: #<Tag:0x00007fd312e8cb68> #<Tag:0x00007fd312e8ca28>

(namraccr) #1

Looking for a code example that shows how to use a Channel object to get the Channel’s ChannelType attributes.

e.g. having the following defined thing-types.xml:

	<channel-type id="minTemperature">
		<label>Minimum Temperature</label>
		<description>The low temperature</description>
		<state readOnly="true" pattern="%.1f %unit%" />

How do I extract the “category” value? I assume I have to get a ChannelType object, but the Channel object has neither a “getType()” method nor a “getChannelType()” method.


(Vincent Regaud) #2

You will get of these things on an item linked to that channel.

(namraccr) #3

Hi @vzorglub, thanks for the reply!

I’m afraid I don’t follow, however, The Channel object doesn’t have any methods that return an Item.

(Christoph Weitkamp) #4

I am afraid that is not possible. Those information are stored in the ChannelTypeRegistry which is not accessible from a binding.

What is your purpose to get the “category” of a channel type? Maybe there is a different way to reach your goal.

(Wouter Born) #5

I’ve used some code to get the ChannelType from the ChannelTypeRegistry in a test bundle. Though I still have plans to refactor it and remove such dependencies to make those tests consume less ESH interfaces. Such code shouldn’t be used in bindings.

(namraccr) #6

Seemed like a natural way of doing UoM updates:

if( myChannel.getChannelType().getCategory().equals("Temperature") ) {
     QuantityType<Temperature> state = new QuantityType<>(tempDouble, SIUnits.CELSIUS);
     updateState(temperatureChannelUID, state);

Since the categories are already defined, it doesn’t make sense to have to create them again myself in a new mapping.

(Christoph Weitkamp) #7

What about this? Never tried it. Just a guess.

if( myChannel.getAcceptedItemType().equals("Number:Temperature") ) {
    // do stuff

(namraccr) #8

That looks like it’ll work nicely. I’ll experiment with it.