]> git.basschouten.com Git - openhab-addons.git/blob
efd19299bc96fc11264987226aafeead7e986765
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.webthing.internal.client.dto.Property;
20
21 /**
22  * The {@link TypeMapping} class defines the mapping of Item types <-> WebThing Property types.
23  *
24  * Please consider that changes of 'Item types <-> WebThing Property types' mapping will break the
25  * compatibility to former releases
26  *
27  * @author Gregor Roth - Initial contribution
28  */
29 @NonNullByDefault
30 public class TypeMapping {
31
32     /**
33      * maps a property type to an item type
34      * 
35      * @param propertyMetadata the property meta data
36      * @return the associated item type
37      */
38     public static ItemType toItemType(Property propertyMetadata) {
39         String type = "string";
40         @Nullable
41         String tag = null;
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 = "switch";
52                 tag = "Switchable";
53                 break;
54             case "CurrentProperty":
55             case "FrequencyProperty":
56             case "InstantaneousPowerProperty":
57             case "VoltageProperty":
58                 type = "number";
59                 break;
60             case "HeatingCoolingProperty":
61             case "ImageProperty":
62             case "VideoProperty":
63                 type = "string";
64                 break;
65             case "BrightnessProperty":
66             case "HumidityProperty":
67                 type = "dimmer";
68                 break;
69             case "ColorModeProperty":
70                 type = "string";
71                 tag = "lighting";
72                 break;
73             case "ColorProperty":
74                 type = "color";
75                 tag = "Lighting";
76                 break;
77             case "ColorTemperatureProperty":
78                 type = "dimmer";
79                 tag = "Lighting";
80                 break;
81             case "OpenProperty":
82                 type = "contact";
83                 tag = "ContactSensor";
84                 break;
85             case "TargetTemperatureProperty":
86                 type = "number";
87                 tag = "TargetTemperature";
88                 break;
89             case "TemperatureProperty":
90                 type = "number";
91                 tag = "CurrentTemperature";
92                 break;
93             case "ThermostatModeProperty":
94                 break;
95             case "LevelProperty":
96                 if ((propertyMetadata.unit != null)
97                         && propertyMetadata.unit.toLowerCase(Locale.ENGLISH).equals("percent")) {
98                     type = "dimmer";
99                 } else {
100                     type = "number";
101                 }
102                 break;
103             default:
104                 switch (propertyMetadata.type.toLowerCase(Locale.ENGLISH)) {
105                     case "boolean":
106                         type = "switch";
107                         tag = "Switchable";
108                         break;
109                     case "integer":
110                     case "number":
111                         type = "number";
112                         break;
113                     default:
114                         type = "string";
115                         break;
116                 }
117                 break;
118         }
119
120         return new ItemType(type, tag);
121     }
122
123     /**
124      * The item type description
125      */
126     public static class ItemType {
127         private final String type;
128         private final @Nullable String tag;
129
130         ItemType(String type, @Nullable String tag) {
131             this.type = type;
132             this.tag = tag;
133         }
134
135         public String getType() {
136             return type;
137         }
138
139         public @Nullable String getTag() {
140             return tag;
141         }
142     }
143 }