namraccr
(namraccr)
June 19, 2018, 4:21am
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">
<item-type>Number</item-type>
<label>Minimum Temperature</label>
<description>The low temperature</description>
<category>Temperature</category>
<state readOnly="true" pattern="%.1f %unit%" />
</channel-type>
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.
TiA…
vzorglub
(Vincent Regaud)
June 19, 2018, 6:11am
2
You will get of these things on an item linked to that channel.
namraccr
(namraccr)
June 19, 2018, 11:34am
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.
cweitkamp
(Christoph Weitkamp)
June 19, 2018, 3:24pm
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.
1 Like
wborn
(Wouter Born)
June 19, 2018, 11:02pm
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
(namraccr)
June 20, 2018, 10:57am
6
cweitkamp:
What is your purpose to get the “category” of a channel type? Maybe there is a different way to reach your goal.
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.
cweitkamp
(Christoph Weitkamp)
June 20, 2018, 11:16am
7
What about this? Never tried it. Just a guess.
if( myChannel.getAcceptedItemType().equals("Number:Temperature") ) {
// do stuff
}
1 Like
namraccr
(namraccr)
June 21, 2018, 11:06pm
8
That looks like it’ll work nicely. I’ll experiment with it.
Thanks!