]> git.basschouten.com Git - openhab-addons.git/blob
6ce549bc184ddd94afc7a243f52b6a516f6119dc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.webthing.internal.channel;
14
15 import static org.openhab.binding.webthing.internal.WebThingBindingConstants.BINDING_ID;
16
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.webthing.internal.client.dto.Property;
21 import org.openhab.binding.webthing.internal.link.TypeMapping;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.ThingUID;
25 import org.openhab.core.thing.binding.builder.ChannelBuilder;
26 import org.openhab.core.thing.type.ChannelTypeUID;
27
28 /**
29  * The {@link Channels} class is an utility class to create Channel based on the property characteristics as
30  * well as ChannelUID identifier
31  *
32  * @author Gregor Roth - Initial contribution
33  */
34 @NonNullByDefault
35 public class Channels {
36
37     /**
38      * create a ChannelUIFD identifier for a given property name
39      *
40      * @param thingUID the thing uid of the associated WebThing
41      * @param propertyName the property name
42      * @return the ChannelUID identifier
43      */
44     public static ChannelUID createChannelUID(ThingUID thingUID, String propertyName) {
45         return new ChannelUID(thingUID.toString() + ":" + propertyName);
46     }
47
48     /**
49      * create a Channel base on a given WebThing property
50      *
51      * @param thingUID the thing uid of the associated WebThing
52      * @param propertyName the property name
53      * @param property the WebThing property
54      * @return the Channel according to the properties characteristics
55      */
56     public static Channel createChannel(ThingUID thingUID, String propertyName, Property property) {
57         var itemType = TypeMapping.toItemType(property);
58         var channelUID = createChannelUID(thingUID, propertyName);
59         var channelBuilder = ChannelBuilder.create(channelUID, itemType.getType());
60
61         // Currently, few predefined, generic channel types such as number, string or color are defined
62         // inside the thing-types.xml file. A better solution would be to create the channel types
63         // dynamically based on the WebThing description to make most of the meta data of a WebThing.
64         // The goal of the WebThing meta data is to enable semantic interoperability for connected things
65         channelBuilder.withType(new ChannelTypeUID(BINDING_ID, itemType.getType().toLowerCase()));
66         channelBuilder.withDescription(property.description);
67         channelBuilder.withLabel(property.title);
68         Set<String> defaultTags = itemType.getTags();
69         if (!defaultTags.isEmpty()) {
70             channelBuilder.withDefaultTags(defaultTags);
71         }
72         return channelBuilder.build();
73     }
74 }