]> git.basschouten.com Git - openhab-addons.git/blob
393a12c9951aaa9068e44573e7687aaee96d9e04
[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.link;
14
15 import java.util.Locale;
16 import java.util.Set;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.webthing.internal.client.dto.Property;
20 import org.openhab.core.library.CoreItemFactory;
21
22 /**
23  * The {@link TypeMapping} class defines the mapping of Item types <-> WebThing Property types.
24  *
25  * Please consider that changes of 'Item types <-> WebThing Property types' mapping will break the
26  * compatibility to former releases
27  *
28  * @author Gregor Roth - Initial contribution
29  */
30 @NonNullByDefault
31 public class TypeMapping {
32
33     /**
34      * maps a property type to an item type
35      *
36      * @param propertyMetadata the property meta data
37      * @return the associated item type
38      */
39     public static ItemType toItemType(Property propertyMetadata) {
40         String type = CoreItemFactory.STRING;
41         Set<String> tags = Set.of();
42
43         switch (propertyMetadata.typeKeyword) {
44             case "AlarmProperty":
45             case "BooleanProperty":
46             case "LeakProperty":
47             case "LockedProperty":
48             case "MotionProperty":
49             case "OnOffProperty":
50             case "PushedProperty":
51                 type = CoreItemFactory.SWITCH;
52                 break;
53             case "CurrentProperty":
54             case "FrequencyProperty":
55             case "InstantaneousPowerProperty":
56             case "VoltageProperty":
57                 type = CoreItemFactory.NUMBER;
58                 break;
59             case "HeatingCoolingProperty":
60             case "ImageProperty":
61             case "VideoProperty":
62                 type = CoreItemFactory.STRING;
63                 break;
64             case "BrightnessProperty":
65                 type = CoreItemFactory.DIMMER;
66                 tags = Set.of("Control", "Light");
67                 break;
68             case "HumidityProperty":
69                 type = CoreItemFactory.DIMMER;
70                 tags = Set.of("Measurement", "Humidity");
71                 break;
72             case "ColorModeProperty":
73                 type = CoreItemFactory.STRING;
74                 break;
75             case "ColorProperty":
76                 type = CoreItemFactory.COLOR;
77                 tags = Set.of("Control", "Light");
78                 break;
79             case "ColorTemperatureProperty":
80                 type = CoreItemFactory.DIMMER;
81                 tags = Set.of("Control", "ColorTemperature");
82                 break;
83             case "OpenProperty":
84                 type = CoreItemFactory.CONTACT;
85                 tags = Set.of("OpenState");
86                 break;
87             case "TargetTemperatureProperty":
88                 type = CoreItemFactory.NUMBER;
89                 tags = Set.of("Setpoint", "Temperature");
90                 break;
91             case "TemperatureProperty":
92                 type = CoreItemFactory.NUMBER;
93                 tags = Set.of("Measurement", "Temperature");
94                 break;
95             case "ThermostatModeProperty":
96                 break;
97             case "LevelProperty":
98                 if ((propertyMetadata.unit != null)
99                         && propertyMetadata.unit.toLowerCase(Locale.ENGLISH).equals("percent")) {
100                     type = CoreItemFactory.DIMMER;
101                 } else {
102                     type = CoreItemFactory.NUMBER;
103                 }
104                 break;
105             default:
106                 switch (propertyMetadata.type.toLowerCase(Locale.ENGLISH)) {
107                     case "boolean":
108                         type = CoreItemFactory.SWITCH;
109                         break;
110                     case "integer":
111                     case "number":
112                         type = CoreItemFactory.NUMBER;
113                         break;
114                     default:
115                         type = CoreItemFactory.STRING;
116                         break;
117                 }
118                 break;
119         }
120
121         return new ItemType(type, tags);
122     }
123
124     /**
125      * The item type description
126      */
127     public static class ItemType {
128         private final String type;
129         private final Set<String> tags;
130
131         ItemType(String type, Set<String> tags) {
132             this.type = type;
133             this.tags = tags;
134         }
135
136         public String getType() {
137             return type;
138         }
139
140         public Set<String> getTags() {
141             return tags;
142         }
143     }
144 }